Codeforces Round #681------The Delivery Dilemma

题目

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.

题意:

有n道菜,每道菜有两种配送方式
1、选择外卖,花费a时间
2、选择自取,花费b时间
请你选择一种送菜方式,使得最后花费时间最少
注意:
选择所有菜的配送方式后
1、对于外卖,所有的外卖同时配送(即所有配送时间取最大值)
2、而自己拿的是一个个的拿(即所有拿的时间累加)
注意配送和自己拿不冲突(即取最后两者的最大值 )

题解

贪心,按菜品外卖配送花费时间从小到大排序
每个菜品,这个菜品及它之前的选择外卖,之后的选择自取
扫一遍,取最小值就结果
此处对每道菜预处理后缀和

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 2e5 + 15;
struct node {
	ll a, b;
}no[N];
bool cmp(node x, node y) {
	if (x.a != y.a)
		return x.a < y.a;
	else
		return x.b < y.b;
}
ll sum[N];
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		for (int i = 1; i <= n; i++)
			cin >> no[i].a;
		for (int i = 1; i <= n; i++)
			cin >> no[i].b;
		sort(no + 1, no  + 1 + n, cmp);
		sum[n + 1] = 0;
		for (int i = n; i >= 1; i--)
			sum[i] = sum[i + 1] + no[i].b;
		ll res = sum[1];
		for (int i = 1; i <= n; i++)
			res = min(res, max(no[i].a, sum[i + 1]));
		cout << res << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值