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好歹过了,罪过,罪过。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<set>
using std::max;
using std::multiset;
const int Max_N = ;
typedef unsigned long long ull;
ull n, p, arr[Max_N];
void solve() {
ull res = ;
multiset<ull> rec;
for (int i = ; i < n; i++) {
scanf("%lld", &arr[i]);
rec.insert(arr[i] %= p);
}
multiset<ull>::iterator ite;
for (int i = ; i < n; i++) {
rec.erase(rec.find(arr[i]));
ite = rec.lower_bound(p - arr[i]);
ull v1 = *--ite;
ite = rec.lower_bound( * p - arr[i]);
ull v2 = *--ite;
res = max(res, max((v1 + arr[i]) % p, (v2 + arr[i]) % p));
rec.insert(arr[i]);
}
printf("%lld\n", res);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%lld %lld", &n, &p)) solve();
return ;
}
hdu 5265 pog loves szh II的更多相关文章
- HDU 5265 pog loves szh II (二分查找)
[题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...
- 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 二分
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5265 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 5265 pog loves szh II (技巧)
题意:给一个数字序列,要求再其中找到两个数,其和再模p的结果是最大的,求此和. 思路:先将输入的元素模p,排序.结果可能有两种情况: (1)a+b大于p:肯定由两个最大的数之和来产生. (2)a+b小 ...
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- 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 5264 pog loves szh I 水题
pog loves szh I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 5266 pog loves szh III(区间LCA)
题目链接 pog loves szh III 题意就是 求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...
随机推荐
- 慕课网-安卓工程师初养成-2-6 Java中的数据类型
来源:http://www.imooc.com/code/1230 通常情况下,为了方便物品的存储,我们会规定每个盒子可以存放的物品种类,就好比在“放臭袜子的盒子”里我们是不会放“面包”的!同理,变量 ...
- Windows7下安装IIS出现“出现错误,并非所有的功能被成功更改
1.开始,搜索输入UAC-->选择 “更改用户账户控制设置” 2.调到最低 3.打开控制面板-->程序-->打开或关闭windows功能,去掉图里的2个选项,点确定,重启 4.重启后 ...
- java中List Set Map使用
@Test public void run() { ArrayList<String> list= ...
- C++异常:no matching function for call to "Matrix(Matrix&)"
C++异常:no matching function for call to "Matrix(Matrix&)" 我定义了一个类叫Matrix,其中构造函数explicit ...
- [转]常用电器认证标志 && 手机频段
一个手电筒就可以算得上一件家用电器的时代已经过去了,现在,谁家里不得有个几件?大家肯定看到了这些电器上贴的各种各样的认证标志了吧?看的人是眼花缭乱,目不暇接,更有一些是“洋认证”.下面罗列出一些常见的 ...
- 二级路由器设置为何要关闭DHCP服务
因为如果不关闭dhcp的话会和一级路由相冲突,你这个因该是lan口的连接法吧,如果不然的话是不需要关闭dhcp功能的路由器和路由器的连接方法有两种,一种是wan口连,一种是lan口连方法1:WAN口连 ...
- 网络流量监控shell脚本
网络收发包计数记录在 /proc/net/dev 文件中, 要取得流量, 只需要读取里面的内容两次, 然后相减, 再除以时间间隔即可. #!/bin/bash #Usage1,record in fi ...
- C# 常用小技巧
int? id 表示id是可以为null的整型 跟Nullable<int> id 是一样的 id ?? 1等于 id==null?1:id;
- vs2012 快捷键修改
打开:工具-->选项 搜索:剪切行 移除原有的 Crtl+L 命令 改为:Ctrl+D
- 图片轮播jQuery
<script type="text/javascript"> //图片轮播 var bannerIndex = 0; ba ...