hdu 5265 pog loves szh II

题目链接:hdu 5265

解题思路:对输入的数取模后进行排序后二分答案即可。没有注意到溢出问题,跪了三发。。。啥都不说了,代码自有分晓(nlogn)

pog loves szh II

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


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
 
/**************************************************************
    Problem:poj 5265 pog loves szh II
    User: youmi
    Language: C++
    Result: Accepted
    Time:343MS
    Memory:2012K
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <sstream>
#include <cmath>
#include <queue>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define rep(i,n) for(int i=0;i<n;i++)
#define lson (step<<1)
#define rson (lson+1)
#define esp 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl;

using namespace std;
typedef long long ll;

int n,mod;
const int maxn=100000+10;
int a[maxn];
ll bs(int now,int l,int r)
{
    if(l>r)
        return 0;
    ll temp=0;
    ll ans=((ll)a[now]+a[r])%mod;/**这个处理很重要,因为a[now]+a[l到r]可以>mod,而大于mod时,最大值一定是a[now]+a[cnt-1](也就是a里的最大值),然后拿这个数与不超过mod的情况时的最大值进行比较就好了**/
    while(l<=r)
    {
        int mid=(l+r)>>1;
        temp=((ll)a[now]+a[mid])%mod;
        ans=temp>ans?temp:ans;
        if(((ll)a[now]+a[mid])>=(ll)mod)
            r=mid-1;
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //printf("%I64d
",((ll)1<<31)-1);
    while(~scanf("%d%d",&n,&mod))
    {
        int val;
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            sc(val);
            val%=mod;
            a[cnt++]=val;
        }
        sort(a,a+cnt);
        /**for(int i=0;i<cnt;i++)
            printf("%d ",a[i]);
        putchar('
');<*/
        ll ans=0;
        ll temp;
        for(int i=0;i<cnt;i++)
        {
            temp=bs(i,i+1,cnt-1);
            //printf("a[%d]->%d,temp->%d
",i,a[i],temp);
            ans=ans>temp?ans:temp;
        }
        printf("%I64d
",ans);
    }
    return 0;
}
不为失败找借口,只为成功找方法
原文地址:https://www.cnblogs.com/youmi/p/4564880.html