跳到正文

题解

CF1601D Difficult Mountain

_OMA_约 4 分钟
原文
文章信息与版本

文章信息

发布
2021年11月17日
收录
2026年7月24日
更新
2026年10月28日
篇幅
约 4 分钟
获赞
9
题目
CF1601D

版本

  1. v1当前

牛逼贪心题。

以 max⁡(ai,si)\max(a_{i},s_{i}) 为第一关键字, sis_{i} 为第二关键字从小到大排序。

然后依次枚举,能选就选,就是最优答案。

证明:

按 max⁡(a,s)=a/s\max(a,s)=a/s 分类讨论。

  • max⁡i=ai,max⁡j=aj\max_{i}=a_{i},\max_{j}=a_{j} ,不妨设 ai<aja_{i}<a_{j} ,如果 jj 先上, ii 一定上不去,所以不如让 ii 先去试试。

  • max⁡i=ai,max⁡j=sj\max_{i}=a_{i},\max_{j}=s_{j} ,如果 ai>sja_{i}>s_{j} 则同上, ii 先上, jj 一定上不去,所以不如让 jj 先去试试。 如果 ai≤sja_{i}\le s_{j} ,那么 ii 先上对 jj 显然不会有影响。

  • max⁡i=si,max⁡j=sj\max_{i}=s_{i},\max_{j}=s_{j} ,不妨设 si<sjs_{i}<s_{j} ,如果 si<ajs_{i}<a_{j} ,显然是要 ii 先去试试,如果 si>ajs_{i}>a_{j} ,先让 ii 上,对 jj 显然没有影响。

取等类似,此处不再展开。

C++
#include<cstdio>
#include<cctype>
#include<iostream>
#include<algorithm>
using std::max;
using std::sort;
#define re register
const int MAX = 5e5+3;
#define long long long
#define scanf oma=scanf
#define freopen file freopen
int oma;
FILE* file;
namespace some
{
	struct stream
	{
		template<typename type>inline stream &operator >>(type &s)
		{
			bool w=0; s=0; char ch=getchar();
			while(!isdigit(ch)){ w|=ch=='-'; ch=getchar(); }
			while(isdigit(ch)){ s=(s<<1)+(s<<3)+(ch^48); ch=getchar(); }
			return s=w?-s:s,*this;
		}
	}cin;
	int ans;
	int n,d;
	struct data
	{
		int s,a;
		inline friend bool operator <(const data &x,const data &y)
		{ return max(x.s,x.a)==max(y.s,y.a)?x.s<y.s:max(x.s,x.a)<max(y.s,y.a); }
	}p[MAX];
	#define debug(s) printf("%s\n",s)
}using namespace some;
namespace OMA
{
	auto main = []() -> signed
	{
		//#define local
		#ifdef local
		debug("look here! if you want submit,please closed this");
		freopen("node.in","r",stdin); freopen("my.out","w",stdout);
		#endif
		cin >> n >> d;
		for(re int i=1; i<=n; i++)
		{ cin >> p[i].s >> p[i].a; }
		sort(p+1,p+1+n);
		for(re int i=1; i<=n; i++)
		{
			if(d<=p[i].s)
			{ ans++,d = max(d,p[i].a); }
		}
		printf("%d\n",ans);
		return 0;
	};
};
signed main()
{ return OMA::main(); }
原文
存档评论2 条抓取于