cf1043E. Mysterious Crime(二分 字首和)

自為風月馬前卒發表於2018-10-30

題意

題目連結

Sol

考場上做完前四題的時候大概還剩半個小時吧,那時候已經困的不行了。

看了看E發現好像很可做??

又仔細看了幾眼發現這不是sb題麼。。。

先考慮兩個人,假設貢獻分別為((x, y) (a, b))

有兩種組合方式,一種是(x + b),另一種是(y + a)

(x + b >= y + a)

那麼(x – y >= a – b)

因此我們按照(x – y)排序,對於每個位置,肯定是某一個字首全選(x+b),除此之外都是(y+a)

二分之後字首和字尾和安排一下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define ull unsigned long long
#define rg register
#define pt(x) printf("%d ", x);
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
//char obuf[1<<24], *O = obuf;
//void print(int x) {if(x > 9) print(x / 10); *O++ = x % 10 + `0`;}
//#define OS  *O++ = ` `;
using namespace std;
//using namespace __gnu_pbds;
const int MAXN = 6e5 + 10, INF = 1e9 + 10, mod = 998244353;
const int base = 137;
const double eps = 1e-9;
inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1;c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int N, M, x[MAXN], y[MAXN], cha[MAXN];
struct Node {
    int x, y, val;
    bool operator < (const Node &rhs) const {
        return val < rhs.val;
    }
}a[MAXN];
int sx[MAXN], sy[MAXN], ans[MAXN];
int calc(int x, int y, int a, int b) {
    return min(x + b, y + a);
}
 main() {
    N = read(); M = read();
    for(int i = 1; i <= N; i++) {
        x[i] = a[i].x = read(), y[i] = a[i].y = read();
        cha[i] = a[i].val = a[i].x - a[i].y;
    }   
    for(int i = 1; i <= M; i++) {
        int p = read(), q = read();
        int mn = calc(a[p].x, a[p].y, a[q].x, a[q].y);
        ans[p] -= mn; ans[q] -= mn;
    }
    sort(a + 1, a + N + 1);
    sort(cha + 1, cha + N + 1);
    for(int i = 1; i <= N; i++) sx[i] = sx[i - 1] + a[i].x;
    for(int i = N; i >= 1; i--) sy[i] = sy[i + 1] + a[i].y;
    for(int i = 1; i <= N; i++) {
        int pos = upper_bound(cha + 1, cha + N + 1, x[i] - y[i]) - cha - 1;
        ans[i] += y[i] * pos + sx[pos] + (N - pos) * x[i] + sy[pos + 1] - calc(x[i], y[i], x[i], y[i]);
    }
    for(int i = 1; i <= N; i++) cout << ans[i] << " ";
    return 0;
}

相關文章