CSU 1098 Happy watering
2016-01-23 18:49
337 查看
1098: Happy watering
Time Limit: 1 Sec Memory Limit: 16 MBSubmit: 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; }
相关文章推荐
- Android&Java的成长之路之二(服务端最基本的)
- POJ 3321 Apple Tree
- [iOS]URL编码和解码
- 高通android开发摘要
- 高通android开发摘要
- Android ActionBar的图标和标题
- iOS swift 字符属性NSAttributedString描述
- Android SDK 在线更新镜像服务器资源
- iOS 模拟器不弹出软键盘的解决办法
- Ubuntu14.04搭建android开发环境
- 【我所认知的BIOS】系列blog整理 1.23.2016.zip
- 报 Unable to resolve target 'android-19' 问题
- 【cocos2d-x制作别踩白块儿】第一期:游戏介绍
- 50个Android开发技巧(11 为文字加入特效)
- Core Animation1-简介
- Android 6.0 请求拨打电话error
- appcan更新
- Android API Level与sdk版本对照表
- iOS block在不同界面的回调传值
- Objective-c开发教程--MRC和ARC混编