0分求助(只为想知道这种思路对不对)
查看原帖
0分求助(只为想知道这种思路对不对)
712506
Lucky_Cloud楼主2022/10/21 15:01
#include<iostream>
#include<algorithm>
using namespace std;
struct star{
	int d,m;
}s[300010];
bool cmp(star a,star b){
	if(a.d==b.d) return a.m<a.m;
	return a.d<b.d;
}
int main(){
	int tot=1,n,k,ans=0,nowk;
	cin>>n>>k;
	for(int i=1;i<=n;i++) cin>>s[i].d>>s[i].m;
	sort(s+1,s+1+n,cmp);
	int maxn=s[n].d;
	nowk=k;
	for(int i=1;i<=maxn+1;i++){
		while(s[tot].d+1!=i&&s[tot].d!=i&&tot<=n) tot++;//把过期的抛出去
		if(tot==n+1)break;//奇妙的判断条件
		if(s[tot].m>nowk){
			ans+=nowk;
			s[tot].m-=nowk;
			nowk=k;
			continue;
		}
		while(s[tot].m<=nowk&&(s[tot].d==i||s[tot].d+1==i)){
			nowk-=s[tot].m;
			ans+=s[tot].m;
			tot++;
		}
		nowk=k;
	}
	cout<<ans<<endl;
	return 0;
}

可以吗?

2022/10/21 15:01
加载中...