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

首先对所有的数模p再排序是没有问题的。

然后就是怎么求模p的最大值。

首先如果a[n-2]+a[n-1]小于p,自然最大值就是它。

然而打BC的时候并没有考虑透彻,然后想对最大值进行枚举,看能不能凑到这个最大值,因为直接暴力枚举会T,然后想二分,但是貌似不能直接二分,于是我还是设了一个左值lt和右值rt,rt自然初始为p-1,而lt初始值可以取a[0]+a[n-1]和a[n-2]+a[n-1]里面较大的。然后就是如果mid值能凑到,自然更新lt为mid值,然后判断rt能否取到,否则自减,然而这样是过不了极限数据的。不过貌似OJ测试数据并没有,这样是可以过的。。。不过当时没考虑int爆掉,还有二分也写搓被Hack掉了。

然后就是正规做法了:

考虑到之前模过后进行的排序,所以任意两个数相加的和比p大的值要小于p,所以减一次就能比p小,所以最大值只有两种可能,x+y或x+y-p。

然后对于后者,最大自然是a[n-2]+a[n-1]-p(在得到它们和大于p的前提下),然后只用找x+y的最大了。

然后从小到大枚举每个x,y自然是从大到小排除,得到首个比p小的。

由于对于x+c来说,x+c+y > x+y,所以,如果之前x+y超过p的,x+c+y自然也超过p了。所以往后的y不需要返回继续减小即可。

暴力枚举代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; int n;
LL s[], p; inline Max(LL x, LL y)
{
if (x < y)
return y;
else
return x;
} void Input()
{
for (int i = ; i < n; ++i)
{
scanf("%I64d", &s[i]);
s[i] %= p;
}
sort(s, s+n);
} int binarySearch(LL key)
{
int lt = , rt = n-, mid;
while (lt <= rt)
{
mid = (lt+rt) >> ;
if(key == s[mid])
return mid;
if(key < s[mid])
rt = mid-;
if(key > s[mid])
lt = mid+;
}
return -;
} bool judge(LL k)
{
for (int i = ; i < n; ++i)
{
if (k-s[i] >= && binarySearch(k-s[i]) != -)
return true;
if (k-s[i] <= && binarySearch(k-s[i]+p) != -)
return true;
}
return false;
} void Work()
{
LL maxs = s[n-]+s[n-];
if (maxs < p)
{
printf("%I64d\n", maxs);
return;
}
LL lt = Max((s[]+s[n-])%p, (s[n-]+s[n-])%p), rt = p-, mid;
while (lt < rt)
{
mid = (lt+rt) >> ;
if (judge(mid))
lt = mid;
if (judge(rt))
{
printf("%I64d\n", rt);
return;
}
else
rt--;
}
printf("%I64d\n", lt);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%I64d", &n, &p) != EOF)
{
Input();
Work();
}
return ;
}

正确代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define LL long long using namespace std; int n;
LL p, a[]; void Input()
{
for (int i = ; i < n; ++i)
{
scanf("%I64d", &a[i]);
a[i] %= p;
}
sort(a, a+n);
} void Work()
{
LL maxs = a[n-]+a[n-];
if (maxs < p)
{
printf("%I64d\n", maxs);
return;
} maxs -= p;
int lt = , rt = n-;
while (lt < rt)
{
while (a[lt]+a[rt] >= p && lt < rt)
rt--;
if (lt >= rt)
break;
maxs = max(maxs, a[lt]+a[rt]);
lt++;
}
printf("%I64d\n", maxs);
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d%I64d", &n, &p) != EOF)
{
Input();
Work();
}
return ;
}

ACM学习历程—HDU5265 pog loves szh II(策略 && 贪心 && 排序)的更多相关文章

  1. ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)

    Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...

  2. hdu 5265 pog loves szh II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...

  3. 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? ...

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

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

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

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...

  6. HDU 5265 pog loves szh II 二分

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5265 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  7. ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

    Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...

  8. HDU 5265 pog loves szh II (技巧)

    题意:给一个数字序列,要求再其中找到两个数,其和再模p的结果是最大的,求此和. 思路:先将输入的元素模p,排序.结果可能有两种情况: (1)a+b大于p:肯定由两个最大的数之和来产生. (2)a+b小 ...

  9. HDU pog loves szh II (数的处理)

    题意: 给一个序列,找出两个数字a和b(可以相等但不可相同),要求(a+b)%p的结果最大. 思路: 先将所有元素模p,再排序.要找出a和b,分两种情况,a+b>p和a+b<p.第一种,肯 ...

随机推荐

  1. LVS + KEEPALIVED + WINDOWS SERVER 2008 R2 ------高可用负载均衡(转)

    工作原理此处不作讲解,自己去官方网站学习(http://www.linuxvirtualserver.org),这里重点讲如何配置!注:最好从官方网站对其进行了解,不至于会对某些问题产生误解,尽管是英 ...

  2. Java设计模式(九)责任链模式 命令模式

    (十七)责任链模式 责任链模式的目的是通过给予多个对象处理请求的机会,已解除请求发送者与接受者之间的耦合关系.面对对象的开发力求对象之前保持松散耦合,确保对象各自的责任最小化.这种设计能够使得系统更加 ...

  3. 可执行Jar包调用动态链接库(DLL/SO)

    踩过了很多的坑,查了很多资料,在此记录一下,以SpringBoot项目为基础. Maven加入JNA依赖 <!-- JNA start --> <dependency> < ...

  4. 关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题

    关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题,有需要的朋友可以参考下. php调用java写的soap接口经验:   场景一: java是以数组的形式接收参数的 ...

  5. 【Python】IDLE启动错误

    启动IDLE时报Subprocess Startup Error错误 错误信息 IDLE's subprocess didn't make connection.Either IDLE cant't ...

  6. Vim 打开文件同时定位到某一行

    在linux下,当后台某一行报警出错后,想用vim打开文件同时定位到某一行, Vim +某一行 filename 即可.

  7. Android 超高仿微信图片选择器 图片该这么载入

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自:[张鸿洋的博客] 1.概述 关于手机图片载入器,在当今像 ...

  8. Swift 学习笔记 (解决Swift闭包中循环引用的三种方法)

    话不多说 直接上代码 class SmartAirConditioner { var temperature:Int = //类引用了函数 var temperatureChange:((Int)-& ...

  9. 性能测试--yslow

    YSlow YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会 ...

  10. C语言实现 操作系统 银行家算法

    /**************************************************** 银行家算法 算法思想: 1. 在多个进程中,挑选资源需求最小的进程Pmin. 可能存在多类资 ...