码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5265 pog loves szh II

时间:2015-06-09 06:09:45      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

pog loves szh II

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


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 的最大值

先把每个数%p , 可以发现两个数加起来 , 要么 < p 要么 大于等于 p  然后小于 2p 

那么对于大于p的,必定是最大那两个数加起来 % p  , 满足贪心的

小于p的话,就可以直接找,二分可以,因为满足单调性,所以双指针更快~

技术分享
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std ;
typedef long long LL ;
const int N = 100100 ;
LL x[N] , p ;
int n ;
int main() {
    while( ~scanf("%d%I64d",&n,&p) ) {
        for( int i = 0 ; i < n ; ++i ) {
            scanf("%I64d",&x[i]);
            x[i] %= p ;
        }
        sort( x , x + n ) ;
        LL ans = ( x[n-1] + x[n-2] ) % p ;
        x[n] = -1 ;
        for( int i = 0 ; i < n ; ++i ) if( x[i] != x[i+1] ) {
            int l = 0 , r = n - 1 , pos = -1 ;
            while( l <= r ) {
                int mid = ( l + r ) >> 1 ;
                if( x[mid] < p - x[i] ) 
                    l = mid + 1 , pos = mid ;
                else 
                    r = mid - 1 ;    
            }
            if( pos != -1 ) ans = max( ans , ( x[i] + ( pos != i ? x[pos] : ( pos ? x[pos-1] : -1 ) ) ) % p ) ;
        }
        printf("%I64d\n",ans);
    }
    return 0 ;
}
View Code

 

HDU 5265 pog loves szh II

标签:

原文地址:http://www.cnblogs.com/hlmark/p/4562396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!