【Codeforces 1443 C】The Delivery Dilemma,贪心,排序

举报
小哈里 发表于 2022/05/10 23:57:04 2022/05/10
【摘要】 problem C. The Delivery Dilemma time limit per test2 seconds memory limit per test256 megabytes input...

problem

C. The Delivery Dilemma
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn’t like to cook, he wants to order these dishes in restaurants.

Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:

the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in ai minutes,
Petya goes to the restaurant i on his own and picks up the dish, he will spend bi minutes on this.
Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.

For example, if Petya wants to order n=4 dishes and a=[3,7,4,5], and b=[2,1,2,4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 3 minutes, the courier of the fourth restaurant will bring the order in 5 minutes, and Petya will pick up the remaining dishes in 1+2=3 minutes. Thus, in 5 minutes all the dishes will be at Petya’s house.

Find the minimum time after which all the dishes can be at Petya’s home.

Input
The first line contains one positive integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.

Each test case begins with a line containing one integer n (1≤n≤2⋅105) — the number of dishes that Petya wants to order.

The second line of each test case contains n integers a1…an (1≤ai≤109) — the time of courier delivery of the dish with the number i.

The third line of each test case contains n integers b1…bn (1≤bi≤109) — the time during which Petya will pick up the dish with the number i.

The sum of n over all test cases does not exceed 2⋅105.

Output
For each test case output one integer — the minimum time after which all dishes can be at Petya’s home.

Example
inputCopy
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2
outputCopy
5
3
2
3

solution

/*
题意:
+ n(2e5)个菜肴,外卖为ai,自取为bi。总用时为max(max(ai),sum(bi)),求最少用时
思路:
+ 如果我们的某个外卖时间x,那么所有y<x都外卖,因为不影响结果。
+ 所以对ai降序排序,对bi求前缀和。当自取和大于外卖时,剩下的都点外卖,之前的自取和外卖选更大值。
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+10;
LL a[maxn], b[maxn];
int main(){
	ios::sync_with_stdio(false);
	int T;  cin>>T;
	while(T--){
		int n;  cin>>n;
		vector<pair<LL,LL> >v;
		for(int i = 0; i < n; i++)cin>>a[i];
		for(int i = 0; i < n; i++){
			cin>>b[i];  v.push_back({a[i],b[i]});
		}
		sort(v.rbegin(),v.rend());
		LL sum = 0, ans, ok = 0;
		for(int i = 0; i < n; i++){
			sum += v[i].second;
			if(sum>=v[i].first){
				ok = 1;
				ans = max(v[i].first,sum-v[i].second);
				break;
			}
		}
		if(ok==1)cout<<ans<<"\n";
		else cout<<sum<<"\n";
	}
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/113478967

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。