热身赛 I - Ice-cream Knapsack Gym - 101991I

题目

There is a wonderful ice-cream shop that contains N ice-creams, such that each ice-cream is represented by two numbers Ci and Hi denoting the number of calories and the happiness value, respectively.
You want to buy exactly K ice-creams such that the calories of the densest ice-cream (the one with most calories) are as minimal as possible. If there is more than one way to do that, you want to maximize the total happiness of the ice-creams you will buy, that is the sum of the happiness values of the chosen ice-creams.
Input
The first line of the input contains a single integer T specifying the number of test cases.
Each test case begins with a line containing two integers N and K (1≤K≤N≤105), in which N is the number of ice-creams in the shop, and K is the number of ice-creams you want to buy.
Then a line follows containing N integers C1,⋯,CN (0≤Ci≤109), in which Ci is the number of calories in the ith ice-cream. Then a line follows containing N integers H1,⋯,HN (0≤Hi≤109), in which Hi is the happiness value of the ith ice-cream.
Output
For each test case, print a single line containing two space-separated integers representing the calories of the densest ice-cream you will buy and the total happiness of the ice-creams you will buy, respectively.
Remember that your goal is to buy K ice-creams such that the calories of the densest ice-cream (the one with most calories) are as minimal as possible. If there is more than one way to do that, you want to maximize the total happiness of the ice-creams you will buy.
Example
Input
1
5 3
1 2 3 4 5
5 4 3 2 1
Output
3 12

解题思路

首先选出最小的最大值,然后再这个卡路里值之下的所有冰淇淋中选择幸福度最高的,可以利用优先队列,排序等多种方法解决,不能直接 从卡路里最小值开始选择

AC代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int t;
struct node{
	int c,d;
	friend bool operator <( const node & a, const node & b)//重载运算符
	{
		
		return a.d<b.d;
	}
	};
node ice[maxn];
int n,k;
priority_queue<node> que;
bool cmp(node a,node b){
	return a.c<b.c;
}
int main(){
	freopen("icecream.in","r",stdin);
	cin>>t;
	while(t--){
		cin>>n>>k;
		for(int i=1;i<=n;i++)
			scanf("%d",&ice[i].c);
		for(int j=1;j<=n;j++)
			scanf("%d",&ice[j].d);
		sort(ice+1,ice+n+1,cmp);
		int temp=ice[k].c;
		int j=1;
		while(ice[j].c<=temp){
			
			que.push(ice[j]);
			j++;
		}
		long long hi=0;
		node te;
		for(int i=1;i<=k;i++){
			te=que.top();
			que.pop();
			hi+=te.d;
		}
		cout<<temp<<' '<<hi<<endl;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值