cf1043E. Mysterious Crime(二分 前缀和)

题意

题目链接

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;
}
原文地址:https://www.cnblogs.com/zwfymqz/p/9874646.html