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 ;
}
上一篇:javascript数据结构-栈


下一篇:实现手机端上下左右滑屏的jq原生代码和使用库·两种办法