跳到正文

题解

题解:AT_abc415_d [ABC415D] Get Many Stickers

anke2017约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月6日
篇幅
约 2 分钟

版本

  1. v1当前

先考虑 nn 很大的情况。这时,显然我们会选择让自己的可乐瓶(在这里,我们认为未喝的可乐其实也是一个瓶子)减少得最少的方案,直到我们无法这样选择为止。

这样的选择正确性几乎是显然的。(因为消耗更多瓶子的方案选择后瓶子更少,接下来它能做的操作显然在瓶子多的方案可做的操作的集合里,而我们只想最大化操作次数)

这样,要干什么已经很容易知道了:每次都选瓶子消耗最少的方案,直到我们无法这样选择(瓶子不够)为止。一个方案可以用公式算出到底要选择几次。

C++
#include<bits/stdc++.h>

using namespace std;
#define int long long
struct node
{
	int dlt,lst;//dlt:消耗瓶子数,lst:剩余多少时不能换
	bool operator <(const node tmp)const
	{
		return dlt>tmp.dlt;
	}
}sth[1145141];
signed main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	long long n,m;cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		int a,b;cin>>a>>b;
		sth[i]={a-b,a};
	}
	sort(sth+1,sth+1+m);
	long long ans=0;
//下面是个人写法问题,用单调栈选出决策集合,其实可以不用
	stack<node> st;
	for(int i=1;i<=m;i++)
	{
		while(st.size()&&st.top().lst>=sth[i].lst)st.pop();
		st.push(sth[i]);
	}
	while(st.size())
	{
		if(st.top().lst>n){st.pop();continue;}
		ans+=(n-st.top().lst)/st.top().dlt+1;
		n-=((n-st.top().lst)/st.top().dlt+1)*st.top().dlt;
		st.pop();
	}
	cout<<ans;
	return 0;
}
原文