POG loves Szh II
Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2115 Accepted Submission (s): 609
Problem Descriptionpog and Szh is playing games. There is a sequence withNNumbers, Pog would choose a number a from the sequence. Szh would choose an another number named B from the rest of the sequence. Then the score'll be (A+B) mod p. They hope to get the largest score. and what is the largest score?
Inputseveral groups of data (no more than5Groupsn≥ ).
For each case:
The following line contains and integers,n(2≤n≤100000) ,P(1≤p≤2−1) 。
The following line containsN integers Ai ( 0≤ a I≤ 231−1) .
Outputfor each case,output an integer means the largest score.
Sample INPUT4 41 2 3 04 40 0 2 2
Sample Output32
Sourcebestcoder Round #43
Recommendhujie | We have carefully selected several similar problems for you:5338 5337 5336 5335 5334 for read-in a[i] mod p for any 0=<i,j<= N-1 A[i]+a[j] greater than or equal to 0 is less than or equal to 2p-2 so a[i]+a[j] results only two cases, one is <p, is directly the answer, the other is >=p <=2p-2 a[i]+a[j]-p is the answer to A[i] ascending order, if there is a [I]+a[j]>=p, then the largest must be a[n-1]+a[n-2] for a[i]+a[j] less than p, we enumerate I, find the largest j, make A[i]+a[j]<p, which is the biggest answer for I. If the Direct enumeration O (N2) will time out because the A[i] array is already ordered, we can take advantage of the monotonicity of A[i], looking from both sides toward the middle. This complexity is O (N) The optimization just a few days ago ....
/************************************************************************* > File name:code/bc/#43/b.cpp > AUTHOR:111QQZ > Email: [email protected] > Created time:2015 July 31 Friday 16:38 13 seconds ************************ ************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intINF =0x7fffffff;Const intn=1e5+7; LL A[n];intn,p;intMain () { while(SCANF ("%d%d", &n,&p)! =EOF) {LL ans= -1; for(inti =0; I < n; i++) {scanf ("%lld",&A[i]); A[i]=a[i]%p; } sort (A,a+N); Ans= (a[n-1]+a[n-2])%p; intj = N1; for(inti =0; I < n2; i++ ) { while(i<j&&a[i]+a[j]>=p) j--; Ans= Max (ans, (a[i]+a[j])%Q); } cout<<ans<<Endl; } return 0;}
BC #43 (hdu 5265) Pog loves Szh II