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;
}
cf1043E. Mysterious Crime(二分 前缀和)的更多相关文章
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- Poj 3061 Subsequence(二分+前缀和)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...
- CodeForces 1043D Mysterious Crime 区间合并
题目传送门 题目大意: 给出m个1-n的全排列,问这m个全排列中有几个公共子串. 思路: 首先单个的数字先计算到答案中,有n个. 然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个 ...
- [题解]Codeforces Round #519 - D. Mysterious Crime
[题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...
- 【BZOJ-1926】粟粟的书架 二分 + 前缀和 + 主席树
1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Sec Memory Limit: 552 MBSubmit: 616 Solved: 238[Submit][Statu ...
- [NOIP2011] 聪明的质监员 二分+前缀和
考试的时候打的二分但没有用前缀和维护.但是有个小细节手误打错了结果挂掉了. 绝对值的话可能会想到三分,但是注意到w增大的时候y是减小的,所以单调性很明显,用二分就可以.但注意一个问题,就是二分最后的结 ...
- 【CF587D】Duff in Mafia 二分+前缀优化建图+2-SAT
[CF587D]Duff in Mafia 题意:给你一张n个点m条边的无向图,边有颜色和边权.你要从中删去一些边,满足: 1.任意两条删掉的边没有公共的顶点.2.任意两条剩余的.颜色相同的边没有公共 ...
- Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀
D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 megabytes ...
随机推荐
- 重载<<运算符第二个参数必须加上const
如题,在重载<<时不停的报错,说找不到匹配的函数,仔细观察和书上的样例对比后发现,我的第二个参数缺少了一个const,抱着试一试的心态,因为平时也没注意const这个东西,也不经常用,试了 ...
- 2016级算法第三次上机-D.双十一的抉择
915 双十一的抉择 思路 中等题.简化题目:一共n个数,分成两组,使得两组的差最接近0,就是说要使两组数都尽可能的接近sum/2. 思路还是很混乱的,不知道如何下手,暴力也挺难的,还不能保证对.想一 ...
- 虚拟立方体调用非共享维度的时候需要指定cubeName,否则Schema无效
但是,即使把非共享维度放上去,会出现一种情况. 一旦放入资产负债的非公共维度进去后,报表的度量值只能出现资产负债的度量值,不能出现损益的度量值了!
- javax.jms.JMSException: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection timed out: connect
本地测试向服务器中ActiveMQ添加队列数据,报错连接超时 解决: 查看服务器端口号是否存在,(最好是0.0.0.0格式的,虽然暂时还不知道为啥得这个格式) 通过telnet测试该端口不通(6161 ...
- linux中ls命令使用选项
ls:英文全名:List即列表的意思 -a 列出目录下的所有文件,包括以 . 开头的隐含文件.-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出.-c 输出文件的 i 节 ...
- js对函数参数的封装
对函数参数的封装 一个原始函数有n个参数,用wrap对函数进行封装,生成一个新的函数,当给它传入参数数量为n的时候,将执行原始函数,否则不执行 //函数封装 function wrap(func){ ...
- .net core webapi 在原有基础上修改。
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- PIE SDK影像坏线修复
1.算法功能简介 坏条带的由来:2003年5月31日,Landsat-7ETM+机载扫描行校正器(SLC) 故障,导致此后获取的影像出现了数据条带丢失,严重影响了Landsat ETM遥感影像的使用. ...
- 实现Map按key或按value排序
原理思路:用List实现排序,然后将List中的值遍历存入到LinkedHashMap 实现方式: //这里将map.entrySet()转换成list List<Map.Entry<St ...
- 自学springboot
参考资料 https://www.renren.io/guide/