跳到正文

个人记录

U528399题解

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

文章信息

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

版本

  1. v2当前
  2. v1

贪心!!!

首先先看如何判无解,很显然,如果所有早餐店能吃的次数严格小于small_dog上学的天数,显然无解。

接下来先提出一个trick。每家早餐店都会有次数,而到达距离是随着到达次数递增,可以将一家有x次数早餐店拆成x家次数为1的早餐店,到店距离也随次数增加,这样就拆成了全部都是次数为1的早餐店,方便我们接下来的操作。

接下来考虑贪。

显然为了能够到达早餐店,且需要最小距离。我们要保证在上学的天数内瞬移距离能够到达早餐店。那就完了嘛,将排序完的数组按照到家距离排序,取排序后第h家早餐店到家的距离,那不就做完了。

码。

C++
#include<bits/stdc++.h>
const int N=1e5+5,M=1e6+5,mod=998277353;
#define int long long
using namespace std;
int n,m,k;
struct node{
	int height,wide;
}s[N],l[M];
bool cmp(node x,node y){
	return x.wide<y.wide;
}
signed main(){
	scanf("%lld%lld%lld",&n,&m,&k);
	int op=0;
	for(int i=1;i<=n;i++){
		scanf("%lld",&s[i].height);
		op+=s[i].height;
	}
	for(int i=1;i<=n;i++){
		int u;
		scanf("%lld",&u);
		s[i].wide=k-u;
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		//cout<<s[i].height<<"HHH"<<endl;
		if(s[i].height==1) {
			l[++cnt].height=s[i].height,l[cnt].wide=s[i].wide;
			//cout<<cnt<<"EEEEE"<<endl;
		}
		else{
			for(int j=1;j<=s[i].height;j++){
				l[++cnt].height=1;
				l[cnt].wide=s[i].wide+j-1;
				//cout<<cnt<<"DDDDD"<<endl;
			}
		}
	}
	sort(l+1,l+1+cnt,cmp);
	//cout<<cnt<<endl;
	int ans;
	//cout<<op<<endl;
	if(op<m){
		printf("-1");
		return 0;
	}else{
		for(int i=1;i<=cnt;i++){
			ans=l[m].wide;
		}
	}
	printf("%lld",ans%mod);
	return 0;
}
原文