宁波工程学院 OJ [1282] A Bouquet of Flowers 最大的k个数的和
来源:互联网 发布:小学课程同步软件 编辑:程序博客网 时间:2024/04/30 00:21
- 问题描述
- Mr.Cai want to send TT a bouquet of the most beautiful flowers.
Mr.Cai have buy K bouquets of flowers from the shop.
When Mr.Cai get the bouquet of flowers that will sorted in descending order by their beautiful value.
And Mr.Cai want pick up H flowers from the K bouquets to make up the most beautiful bouquet flowers. - 输入
- There are muti-case.
First line contain two integers K (0 < K <= 1000) and H (0 < H <= 1000)
Then there are K lines.
For each lines, the first number Ni (0 < Ni <= 10000) means the bouquet of flowers contains Ni flowers.
the following Ni numbers means the flower's beautiful value Vi (0 < Vi <= 100000).
The H is less than the sum of all Ni. - 输出
- For each case, print the most beautiful flowers's beatiful value.
- 样例输入
2 33 3 2 13 6 5 42 43 4 2 13 3 2 1
- 样例输出
1511
- 提示
无
- 来源
Monkeyde17
题意:输入 h k
表示h行 每行 第一个数m 其后跟m个数字
问这些数字中取k个 问和最大为多少?
思路:
由于 只有444k的内存 所以要用到 优先队列 保持k个数
但是 我的却一直超时 看了别人的解题报告才知道 语句中多很多判断 比入队出队要耗的时间少很多 也就是 本题的主要耗时间的地方时入队出队
下面是AC 代码 最后面是TLE的
AC
#include<stdio.h>#include<stdlib.h>#include<queue>using namespace std;int main(){int h,i,j,n,k;while(scanf("%d %d",&k,&h)!=EOF){priority_queue<int,vector<int>,greater<int> >que;while(k--){scanf("%d",&n);int num;while(n--){scanf("%d",&num);if(que.size()<h)que.push(num);else if(que.size()>=h&&num>que.top()) {que.pop();que.push(num);}}}int sum=0;while(que.size()>h) que.pop();while(!que.empty()){sum+=que.top();que.pop();}printf("%d\n",sum);}return 0;}TLE
#include<stdio.h>#include<stdlib.h>#include<queue>using namespace std;int main(){int h,i,j,n,k;while(scanf("%d %d",&k,&h)!=EOF){priority_queue<int,vector<int>,greater<int> >que;while(!que.empty()){que.pop();}while(k--){scanf("%d",&n);int num;while(n--){scanf("%d",&num);que.push(num);if(que.size()>h) que.pop();}}int sum=0;while(que.size()>h) que.pop();while(!que.empty()){sum+=que.top();que.pop();}printf("%d\n",sum);}return 0;}
上面的 AC的982ms
下面处理了下 就200ms
#include<stdio.h>#include<stdlib.h>#include<queue>using namespace std;int getval(){ int ret(0); char c; while((c=getchar())==' '||c=='\n'||c=='\r'); ret=c-'0'; while((c=getchar())!=' '&&c!='\n'&&c!='\r') ret=ret*10+c-'0'; return ret;}int main(){int h,i,j,n,k;while(scanf("%d %d",&k,&h)!=EOF){priority_queue<int,vector<int>,greater<int> >que;while(k--){scanf("%d",&n);int num;while(n--){num=getval();if(que.size()<h)que.push(num);else if(que.size()>=h&&num>que.top()) {que.pop();que.push(num);}}}int sum=0;while(que.size()>h) que.pop();while(!que.empty()){sum+=que.top();que.pop();}printf("%d\n",sum);}return 0;}
- 宁波工程学院 OJ [1282] A Bouquet of Flowers 最大的k个数的和
- 宁波工程学院 OJ [1292] Jokes 一堆数中对每个数与前k个重复的数字的个数
- 宁波工程学院 [1369] A Breaking Computer deque的应用
- 宁波工程学院 OJ [1347] 老虎满坡找灰兔
- 懒惰的风纪委Elaine(宁波工程学院)
- 最大的k个数
- 最大的K个数
- 最大的k个数
- 求连续K个数的和最大
- 寻找最大的K个数
- 寻找最大的K个数
- 寻找最大的k个数
- 寻找最大的K个数
- 寻找最大的K个数
- 寻找最大的K个数
- 寻找最大的K个数
- 寻找最大的K个数
- 寻找最大的K个数
- 解决OleDbDataReader重新获取记录时,使用GetString()方法出错的问题
- boost安装以及连接VS2008的方法
- 快速读取的数字函数
- 贝叶斯学习的理论
- MFC的连续存储 永久保存(串行化)两篇
- 宁波工程学院 OJ [1282] A Bouquet of Flowers 最大的k个数的和
- JavaScript中的null和undefined的区别
- 【自绘ListBox之一】带Icon的ListBox控件
- 虚短和虚断的概念
- 软件定价:我们深谙其道吗?
- 《partner4java 讲述Spring入门》之第一步:Spring概述与Spring IoC
- poj 1384 Piggy-Bank
- 关于adaboost分类器的一点看法
- 请解释一下集成运放虚短,虚断,虚地这几个概念