Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)

题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书.
题解:我们可以分\(3\)种情况来存,即:
1.\(a=b=1\). 2.\(a=1,b=0\). 3.\(a=0,b=1\).
对于2和3来说,我们可以将他们排序,然后合并到一起,最后放到第1种情况中再排一次序,取前\(k\)个前缀和即可.
代码:
int n,k;
int t,x,y;
int ans;
vector<int> a,b,c; int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;++i){
cin>>t>>x>>y;
if(x==1 && y==1){
a.pb(t);
}
if(x==1 && y==0){
b.pb(t);
}
if(x==0 &&y==1){
c.pb(t);
}
}
if(b.size()>c.size()) swap(b,c);
if(a.size()+b.size()<k) cout<<-1<<endl;
else{
sort(b.begin(),b.end());
sort(c.begin(),c.end());
for(int i=0;i<b.size();++i){
b[i]+=c[i];
}
sort(b.begin(),b.end());
for(int i=0;i<b.size();++i){
a.pb(b[i]);
}
sort(a.begin(),a.end());
for(int i=0;i<k;++i){
ans+=a[i];
}
cout<<ans<<endl;
}
return 0;
}
Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)的更多相关文章
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】
一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)
This problem is different from the hard version. In this version Ujan makes exactly one exchange. Yo ...
- Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version) (贪心)
题意:给你一组数,每次可以选队首或队尾的数放入栈中,栈中元素必须保持严格单增,问栈中最多能有多少元素,并输出选择情况. 题解:首先考虑队首和队尾元素不相等的情况,如果两个数都大于栈顶元素,那么我们选小 ...
- Codeforces Round #570 (Div. 3) G. Candy Box (hard version) (贪心,优先队列)
题意:你有\(n\)个礼物,礼物有自己的种类,你想将它们按种类打包送人,但是打包的礼物数量必须不同(数量,与种类无关),同时,有些礼物你想自己留着,\(0\)表示你不想送人,问你在送出的礼物数量最大的 ...
- Codeforces Round #672 (Div. 2) C1. Pokémon Army (easy version) (DP)
题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置, ...
- Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...
随机推荐
- 日常采坑:.NET Core SDK版本问题
1..NetCore SDK版本问题 .NetCore3.1 webapi 部署linux,遇到一个坑,开启的目录浏览功能失效,几番尝试发现是版本问题.本地sdk版本与linux安装的sdk版本不对应 ...
- Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because of access permission
Received empty response from Zabbix Agent at [agent]. Assuming that agent dropped connection because ...
- 【ORA】ORA-16629解决办法
数据库向保护模式报告不同的保护级别"警告消息. 首先查看主备库的保护模式和保护级别 select protection_mode,protection_level from v$databa ...
- java锁的对象引用
当访问共享的可变数据时,通常需要同步.一种避免使用同步的方式就是不共享数据. 如果数据仅在单线程内访问,就不需要同步,这种技术称为"线程封闭",它是实现线程安全性最简单方式之一. ...
- 记一道C语言编程题(C语言学习笔记)
题目如下 解答如下 #include <stdio.h> #include<math.h> double Mysqrt(double n) { return sqrt(n); ...
- ctfhub技能树—sql注入—报错注入
打开靶机 payload 1 Union select count(*),concat((查询语句),0x26,floor(rand(0)*2))x from information_schema.c ...
- bash shell数组使用总结
本文为原创博文,转发请注明原创链接:https://www.cnblogs.com/dingbj/p/10090583.html 数组的概念就不多说了,大家都懂! shell数组分为索引数组和关联数 ...
- oracle_fdw的安装和使用
1.下载instant oracle client 下载网址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html ...
- 使用smartform打印表单
昨天写了个smartform打印表单,在开发完成,在测试机测试OK,传到生产机,出现严重问题!无法打印,干脆就是无法调用打印图形界面,进入SMARTFORM事物,查看这个表单,发现,居然公司的LOGO ...
- 2020 CSP&NOIP 游记
CSP初赛 CSP初赛 Day -1 早上打了模拟赛,T2寒假正好做过,然而还是还是被踩Orz,郑外NB!.中午出校吃了大盘鸡和拉面,还带回来了三瓶可乐. 初赛知识点看了两页不(看)想(不)看(懂)了 ...