POG loves Szh II
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=5265
Descriptionpog in the game with Szh, first POG found a sequence containing n number, then he picked a number in the N number a,szh out of the pog of love, in the remaining number of n−1 also picked a number B, then Szh and POG of the value of the affectionate (a+b) The remainder of P-modulo, POG and szh of course want to make the value of love higher the better, and they want to know the highest love value is how much.
Input
Several sets of data (no more than 5 groups of n≥1000).
The first row of each group of data is two integers n (2≤n≤100000), P (1≤p≤231−1).
The next line is n integer ai (0≤ai≤231−1).
Output
For each query for each group, the output line represents the maximum affectionate value of POG and Szh.
Sample Input
4 4
1 2 3 0
4 4
0 0 2 2
Sample Output
3
2
HINT
Test instructions
Exercises
STL Dafa Good!
STL Saves the world!
Code:
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)#defineMAXN 2000001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;} while(x>0) ch[++num]=x%Ten, x/=Ten; while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************ll A[maxn];multiset<ll>S;intMain () {//test; intN; ll P; while(cin>>n>>p) {s.clear (); for(intI=0; i<n;i++) {A[i]=read (); A[i]%=p; S.insert (A[i]); } ll ans=0; for(intI=0; i<n;i++) {s.erase (S.find (a[i)); Multiset<ll>::iterator It=s.lower_bound (P-A[i]); It--; Ans=max (ans, (a[i]+*it)%p); It=s.lower_bound (p*2-A[i]); It--; Ans=max (ans, (a[i]+*it)%p); S.insert (A[i]); } cout<<ans<<Endl; } }
Hdu 5265 Pog loves Szh II STL