题目连接

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. 自定义PageControl样式

    #define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0) //调用方法 改变PageControl样式 ...

  2. nginx 配置.json文件直接访问

    不要弹出下载 配置nginx.conf http { include mime.types; default_type application/json;

  3. linux C中va_list用法

    #include <stdio.h> #include <stdarg.h> int demo( int, ... ); int main( void ) { demo(1, ...

  4. devexpress中应用于girdviw中HtmlDataCellPrepared事件与CellEditorInitialize事件的区别

    HtmlDataCellPrepared 事件为页面展示的时候对页面做的初始化(将id变为name) ​CellEditorInitialize 事件为页面在编辑时(新增.修改)时做的初始化,如将值填 ...

  5. SVN与TortoiseSVN实战:标签与分支

    最近在写<IOS性能调优系列>,今天偷个懒,写写SVN与TortoiseSVN实战的第二篇,标签与分支. 第一篇详见<SVN与TortoiseSVN实战:从入门到精通> SVN ...

  6. jenkins smtp设置调试

  7. js 函数function的几种形式

    //有的时候我们一直都在使用函数,但是却不知道函数使用的正真含义 //其中一个重要的目的是为了提高代码的复用率 //其二函数可以传递参数并有返回值 //函数声明 //不带参数,不带返回值 functi ...

  8. Oracle笔记 四、增删改、事务

    1.插入 insert into dept values(50, 'soft', 'Guangzhou'); insert into dept(deptno, dname) values(60, 's ...

  9. Flexbox布局详解

    弹性框布局 (flexbox) 添加了级联样式表级别 2 修订版 1 (CSS2.1) 中定义的四个基本布局模式:块布局.内联布局.表格布局和定位布局.使用弹性框布局功能,你可以更加轻松地设计复杂网页 ...

  10. js怎样改变div的宽度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...