Codeforces Round #653 (Div. 3) E1. Reading Books (easy version)(排序)

题目链接:https://codeforces.com/contest/1374/problem/E1

思路:

     可将书本分成三类:只有Alice喜欢,只有Bob喜欢,二人都喜欢(都不喜欢的书直接忽略不计),可先分别将前两类的书本按阅读时间从小到大排序,然后时间对应相加,合并到第三类。最后将最终结果排序,计算前k项和。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#define maxn 200010
#define mod 1000000007
#define ll long long
using namespace std;

int n, k;
int t[maxn];
int a[maxn];
int b[maxn];
int ta[maxn], tb[maxn], ts[maxn];
int na = 0, nb = 0, ns = 0;
int main() {
	int i, j;
	cin >> n >> k;
	for (i = 1; i <= n; i++) {
		scanf("%d%d%d", &t[i], &a[i], &b[i]);
		if (a[i] == 1 && b[i] == 0) {
			ta[++na] = t[i];
		}
		if (a[i] == 0 && b[i] == 1) {
			tb[++nb] = t[i];
		}
		if (a[i] && b[i]) {
			ts[++ns] = t[i];
		}
	}
	sort(ta + 1, ta + 1 + na);
	sort(tb + 1, tb + 1 + nb);
	for (i = 1; i <= min(na, nb); i++) {
		ts[++ns] = ta[i] + tb[i];
	}
	sort(ts + 1, ts + 1 + ns);
	if (ns < k) {
		cout << -1;
		return 0;
	}
	int sum = 0;
	for (i = 1; i <= k; i++)sum += ts[i];
	cout << sum;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值