思路:采用贪心的策略,每次都选择最矮的书浇水,高度差为0时或k次浇水后结束,每次更新最短距离
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
struct note
{
int h,d;
bool operator < (const struct note &tmp)const
{
return h>tmp.h;
}
};
int main()
{
//freopen("in.txt","r",stdin);
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
note tmp;
int tmpmax=0;
int ans = 0x3f3f3f3f;
priority_queue<note> q;
for(int i=0;i<n;i++)
{
scanf("%d%d",&tmp.h,&tmp.d);
tmpmax = max(tmp.h,tmpmax);
q.push(tmp);
}
k++;
while(k--)
{
tmp = q.top();
q.pop();
ans = min(tmpmax-tmp.h,ans);
if(ans==0)
break;
tmp.h+=tmp.d;
tmpmax = max(tmpmax,tmp.h);
q.push(tmp);
}
printf("%d\n",ans);
}
}