hdu 5265 pog loves szh II

pog loves szh II


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




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,n≥1000).


For each case: 


The following line contains two integers,n(2≤n≤100000),p(1≤p≤231−1)。


The following line contains n integers ai(0≤ai≤231−1)。
 


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


思路:

STL大法好!


#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <map>
#include <set>
//#define ONLINE_JUDGE
#define eps 1e-6
#define INF 0x7fffffff                                          //INT_MAX
#define inf 0x3f3f3f3f                                          //int??????????????????
#define FOR(i,a) for((i)=0;i<(a);(i)++)                          //[i,a);
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define MEM3(a) memset(a,0x3f,sizeof(a))
#define MEMS(a) memset(a,'\0',sizeof(a))
#define LL __int64
using namespace std;
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
template<class T>
T Mint(T a, T b, T c) {
    if (a>b) {
        if (c>b)
            return b;
        return c;
    }
    if (c>a)
        return a;
    return c;
}
template<class T>
T Maxt(T a, T b, T c) {
    if (a>b) {
        if (c>a)
            return c;
        return a;
    }
    else if (c > b)
        return c;
    return b;
}

const int maxn=1000005;
int T;
LL n,m,k,p;
LL ans,a[maxn];

int main(){
#ifndef ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
#endif
    while(~scanf("%I64d%I64d",&n,&p)){
    	for1(i,0,n){
    		sfI(a[i]);
    		a[i]%=p;
    	}
    	sort(a,a+n);
    	int index=lower_bound(a,a+n,p/2)-a;
    	ans=(a[n-1]+a[n-2])%p;
    	for1(i,index,n){
    		k=lower_bound(a,a+i,p-a[i])-a;
    		if(k!=0){
    			ans=max(ans,(a[i]+a[k-1])%p);
    		}
    		if(ans==p-1) break;
    	}
    	pfI(ans);
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值