码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P2777 [AHOI2016初中组]自行车比赛

时间:2017-05-17 21:09:38      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:保留   tin   amp   algo   匹配   报告   span   etc   line   

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

 

 

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

 

题目链接:洛谷P2777

正解:贪心

解题报告:

  考虑每次的匹配情况,很容易发现,每次都是当前的取最大值,然后其余的交叉匹配,维护一个区间$max$就好了,每次移动。

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 4000011;
int n,a[MAXN],b[MAXN],ans;
int c[MAXN],maxl;

inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<‘0‘||c>‘9‘) && c!=‘-‘) c=getchar();
    if(c==‘-‘) q=1,c=getchar(); while (c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w;
}

inline void work(){
	n=getint();
	//for(int i=1;i<=n;i++) a[i]=getint();
	for(int i=1;i<=n;i++) a[i]=i;
	for(int i=1;i<=n;i++) b[i]=getint();
	sort(a+1,a+n+1); sort(b+1,b+n+1);
	reverse(a+1,a+n+1); reverse(b+1,b+n+1);
	for(int i=1;i<=n;i++) c[i]=b[i]+a[n-i+2];
	for(int i=n-1;i>=1;i--) c[i]=max(c[i],c[i+1]);
	maxl=0;	
	for(int i=1;i<=n;i++) {
		if(max(maxl,c[i+1])<=b[i]+a[1]) ans++;
		maxl=max(maxl,b[i]+a[n-i+1]);
	}
	printf("%d",ans);
}

int main()
{
	work();
	return 0;
}

  

 

洛谷P2777 [AHOI2016初中组]自行车比赛

标签:保留   tin   amp   algo   匹配   报告   span   etc   line   

原文地址:http://www.cnblogs.com/ljh2000-jump/p/6869324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!