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好歹过了,罪过,罪过。。

 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

 

posted @ 2015-06-09 19:34  GadyPu  阅读(152)  评论(0编辑  收藏  举报