# 宁波工程学院 OJ [1282] A Bouquet of Flowers 最大的k个数的和

32 篇文章 0 订阅
• 问题描述
• 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 3
3 3 2 1
3 6 5 4
2 4
3 4 2 1
3 3 2 1

• 样例输出
• 15
11

• 提示
• 无
• 来源
• Monkeyde17

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;
}

#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;
}

• 1
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
03-02 1980
05-10 315
05-14 1321
02-23 1196
04-14 1513
11-25 5288
12-06 7262
12-05 3560

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。