本文主要是介绍C1475D Cleaning the Phone 题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- C1475D Cleaning the Phone 题解
- Cleaning the Phone
- 题面翻译
- 题目描述
- 输入格式
- 输出格式
- 样例 #1
- 样例输入 #1
- 样例输出 #1
- 提示
- 算法:贪心
- 代码:
C1475D Cleaning the Phone 题解
link
Cleaning the Phone
题面翻译
题目大意:
有 n n n 个物品和一个最低价值 m m m ,每一个物品有一个价值 a i a_i ai 和 体积 b i b_i bi 体积只有可能为 1 1 1 或 2 2 2 。
你需要选出几个物品,使得它们的价值和大于等于 m m m 且使体积最小。
T T T组询问。
数据范围:
1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104, 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1≤n≤2⋅105 , 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109, 1 ≤ b i ≤ 2 1 \leq b_i \leq 2 1≤bi≤2。
保证 ∑ n ≤ 2 ⋅ 1 0 5 \sum n \leq 2 \cdot 10^5 ∑n≤2⋅105。
题目描述
Polycarp often uses his smartphone. He has already installed n n n applications on it. Application with number i i i takes up a i a_i ai units of memory.
Polycarp wants to free at least m m m units of memory (by removing some applications).
Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer b i b_i bi to each application:
- b i = 1 b_i = 1 bi=1 — regular application;
- b i = 2 b_i = 2 bi=2 — important application.
According to this rating system, his phone has b 1 + b 2 + … + b n b_1 + b_2 + \ldots + b_n b1+b2+…+bn convenience points.
Polycarp believes that if he removes applications with numbers i 1 , i 2 , … , i k i_1, i_2, \ldots, i_k i1,i2,…,ik , then he will free a i 1 + a i 2 + … + a i k a_{i_1} + a_{i_2} + \ldots + a_{i_k} ai1+ai2+…+aik units of memory and lose b i 1 + b i 2 + … + b i k b_{i_1} + b_{i_2} + \ldots + b_{i_k} bi1+bi2+…+bik convenience points.
For example, if n = 5 n=5 n=5 , m = 7 m=7 m=7 , a = [ 5 , 3 , 2 , 1 , 4 ] a=[5, 3, 2, 1, 4] a=[5,3,2,1,4] , b = [ 2 , 1 , 1 , 2 , 1 ] b=[2, 1, 1, 2, 1] b=[2,1,1,2,1] , then Polycarp can uninstall the following application sets (not all options are listed below):
- applications with numbers 1 , 4 1, 4 1,4 and 5 5 5 . In this case, it will free a 1 + a 4 + a 5 = 10 a_1+a_4+a_5=10 a1+a4+a5=10 units of memory and lose b 1 + b 4 + b 5 = 5 b_1+b_4+b_5=5 b1+b4+b5=5 convenience points;
- applications with numbers 1 1 1 and 3 3 3 . In this case, it will free a 1 + a 3 = 7 a_1+a_3=7 a1+a3=7 units of memory and lose b 1 + b 3 = 3 b_1+b_3=3 b1+b3=3 convenience points.
- applications with numbers 2 2 2 and 5 5 5 . In this case, it will free a 2 + a 5 = 7 a_2+a_5=7 a2+a5=7 memory units and lose b 2 + b 5 = 2 b_2+b_5=2 b2+b5=2 convenience points.
Help Polycarp, choose a set of applications, such that if removing them will free at least m m m units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.
输入格式
The first line contains one integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104 ) — the number of test cases. Then t t t test cases follow.
The first line of each test case contains two integers n n n and m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅105 , 1 ≤ m ≤ 1 0 9 1 \le m \le 10^9 1≤m≤109 ) — the number of applications on Polycarp’s phone and the number of memory units to be freed.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1≤ai≤109 ) — the number of memory units used by applications.
The third line of each test case contains n n n integers b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,…,bn ( 1 ≤ b i ≤ 2 1 \le b_i \le 2 1≤bi≤2 ) — the convenience points of each application.
It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105 .
输出格式
For each test case, output on a separate line:
- -1, if there is no set of applications, removing which will free at least m m m units of memory;
- the minimum number of convenience points that Polycarp will lose if such a set exists.
样例 #1
样例输入 #1
5
5 7
5 3 2 1 4
2 1 1 2 1
1 3
2
1
5 10
2 3 2 3 2
1 2 1 2 1
4 10
5 1 3 4
1 2 1 2
4 5
3 2 1 2
2 1 2 1
样例输出 #1
2
-1
6
4
3
提示
In the first test case, it is optimal to remove applications with numbers 2 2 2 and 5 5 5 , freeing 7 7 7 units of memory. b 2 + b 5 = 2 b_2+b_5=2 b2+b5=2 .
In the second test case, by removing the only application, Polycarp will be able to clear only 2 2 2 of memory units out of the 3 3 3 needed.
In the third test case, it is optimal to remove applications with numbers 1 1 1 , 2 2 2 , 3 3 3 and 4 4 4 , freeing 10 10 10 units of memory. b 1 + b 2 + b 3 + b 4 = 6 b_1+b_2+b_3+b_4=6 b1+b2+b3+b4=6 .
In the fourth test case, it is optimal to remove applications with numbers 1 1 1 , 3 3 3 and 4 4 4 , freeing 12 12 12 units of memory. b 1 + b 3 + b 4 = 4 b_1+b_3+b_4=4 b1+b3+b4=4 .
In the fifth test case, it is optimal to remove applications with numbers 1 1 1 and 2 2 2 , freeing 5 5 5 units of memory. b 1 + b 2 = 3 b_1+b_2=3 b1+b2=3 .
算法:贪心
注意: 1 ≤ m ≤ 1 0 9 1≤m≤10^9 1≤m≤109 。
动态规划肯定超时了,就往贪心方面去想。
又注意到 b b b 的取值只能为 1 1 1 或 2 2 2,比较特殊,就从中入手。
当总价值 < m <m <m 时,肯定不成立,直接输出。
接下来贪心:
因为 1 + 1 = 2 1+1=2 1+1=2,所以我们可以通过两个体积为 1 1 1 的物品与一个体积为 2 2 2 的物品(总体积一样)比较价值大小来取。
因此输入时应该根据体积来分别保存到两个数组中,并且排序(从大到小)。之后就可以按照以上步骤来贪心啦~
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=2e5+10;
ll T,n,m,a[N],b,t1,t2,q1[N],q2[N],ct,ans,h1,h2;
bool cmp(ll l,ll r){return l>r;
}
int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin>>T;while(T--){cin>>n>>m;for(int i=1;i<=n;i++) cin>>a[i];t1=t2=ct=ans=0,h1=h2=1;for(int i=1;i<=n;i++){cin>>b;if(b==1) q1[++t1]=a[i];else q2[++t2]=a[i];}sort(q1+1,q1+t1+1,cmp);sort(q2+1,q2+t2+1,cmp);while(ct<m){//贪心取最大值if(!q1[h1]&&!q2[h2]){ans=-1;break;}//q1和q2都取完了,总价值还没达到mif(ct+q1[h1]>=m){ans++;break;}//取q1[h1]后达到if(q1[h1]+q1[h1+1]>=q2[h2]) ct+=q1[h1++],ans++;//取q1else ct+=q2[h2++],ans+=2;//取q2}cout<<ans<<"\n";for(int i=1;i<=n;i++) q1[i]=q2[i]=0;}return 0;
}
感谢大家的支持~
这篇关于C1475D Cleaning the Phone 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!