您的位置:首页 > 移动开发

CSU 1098 Happy watering

2016-01-23 18:49 337 查看

1098: Happy watering

Time Limit: 1 Sec Memory Limit: 16 MB

Submit: 132 Solved: 60

[Submit][Status][Web
Board]

Description

GBQC国的小明家里有N棵树,每天小明都会给其中一棵树浇水,每次浇水后,树都会长高一些,但由于树的品种不同,每次增长的高度也有所区别。

为了使这N棵树看起来整洁、美观,小明希望最高的树和最低的树的高度差越小越好。现在小明想知道,如果至多浇K次水,最高的树和最低的树的高度差最小为多少?

Input

输入包含多组测试数据。

对于每组测试数据,第一行包含两个正整数N(2<=N<=10^5)、K(1<=K<=10^5),含义同上。接下来一共有N行,每行有两个正整数h(1<=h<=10^3)、d(1<=d<=10^3),分别描述了这N棵树的初始高度,以及每次浇水后这棵树增长的高度。

Output

对于每组测试数据,用一行输出一个整数表示如果小明至多浇K次水,最高的树和最低的树的高度差最小为多少。

Sample Input

2 17 210 32 47 210 32 37 810 9

Sample Output

103

HINT

由于数据量较大,推荐使用scanf和printf。

Source

CSU Monthly 2012 Aug.

贪心水题。用优先队列省事。

#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=0x3f3f3f;
typedef struct Node
{
int height,grow;
bool friend operator <(Node a,Node b)
{
return a.height > b.height;
}
}Node;
priority_queue<Node> q;
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)>0)
{
while(!q.empty())
q.pop();
int maxHeight=0;
Node temp;
for(int i=0;i<n;i++)
{
scanf("%d%d",&temp.height,&temp.grow);
q.push(temp);
maxHeight=max(maxHeight,temp.height);
}
int minDistance=MAXN;
k++;
while(k--&&minDistance)
{
temp=q.top();
q.pop();
int dis=maxHeight-temp.height;
minDistance=min(minDistance,dis);
temp.height+=temp.grow;
maxHeight=max(maxHeight,temp.height);
q.push(temp);
}
printf("%d\n",minDistance);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: