C1475D Cleaning the Phone 题解

2024-02-23 05:28
文章标签 题解 phone cleaning c1475d

本文主要是介绍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 1t104 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109 1 ≤ b i ≤ 2 1 \leq b_i \leq 2 1bi2

保证 ∑ n ≤ 2 ⋅ 1 0 5 \sum n \leq 2 \cdot 10^5 n2105

题目描述

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 1t104 ) — 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 1n2105 , 1 ≤ m ≤ 1 0 9 1 \le m \le 10^9 1m109 ) — 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 1ai109 ) — 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 1bi2 ) — 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 2105 .

输出格式

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 1m109

动态规划肯定超时了,就往贪心方面去想。

又注意到 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 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/737634

相关文章

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

cell phone teardown 手机拆卸

tweezer 镊子 screwdriver 螺丝刀 opening tool 开口工具 repair 修理 battery 电池 rear panel 后盖 front and rear cameras 前后摄像头 volume button board 音量键线路板 headphone jack 耳机孔 a cracked screen 破裂屏 otherwise non-functiona

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

MiniCPM-V: A GPT-4V Level MLLM on Your Phone

MiniCPM-V: A GPT-4V Level MLLM on Your Phone 研究背景和动机 现有的MLLM通常需要大量的参数和计算资源,限制了其在实际应用中的范围。大部分MLLM需要部署在高性能云服务器上,这种高成本和高能耗的特点,阻碍了其在移动设备、离线和隐私保护场景中的应用。 文章主要贡献: 提出了MiniCPM-V系列模型,能在移动端设备上部署的MLLM。 性能优越:

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

【C++题解】1272. 郭远摘苹果

欢迎关注本专栏《C++从零基础到信奥赛入门级(CSP-J)》 问题:1272. 郭远摘苹果 类型:二维数组 题目描述: 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?