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. jQuery-niceScroll使用中父子div都存在滚动条导致错位的问题

    1.做项目时因为客户要求改变了项目的整体样式,所以导致浏览器自带的滚动条样式与项目的样式风格格格不入,所以要使用一个滚动条插件来替换浏览器自带的滚动条,我在网上搜了下,发现niceScroll这个滚动 ...

  2. 《Java编程思想》笔记 第十五章 泛型

    1 泛型 “泛型”意思就是适用于许多类型. 使用泛型的目的之一: 指定容器持有什么类型,让编译器确保正确性,而不是在运行期发现错误. 这个容器可以看成是有其他类型对象作为成员的类,而不单单只是JDK中 ...

  3. java使用maven项目(二)分模块开发

    1       整合ssh框架 1.1     依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar, 这种情况 叫 依赖传递 1.2     依赖版本冲突的解决 1.  第 ...

  4. 通过Cookie来记住用户名出现乱码问题(URL编码)

    在登录时,提交一个中文名的用户名到服务器并返回到客户端的Cookie中时, 这个过程会后台会报 java.lang.IllegalArgumentException (非法数据异常) -->在给 ...

  5. HDU 1711 Number Sequence【kmp求子串首次出现的位置】

      Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= ...

  6. HDU 1060 Leftmost Digit (数论,快速幂)

    Given a positive integer N, you should output the leftmost digit of N^N.  InputThe input contains se ...

  7. Tarjan求LCA总结

    Tarjan算法向上标记法:从x向上走到根节点,并标记所有经过的点从y向上走到根节点,当第一次遇到已标记的节点时,就找到了LCA(x, y)对于每个询问,向上标记法的时间复杂度最坏为O(n) 在深度遍 ...

  8. ubuntu 16.04.1 LTS redis安装配置

    编译安装:apt-get updateapt-get install build-essential tclwget http://download.redis.io/redis-stable.tar ...

  9. ( 转 ) .net 操作 JWT

    GitHub: https://github.com/jwt-dotnet/jwt 1.JWT定义 JWT(Json Web Token)是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明 ...

  10. DeprecationWarning: current URL string parser is deprecated解决方法

    我最近在使用mongoDB的时候,发现了这个警告语句,纳闷了,按照官方文档的教程去连接数据库还能出错,也是醉了. 后来尝试去阅读相关资料,发现只是需要将{ useNewUrlParser: true ...