"Topic link" click here~~
"The main idea" in the given array to choose two number modulo p in case and maximum
"Problem-solving ideas":
See the official solution.
Weak weak offer code:
View codeproblem:5265 (Pog loves Szh II) Judge status:acceptedrunid:13961817 language:g++ Author:jav Aherongweicode render status:rendered by hdoj g++ Code Render Version 0.01 beta#include <stdio.h> #include <math .h> #include <string.h> #include <iostream> #include <algorithm>using namespace Std;const int n=1e6 ; __int64 num[n],n,m,p,k,ans,cnt,sum;int my_pow (int a,int n,int mod) {int ans=a,tmp=1; while (n) {if (n&1) Tmp*=ans; Ans*=ans%mod; n>>=1; } return TMP; int main () {while (~scanf ("%i64d%i64d", &n,&p)) {for (int i=0; i<n; ++i) {scanf (" %i64d ", &num[i]); Num[i]%=p; } sort (num,num+n); __int64 maxx= (num[n-1]+num[n-2])%p;///The second case of maximum __int64 maxx2; for (int i=0; i<n; ++i) {maxx2=upper_bound (num,num+n,p-num[i]-1)-num-1; if ((num[i]+num[maxx2]) <p&& (maxx2>i)) Maxx=max ((num[i]+num[maxx2])%p,maxx); } printf ("%i64d\n", Maxx); } return 0;} /* Because the number in the sequence may exceed p, all the numbers are read into the modulo operation. After that, all the numbers after the modulo are sorted from small to large. The topic asks us to ask for the maximum value of two numbers in different positions and in the sense of modulus, and now all the numbers are less than p and are in good order. So I arbitrarily selected two numbers are X and Y, apparently 0≤x+y≤2p?2. If x+y<p, then the answer of this election is x+y, if x+y≥p, then the answer is X+y? P. So we can do this: add the largest of the two numbers to modulo, set as a possible answer recorded in ans. The answer is the maximum value of the second case. Then enumerate the sorted sequence, find out the maximum number of each enumeration to the number, make the number and the number of the enumeration to the sum of the maximum and less than P, recorded as a possible answer and compared to the largest value ans found previously. This answer is the maximum possible value in the first case. The normal enumeration is to time out, but we find that if the first number is enumerated from small to large, then the other match is obviously from the largest to the smallest, so you can use one now to record the position of the current other matching number, and then each time the enumeration is decremented to the eligible. The time complexity of O (n) can be achieved. In summary, the time complexity is the fast sort O (Nlogn), the space complexity is O (n). Note that some special cases are not selectable as many times as one location. 4 2 3 0 2 2Sample output32*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5265 Pog loves Szh II (binary search)