bestcoder#43 1002 Find two numbers in the array and modulo the maximum value of two points

Source: Internet
Author: User

bestcoder#43 1002 Find two numbers in the array and the maximum value of modulo two pog loves Szh IIaccepts:97submissions:834Time limit:4000/2000 MS (java/others)Memory limit:65536/65536 K (java/others)Problem Description

Pog and Szh is playing games. There is a sequence withN 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≥).

For each case:

The following line contains and integers,n(2≤n≤100000) , P(1≤p≤2−1) .

The following line containsNIntegers Ai ( 0≤ a I≤ 231−1) .

Output

For each case,output an integer means the largest score.

Sample Input
4 41 2 3 04 40 0 2 2
Sample Output
32
Test instructions: Given the array A and integer p, find the two number in the array, a, B, so that (a+b)%p maximum, output maximum value.
Idea: The number of all inputs to modulo, sort, then to any two in the array of the number of a, a, there is 0<=a<=p-1,0<=b<=p-1, that is, 0<=a+b<=2*p-2, so (a+b)%p maximum when there are two cases, namely a+b <p,a+b is less than but closest to P, the other is a+b>p, that is, a+b is less than, but closest to, 2*p maximum. Move the item, fix A, find the first number less than P-a, and the first number less than 2*p-a, and take the optimal one.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<cctype>using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intInf= (1<< in);Const Doubleeps=0.0000000001;Const DoublePi=acos (-1.0); ll n,p;ll A[maxn];ll Bin_search (intLeftintright,ll Key) {     while(left<=Right ) {        intMid= (left+right)/2; //cout<<left<< "" <<right<< "" <<mid<<endl;        if(A[mid]>key) right=mid-1; Else if((Mid==n&&a[mid]<=key) | | (a[mid+1]>key&&a[mid]<=key))returnA[mid]; ElseLeft=mid+1; }    return-INF;}intMain () { while(cin>>n>>p) {         for(intI=1; i<=n;i++) {scanf ("%i64d",&A[i]); A[i]%=p; } sort (A+1, a+n+1); ll ans=0;  for(intI=1; i<=n-1; i++) {ll x=bin_search (i+1, n,p-A[i]); ll y=bin_search (i+1N2*p-A[i]); //cout<< "i+1=" <<i+1<< "p-a[i]=" <<p-a[i]<<endl; //cout<< "x=" <<x<< "y=" <<y<<endl;ll T=max ((X+a[i])%p, (Y+a[i])%p); if(T>ans) ans=T; } cout<<ans<<Endl; }    return 0;}
View Code

bestcoder#43 1002 Find two numbers in the array and modulo the maximum value of two points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.