HDU5265——贪心——pog loves szh II

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 \geq 1000$).

For each case:

The following line contains two integers,$n(2 \leq n \leq 100000)$,$p(1 \leq p \leq 2^{31}-1)$。

The following line contains $n$ integers $a_i(0 \leq a_i \leq 2^{31}-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

Source

BestCoder Round #43

Recommend

hujie   |   We have carefully selected several similar problems for you:  5267 5266 5263 5262 5261

由于序列中的数可能超过P,所以将所有的数读入后进行取模操作。之后将取模后的所有数从小到大排序。题目要求我们求不同位置的两个数的和在取模意义下的最大值,而现在所有数都是小于P且排好序的。因此设我任意选了两个数是X和Y,显然0≤X+Y≤2P−2。若X+Y<P,则这次选数的答案就是X+Y,若X+Y≥P,则答案是X+Y−P。
那么我们可以这样做:将其中最大的两个数字相加取模,设为一个可能的答案记录在ANS中。这个答案是第二种情况的最大值。再对排序好的序列进行枚举,对每个枚举到的数,找出最大的数,使这个数与枚举到的数相加后的和最大且小于P,将之记为可能的答案并于之前找到的最大值ANS进行比较。这个答案是第一种情况中的可能的最大值。而普通的枚举是要超时的,但是我们发现如果从小到大枚举第一个数,那么另一个匹配的数显然是从大到小的,因此可以用一个NOW记录当前另一个匹配的数的位置,每次枚举时NOW递减至符合条件。可以做到O(n)的时间复杂度。
综上所述,时间复杂度为快速排序的O(nlogn),空间复杂度为O(n)。注意一些特殊情况如同一个位置不能多次选。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long   a[100100];
int main()
{

    int n;
    long long p;
    while(~scanf("%d%lld",&n,&p)){
        for(int i = 1; i <= n; i++){
            scanf("%lld",&a[i]);
            a[i]%=p;
        }
        sort(a+1,a+n+1);
        long long  max1 = (a[n]+a[n-1])%p;
        int now = n;
        for(int i = 1; i < now ;i++){

            max1 = max(max1,(a[i]+a[now])%p);
            if(a[i]+a[now] >= p){
                now--;
                i--;
            }
           }
        printf("%lld\n",max1);
    }
    return  0;
}

  

 
时间: 2024-08-04 15:19:15

HDU5265——贪心——pog loves szh II的相关文章

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>

hdu 5265 pog loves szh II STL

pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 Description pog在与szh玩游戏,首先pog找到了一个包含n个数的序列,然后他在这n个数中挑出了一个数A,szh出于对pog的爱,在余下的n−1个数中也挑了一个数B,那么szh与pog的恩爱值为(A+B)对p取模后的余数,pog与szh当然想让恩爱值越高越好,并且他们

bc #43(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): 2115    Accepted Submission(s): 609 Problem Description Pog and Szh are playing games.There is a sequence with n numbers, Pog wil

HDU 5265 pog loves szh II (二分查找)

[题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog loves szh II ) Judge Status : Accepted RunId : 13961817 Language : G++ Author : javaherongwei Code Render Status : Rendered By HDOJ G++ Code Render Versio

ACM学习历程—HDU5265 pog loves szh II(策略 &amp;&amp; 贪心 &amp;&amp; 排序)

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

hdu 5265 pog loves szh II

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则返回last的位置 #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; typedef unsigned long long ull; int main() { __int64 n,p,a[100000+5],ans; _

BestCoder Round #43 pog loves szh II (数的处理)

题意:给一个序列,找出两个数字a和b(可以相等但不可相同),要求(a+b)%p的结果最大. 思路:先将所有元素模p,再排序.要找出a和b,分两种情况,a+b>p和a+b<p.第一种,肯定是序列中两个最大的数之和.第二种,用两个指针来扫,要求找到一个小于p的和.两种求最大者.时间复杂度:排序nlogn,扫一遍n,所以nlogn. 1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 #in

HDU 5265 pog loves szh II (找两数之和)

题意:给一个数字序列,要求再其中找到两个数,其和再模p的结果是最大的,求此和. 思路:先将输入的元素模p,排序.结果可能有两种情况: (1)a+b大于p:肯定由两个最大的数之和来产生. (2)a+b小于p:设b为最大且a+b小于p,那么结果在这两个数的位置之间产生.用两个指针找出来,再与(1)中的ans比较,谁大就取谁. 若有a+b=p-1肯定是答案. 1 #include <bits/stdc++.h> 2 #define LL long long 3 using namespace std

HDU ACM 5265 pog loves szh II

题意:求数组中两个不同元素使得两元素和%p最大. 分析: 1.序列中的数可能超过P,将所有数读入后进行模P操作. 2.将取模后的所有数从小到大排序,现在所有数都是小于P且排好序的. 3.假设任意选了两个数X和Y,则0≤X+Y≤2P-2.若X+Y<P,则答案就是X+Y,若X+Y≥P,答案是X+Y-P. 从小到大枚举第一个数,另一个匹配的数显然是从大到小的,可以用POS记录当前另一个匹配的数的位置,每次枚举时POS递减至符合条件.可以做到O(n)的时间复杂度.这题还可以使用二分等方法. #inclu