codeforces 1374E1(贪心+优先队列)
题意描述
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but… Their mom doesn’t think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: ti — the amount of time Alice and Bob need to spend to read it, ai (equals 1 if Alice likes the i-th book and 0 if not), and bi (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it’s shared between them) and they read all books together, so the total reading time is the sum of ti over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
两个小孩都要读k本书,求读k本书花费的最小时间或者不能读k本书。
思路
要想读书的时间最小,我们要尽可能多的从两个小孩都可以读的书的种类中选。 假设 a a a为第一个小孩可以读的书的种类, b b b为第二个小孩可以读的书的种类, b o t h both both为两个小孩都可以读的种类,那么容易想到,如果 b o t h < = a + b both<=a+b both<=a+b的话,我们就选择 b o t h both both,否则选择 a + b a+b a+b。如果 a a a或者 b b b的种类不够的话,我们就只能选择 b o t h both both堆里的书。最后再从 a a a和 b b b堆中选择剩下的k本。如果最后k不为0的话,则说明无法选择。 对于上述的过程,我们可以使用优先队列来进行优化
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=3*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
bool used[N];
void solve(){
int n,k;cin>>n>>k;
priority_queue<int,vector<int>,greater<int>> pq1,pq2,pq3;
rep(i,0,n){
int x,y,z;cin>>x>>y>>z;
if(y && z) pq1.push(x);
else if(y) pq2.push(x);
else if(z) pq3.push(x);
}
ll ans=0;
while(pq1.size() && k){
int t=pq1.top();
if(pq2.size() && pq3.size()){
int t1=pq2.top(),t2=pq3.top();
if(t<=(t1+t2)){
ans+=t;
pq1.pop();
}else{
ans+=t1+t2;
pq2.pop();pq3.pop();
}
}else{
ans+=t;
pq1.pop();
}
k--;
}
while(pq2.size() && pq3.size() && k){
ans+=pq2.top()+pq3.top();
k--;
pq2.pop();pq3.pop();
}
if(k) cout<<-1<<endl;
else cout<<ans<<endl;
}
int main(){
IOS;
//int t;scanf("%d",&t);
//while(t--){
solve();
//}
return 0;
}
- Javascript快速入门(上篇)
- SQL练习之不反复执行相同的计算
- SQL练习之求解填字游戏
- 快速入门系列--WCF--08扩展与新特性
- SQL练习之两个列值的交换
- Parcel,零配置开发 React 应用!
- 像 React Native 开发 APP 一样,用wn-cli 开发 weapp (微信小程序)
- 正则表达式快速入门
- JavaScript之<script>标签简介
- 数控机床数控系统选择4大关键要素
- Python快速入门
- Jquer学习之jQuery(function(){})与(function(){})(jQuery)之间的区别
- 服务端常见性能隐患分享
- ExtJs学习笔记(19)_复杂Form示例
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 解决pyqt5异常退出无提示信息的问题
- TensorFlow2.1.0安装过程中setuptools、wrapt等相关错误指南
- 来来来,让咱重新认识一下算法的复杂度!
- 带你通过字节跳动面试---操作系统复习
- Java 8 中的接口和抽象类到底有啥区别?
- Android自定义View实现简单炫酷的球体进度球实例代码
- 详解android 中文字体向上偏移解决方案
- Android自定义控件仿ios下拉回弹效果
- 当飞桨PaddleHub遇到微信小程序,AI也能指物作诗
- Android仿QQ好友详情页下拉顶部图片缩放效果
- 数组对象转为Map
- TensorFlow2.X使用图片制作简单的数据集训练模型
- Android高性能日志写入方案的实现
- MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列)
- 想后台运行没想到导致磁盘满了