跳过正文
  1. Posts/

bc #43(hdu 5265) pog loves szh II (单调性优化)

·2 分钟

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 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

BestCoder Round #43

Recommend

hujie | We have carefully selected several similar problems for you: 5338 5337 5336 5335 5334

对于读入的a[i] mod p后

对于任意 0=

所以a[i]+a[j]的结果只有两种情况,一种是 =p <=2p-2 a[i]+a[j]-p 是答案

把a[i]升序排列,如果存在a[i]+a[j]>=p ,那么最大的一定是a[n-1]+a[n-2]

对于a[i]+a[j]小于p的,我们枚举i,找到最大的j,使得a[i]+a[j]

如果直接枚举O(N2)会超时

由于a[i]数组已经是有序的了

我们可以利用a[i]的单调性,从两边往中间找。

这样复杂度就是O(N)了

这个优化前几天刚遇到过。。。。

 1/*************************************************************************
 2	> File Name: code/bc/#43/B.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年07月31日 星期五 16时38分13秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x7fffffff;
31const int N=1E5+7;
32LL a[N];
33int n,p;
34
35int main()
36{
37    while (scanf("%d %d",&n,&p)!=EOF)
38    {
39	LL ans = -1;
40	for ( int i = 0 ; i < n;  i++ )
41	{
42	    scanf("%lld",&a[i]);
43	    a[i]=a[i]%p;
44	}
45	sort(a,a+n);
46	ans = (a[n-1]+a[n-2])%p;
47	int j = n-1;
48	for ( int i = 0 ; i  < n-2 ; i++ )
49	{
50	    while (i<j&&a[i]+a[j]>=p) j--;
51	    ans = max(ans,(a[i]+a[j])%p);
52	}
53	cout<<ans<<endl;
54
55    }
56
57	return 0;
58}

相关文章

SGU 456 Annuity Payment Scheme

·1 分钟
水题,推个公式出来,注意精度…一遍A 1 2 /************************************************************************* 3 > File Name: code/2015summer/#5/D.cpp 4 > Author: 111qqz 5 > Email: rkz2013@126.com 6 > Created Time: 2015年07月30日 星期四 13时17分26秒 7 ************************************************************************/ 8 9 #include<iostream> 10 #include<iomanip> 11 #include<cstdio> 12 #include<algorithm> 13 #include<cmath> 14 #include<cstring> 15 #include<string> 16 #include<map> 17 #include<set> 18 #include<queue> 19 #include<vector> 20 #include<stack> 21 #define y0 abc111qqz 22 #define y1 hust111qqz 23 #define yn hez111qqz 24 #define j1 cute111qqz 25 #define tm crazy111qqz 26 #define lr dying111qqz 27 using namespace std; 28 #define REP(i, n) for (int i=0;i<int(n);++i) 29 typedef long long LL; 30 typedef unsigned long long ULL; 31 const int inf = 0x7fffffff; 32 int s,m,p; 33 double ans; 34 35 double cal(double x,int n) 36 { 37 double res = 1.0; 38 for ( int i = 1 ; i <= n ; i++ ) 39 { 40 res = res * x; 41 } 42 // cout<<"res:"<<res<<endl; 43 return res; 44 } 45 int main() 46 { 47 cin>>s>>m>>p; 48 double sum = 0; 49 double per = p*1.0/100+1; 50 for ( int i = 1 ; i <= m; i++ ) 51 { 52 sum=sum+1.0/cal(per,i); 53 // cout<<"sum:"<<sum<<endl; 54 } 55 // cout<<sum<<endl; 56 cout<<fixed<<setprecision(5)<<s*1.0/sum<<endl; 57 58 return 0; 59 }

poj 2823 Sliding Window (单调队列)

·4 分钟
Sliding Window 看这个问题:An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position.Your task is to determine the maximum and minimum values in the sliding window at each position.