【洛谷 P7412】 Year of the Cow S
题目
解题思路
先不考虑k,那么一定得回到最远的年份
接着我们想k次可以不等待,一定是去掉等的最久的k个段
首先将年份排序,才能知道相邻的两个段
然后处理两两之间的差
用最远的年份减去等的最久的k个段
代码
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int n,k,tot,ans,a[700010],c[700010],b[700010];
int main()
{
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=(a[i]+11)/12; //求出被包括在哪个12年
}
sort(a+1,a+n+1);
int i=1;
while (i<=n)
{
c[++tot]=a[i];
b[tot]=c[tot]-c[tot-1]-1; //求出两两之间的差,因为最后要回到今年,所以也看作一个段
while (a[i]==a[i+1]) i++;
i++;
}
sort(b+1,b+tot+1);
ans=c[tot];
for (int i=tot;i>tot-k+1;i--)
ans=ans-b[i];
printf("%d",ans*12);
return 0;
}