pog loves szh II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2106    Accepted Submission(s): 606

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≥1000).



For each case:



The following line contains two integers,n(2≤n≤100000)。p(1≤p≤231−1)。



The following line contains n
integers ai(0≤ai≤231−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

有n个数,从这n个数中选出两个数,不能同样,使得两个数相加取模后的值最大。

能够先进行排序,然后用线性的方法找最大。

排序之前先对全部的数取一遍模。因为模运算的性质。这并不会影响结果。(a+b)%mod==( a%mod+b%mod )%mod。

能够先取排序之后的最后两个数,假设他们的和小于模p。直接输出他们的和,由于这一定是最大的。

否则的话还得找,设置 l 从0開始往后,r从n-1開始往前,对于每一个 l 。找到最右边的r,使得a[ l ]+a[ r ]<p,

这时r就不用往前了,由于往前的话值一定会变小,每次找到之后,更新最大值,同一时候 l 往前一位,可是 r 不用动,

由于数组是有序的。(细致想想就知道了)。这样时间复杂度就控制在线性阶了。

另外2^31-1是2147483647,有符号整型能表示的最大值.

#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a)) using namespace std; typedef long long ll;
typedef pair<int,int> pii; inline ll in()
{
ll res=0;char c;
while((c=getchar())<'0' || c>'9');
while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
return res;
}
ll a[100010];
int main()
{
int n,p;
while(~scanf("%d%d",&n,&p))
{
for(int i=0;i<n;i++)
{
a[i]=in()%p;
}
sort(a,a+n); //排序
ll mx=(a[n-1]+a[n-2])%p;
int l=0,r=n-1;
while(l<r)
{
while(r>=0 && a[l]+a[r]>=p)r--; //防止r<0
if(l<r) mx=max(mx,a[l]+a[r]); //r不能小于等于l
l++;
}
cout<<mx<<endl;
}
return 0;
}

hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值的更多相关文章

  1. Python3求m以内的素数、求m个数中最小的n个数

    [本文出自天外归云的博客园] 题1:求m以内的素数(m>2) def find_all_primes_in(m): def prime(num): for i in range(2, num): ...

  2. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  3. 求n个数中的最大或最小k个数

    //求n个数中的最小k个数        public static void TestMin(int k, int n)        {            Random rd = new Ra ...

  4. leetcode刷题2:两数相加add_two_numbers

    题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...

  5. LeetCode刷题--两数相加(中等)

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  6. LeetCode题解【题2】:两数相加

    原题链接:https://leetcode-cn.com/problems/add-two-numbers/ 查看请另起链接打开. 解题思路执行用时 :2 ms, 在所有 Java 提交中击败了99. ...

  7. 【LeetCode每日一题 Day 2】2. 两数相加

    大家好,我是编程熊,今天是LeetCode每日一题的第二天,一起学习的是LeetCode第二题<两数相加>. 题意 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. JAVA_新建一个方法并且求三个数中的最大值

    package wac.wev.as;//新建一个方法在求最大值import java.util.Scanner; public class MaxLian {public static void m ...

随机推荐

  1. selenium3+python自动化50-环境搭建(firefox)【转载】

    前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...

  2. [BZOJ3698] XWW的难题 网络流

    3698: XWW的难题 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 533  Solved: 275[Submit][Status][Discus ...

  3. [BZOJ2151] 种树 贪心

    2151: 种树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1151  Solved: 613[Submit][Status][Discuss] ...

  4. mvc路由问题

    概述 使用MVC Routing可以不必将URL映射到网站中特定文件.通过添加Routing规则,使URL能够安装指定的规则发送HTTP请求,这种方式能够简洁.方便.有效.快速的实现对指定页面的访问或 ...

  5. CF 834B The Festive Evening【差分+字符串处理】

    B. The Festive Evening time limit per test1 second memory limit per test256 megabytes inputstandard ...

  6. 百度之星资格赛 2016 Problem 1001

    本文链接http://www.cnblogs.com/Ash-ly/p/5494618.html 题意: 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来 ...

  7. DP(悬线法)+二维前缀和【p2706】巧克力

    Background 王7的生日到了,他的弟弟准备送他巧克力. Description 有一个被分成n*m格的巧克力盒,在(i,j)的位置上有a[i,j]块巧克力.就在送出它的前一天晚上,有老鼠夜袭巧 ...

  8. CodeForces 348C Subset Sums(分块)(nsqrtn)

    C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  9. [BZOJ2823][BZOJ1336][BZOJ1337]最小圆覆盖(随机增量法)

    算法介绍网上有很多,不解释了. 给出三点坐标求圆心方法:https://blog.csdn.net/liyuanbhu/article/details/52891868 记得先random_shuff ...

  10. [CF911C]Three Garlands

    题目大意: 给你三个灯,分别以k1秒一次,k2秒一次和k3秒一次的频率闪烁着. 你可以自定义三个灯开启的时间,问是否有一种方案,使得max(k1,k2,k3)秒之后,每秒钟都至少有一盏灯闪烁. 思路: ...