Topic Portal
1 /*2 greedy/Two-point lookup: First for ai%=p, then sort, so that you can use binary search in order. The greedy idea is to find a AJ every time to make and for p-1 (if any)3 of course it is possible to have two numbers and more than P, then the value of an is optimal and is compared with an at a time4 Note: You cannot select two identical numbers5 Reflection: The game thought of%p and Sort,lower_bound, but still did not think of this greedy method to ensure maximum value, or the problem does less AH: (6 */7#include <cstdio>8#include <algorithm>9#include <cstring>Ten#include <cmath> One using namespacestd; A -typedefLong Longll; - Const intMAXN = 1e5 +Ten; the Const intINF =0x3f3f3f3f; - ll A[MAXN]; - - intMainvoid)//bestcoder Round #43 1002 Pog loves Szh II + { - //freopen ("b.in", "R", stdin); + A intN; ll P; at while(SCANF ("%d%i64d", &n, &p) = =2) - { - for(intI=1; i<=n; ++i) {scanf ("%i64d", &a[i]); A[i]%=p;} -Sort (A +1, A +1+n); - -ll ans =0; in for(intI=1; i<=n; ++i) - { to intpos = Lower_bound (A +1+i, A +1+n, P-a[i])-A; pos--; + if(POS <= n && pos! = i) ans = max (ans, (a[i] + a[pos])%p); - if(I! = n) ans = max (ans, (a[i] + a[n])%p); the } * $printf ("%i64d\n", ans);Panax Notoginseng } - the return 0; +}
Greedy/two points find Bestcoder Round #43 1002 Pog loves Szh II