题意描述
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;
}