• 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): 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
    ",ans);
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    第十二节:类的定义
    第十二节:类的定义
    第十二节:类的定义
    Android核心技术Intent和数据存储篇
    Android核心技术Intent和数据存储篇
    Android核心技术Intent和数据存储篇
    ObjectDataSource配置数据源的时候,选择业务对象下拉菜单没有任何东西
    两个时间相差多少 .net中的timespan应用
    net3:DropDownList的动态绑定
    ADO:DataSet存入缓存Cache中并使用
  • 原文地址:https://www.cnblogs.com/hlmark/p/4562396.html
Copyright © 2020-2023  润新知