璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:usaco21feb_year_of_the_cow_s
problems
名称Year of the Cow S
题目编号USACO21FEB_S3
题目链接luogu.com.cn/…
来源USACO
算法分类贪心, 排序
难易程度容易

Year of the Cow S

想法

共识:

  1. 某个祖先在第x年,则必须经过 $[x-x\%12+12, x-x\%12]$ 这12年。
  2. 无论安排什么穿梭方案,最远的年份是一定要穿梭的。

所以第一次穿梭,穿到最远的年份。接下来选择最大的k-1个空闲12年的间隙作为穿梭掉的时间,剩下的则为必须经过的年份。由于选择了最大的空闲间隙,所以余下的时间一定是最少的。

代码实现

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
int main()
{
	int n, k;
	cin >> n >> k;
	vector<int>line(n), line12, c;
	for(int& i : line)cin >> i;
	sort(line.begin(), line.end());
	line12.push_back(0);
	for(int i : line)
		line12.push_back(i - i % 12 + 12);
	for(int i=1; i<=n; i++)
		if(line12[i] != line12[i-1])
			c.push_back(line12[i]-12 - line12[i-1]);
	sort(c.begin(), c.end());
	long long int ans = 0;
	for(int i=c.size()-1; i > c.size()-k && i>=0; i--)
		ans += c[i];
	cout << line12[n] - ans;
	return 0;
}
/app/www/public/data/pages/icpc/problems/usaco21feb_year_of_the_cow_s.txt · 最后更改: 2023/04/01 13:55 由 温婕莺