题目连接

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的更多相关文章

  1. HDU 5265 pog loves szh II (二分查找)

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...

  2. 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? ...

  3. HDU 5265 pog loves szh II 二分

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5265 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  4. HDU 5265 pog loves szh II (技巧)

    题意:给一个数字序列,要求再其中找到两个数,其和再模p的结果是最大的,求此和. 思路:先将输入的元素模p,排序.结果可能有两种情况: (1)a+b大于p:肯定由两个最大的数之和来产生. (2)a+b小 ...

  5. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  6. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  7. 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 ...

  8. 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 ...

  9. HDU 5266 pog loves szh III(区间LCA)

    题目链接 pog loves szh III 题意就是  求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...

随机推荐

  1. xml 和 json 的区别

    JSON(Javascript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于Javascript Programming Langu ...

  2. sql server数据库中 smallint, int ,bigint ,tinyint的区别与长度

    smallint  是一种精确的数值数据类型,其精度在算术运算后不变,采用2个字节编码 有符号的 smallint 值的范围是 -2^15-------2^15 -1,即 -32768 ----327 ...

  3. 学习记录 Java常见的几种字符集以及对 AscII的了解

     1.ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...

  4. note name

    谦谦君子:借用<周易·谦>:“初六,谦谦君子,用涉大川,吉.” 温润如玉:化用<诗经·卫风·淇奥>“有匪君子,如切如磋,如琢如磨”之义.

  5. 华为OJ平台——百钱买百鸡问题

    题目描述: 元前五世纪,我国古代数学家张丘建在<算经>一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一. 百钱买百鸡,问鸡翁.鸡母.鸡雏各几何? 思路: 这道题很简单,假 ...

  6. SVN 记录冲突、忽略

    之前对SVN不熟悉,一碰到冲突就怕得要死,不知道应该怎么处理.今天必须要正视这个问题,研究一下. 一.冲突 SVN非常智能,它不像VSS那样,一个人在改的时候必须以独占的方式签出文件,导致其他人不能够 ...

  7. 从Git仓库中恢复已删除的分支、文件或丢失的commit

    亲测可用 因为自己 commit 并且 push 后 因为冲突 提交不了,不小心做了 rebase 代码被 覆盖 用以下命令 还原: 查看所有日志 并记下 hash 值 git reflog 然后用: ...

  8. 调试时屏蔽JavaScript库代码 –Chrome DevTools Blackbox功能介绍

    代码难免会有Bug,每次我们在Chrome调试代码时,总是会进入各种各样的库代码(比如jQuery.Zepto),但实际上很多时候我们并不希望这样,要是能把这些库代码“拉黑”多好啊. 广大码农喜闻乐见 ...

  9. MAC OS下免费下载YouTube

    YouTube上有很多不错的视频,你感兴趣的视频除了可以加入自己播放列表之外,还可以将其下载到本地收藏起来.推荐这款软件“Xilisoft Download YouTube Video for Mac ...

  10. 首页banner特效

     <link href="css/swiper.min.css" rel="stylesheet" />  <script src=" ...