# pog loves szh II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2011    Accepted Submission(s): 576

Problem Description
Pog and Szh are playing games.There is a sequence with n 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,n1000).

For each case:

The following line contains two integers,n(2n100000)p(1p2311)

The following line contains n integers ai(0ai2311)

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

Sample Input
4 4 1 2 3 0 4 4 0 0 2 2

Sample Output
3 2

简单说一下本题题意：给你两个数  n，p，然后在给你 n 个数，让你在其中找到两个数 a ，b，使得（a+b）%p 达到最大

这是杭电BC上面的一道题，有个公式是 (a+b)modc==(a mod c+b mod c ) mod  c

根据这个公式，问题便能解决了， 先把所有的数都对p取模，然后再对p取模，再排序，之后再从两端向中间找，找到最大的两个数。注意范围，两个int 有可能超int。

代码如下：

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
#define  LL __int64 //与long long 型一样，格式控制是%I64d
int main()
{
LL  a[100008];
LL  n,p;
while(~scanf("%I64d%I64d",&n,&p))
{
for(LL i=0; i<n; i++)
{
scanf("%I64d",&a[i]);
if(a[i]>=p)
a[i]=a[i]%p;
}
sort(a,a+n);
LL left=0,right=n-1;
//此处求得的数是大于p时的最大的解
LL max1=(a[n-1]+a[n-2])%p;
//从两边向中间查找
while(left<right)
{
//如果大于p，不管，前面已求出
if(a[left]+a[right]>=p)
right--;
else
{
if(a[left]+a[right]>max1)
max1=a[left]+a[right];
left++;
}
}
printf("%I64d\n",max1);
}
}


06-09 416

06-09 453

06-07 407

07-07 794

06-19 195

08-10 113

06-09 399

06-08 253

06-29 612