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 ...
随机推荐
- 1093 字符串A+B (20 分)
#include <iostream> using namespace std; int main() { int n; string s1, s2; getline(cin,s1); g ...
- Python爬取LOL英雄皮肤
Python爬取LOL英雄皮肤 Python 爬虫 一 实现分析 在官网上找到英雄皮肤的真实链接,查看多个后发现前缀相同,后面对应为英雄的ID和皮肤的ID,皮肤的ID从00开始顺序递增,而英雄ID跟 ...
- 分享一下Ubuntu好用的源
vim /etc/apt/sources.list 然后用G跳转到最后一行,然后[ESC]切换到命令行模式,然后键入[o](表示在当前行后插入).也可以多插入几个空行.这样可以有条理的和系统默认的区分 ...
- php中慎用==
var_dump(' 123fg456'==123);var_dump('some string' == 0);var_dump(123.0 == '123d456');var_dump(0 == & ...
- js 常用基本知识
Object.isObject = function(obj){ return obj != null && typeof obj === 'object' && Ar ...
- Oracle触发器简单使用记录
在ORACLE系统里,触发器类似函数和过程.1.触发器类型:(一般为:语句级触发器和行级触发器.) 1).DML触发器: 创建在表上,由DML事件引发 2).instead of触发器: 创建在视图上 ...
- vue开发的一些设置以及技巧
引入其它css样式 需要加~ @找到的是src目录 对于常用的路径可以设置别名 @代表src 常用的地址在webpack.base.conf.js中 ...
- element-ui日期组件DatePicker选择日期范围赋值编辑问题
最近在项目中使用element-UI的日期范围组件时遇到一个问题,相信很多人也做过这种场景,一个录入页面也同时是编辑页面,编辑的时候就需要先赋值.但是我给date组件赋值后,确无法操作了,change ...
- ionic3 cordova 调取软键盘
应用场景,因为兼容ios,安卓问题,不能直接调用激活软键盘方法.只有在点击按钮时让input框自动获取焦点,激活软键盘.然后把input框定位在键盘上方,软键盘激活可以监听到键盘高度. 先下载keyb ...
- 1.python学习计划
1.python学习 第一次使用博客园作为学习记录日志,希望能在这里记录自己的学习点滴. 慢慢去挖掘它的强大功能吧