hdu 5265 pog loves szh II
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5265
pog loves szh II
Description
Pog and Szh are playing games.There is a sequence with n numbers, Pog will choose a number A from the sequence. Szh will choose an another number named B from the rest in the sequence. Then the score will be $(A+B)$ mod $p.$They hope to get the largest score.And what is the largest score?
Input
Several groups of data (no more than 5 groups,$n \geq 1000$).
For each case:
The following line contains two integers,$n(2 \leq n \leq 100000),p(1 \leq p \leq 2^{31}-1)$。
The following line contains $n$ integers $a_i(0 \leq a_i \leq 2^{31}-1)$。
Output
For each case,output an integer means the largest score.
Sample Input
4 4
1 2 3 0
4 4
0 0 2 2
Sample Output
3
2
原先用二分写挂了,估计边界没处理好,换了set好歹过了,罪过,罪过。。
![](/assets/blank.gif)
![](/assets/blank.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<set> 7 using std::max; 8 using std::multiset; 9 const int Max_N = 100010; 10 typedef unsigned long long ull; 11 ull n, p, arr[Max_N]; 12 void solve() { 13 ull res = 0; 14 multiset<ull> rec; 15 for (int i = 0; i < n; i++) { 16 scanf("%lld", &arr[i]); 17 rec.insert(arr[i] %= p); 18 } 19 multiset<ull>::iterator ite; 20 for (int i = 0; i < n; i++) { 21 rec.erase(rec.find(arr[i])); 22 ite = rec.lower_bound(p - arr[i]); 23 ull v1 = *--ite; 24 ite = rec.lower_bound(2 * p - arr[i]); 25 ull v2 = *--ite; 26 res = max(res, max((v1 + arr[i]) % p, (v2 + arr[i]) % p)); 27 rec.insert(arr[i]); 28 } 29 printf("%lld\n", res); 30 } 31 int main() { 32 #ifdef LOCAL 33 freopen("in.txt", "r", stdin); 34 freopen("out.txt", "w+", stdout); 35 #endif 36 while (~scanf("%lld %lld", &n, &p)) solve(); 37 return 0; 38 }
View Code
转载于:https://www.cnblogs.com/GadyPu/p/4564379.html
hdu 5265 pog loves szh II相关推荐
- hdu 5265 pog loves szh II STL
pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- HDU - 5265 pog loves szh II (二分或者贪心)
这题有两种做法,一种是二分,我很快就想到了(nlogn的复杂度,10^6的n). 第二种就是贪心了,我觉得这个不那么容易想到,好像速度要快一些. 首先,我们找答案的两个数x,y,有两种,一种是x+y& ...
- HDU 5265 pog loves szh II(二分)
题意: 给出n,p,n个数,从中取两个数 a,b , 求出( a + b )% p 的最大值. 解析: 先把每个数%p , 可以发现两个数加起来,要么 < p,要么 ≥ p,然后小于 2p. 那 ...
- HDOJ 5265 pog loves szh II
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5265 题意很简单,给我们n个数,和一个p,从n个数中取两个数A和B,问我们能够得到的最大的(A+B)% ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an ...
- BestCoder Round #43 第二题 pog loves szh II
pog loves szh II Accepts: 219 Submissions: 834 Time Limit: 4000/2000 MS (Java/Others) Memory Lim ...
- hdu 5264 pog loves szh I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...
- HDU 5266 pog loves szh III【LCA RMQ】
B - pog loves szh III 题目:添加链接描述 题意:找出区域l到r的LCA->找l和r的LCA 分析: 链式前向星存树,先用dfs处理结点倍增关系. 然后从循环处理较深结点,直 ...
- HDU 5266 pog loves szh III (LAC)
问题描述 pog在与szh玩游戏,首先pog在纸上画了一棵有根树,这里我们定义1为这棵树的根,然后szh在这棵树中选了若干个点,想让pog帮忙找找这些点的最近公共祖先在哪里,一个点为S的最近公共祖先当 ...
最新文章
- 接受许可协议 将禁用_知识产权小知识|知识产权许可
- python中的列表生成式
- Spring.Net学习笔记(2)-依赖注入
- 教你如何塑造JavaScript牛逼形象
- 【转】使用 OpenSSL API 进行安全编程 - 创建基本的安全连接和非安全连接
- Vue报错:3 errors and 0 warnings potentially fixable with the `--fix` option.
- centos7黑客帝国装逼
- 2021高考河北省艺术类成绩查询,2021河北省艺术类高考专业考试成绩有关规定
- JAVA笔试题常见坑_java笔试常见的选择题(坑你没商量)
- cuda驱动更新方法之一:
- 项目管理 - 常用英语词汇一
- 不负韶华 只争朝夕(2019年终报告)
- 批量拿php webshell,剑眉大侠:批量入侵网站拿webshell挂广告引流!
- 电影:少年派的奇幻漂流(Life of Pi)
- 黑马程序员---继承(下)
- LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯
- Ubuntu20.4 搭建OnlyOffice文档服务器
- wkhtmltopdf使用指南,html转图片,转pdf
- C语言——跳跃游戏 II
- erdas查看灰度直方图_实习1:ERDAS软件视窗基本操作