Codeforces 416C Booking System(贪心)

题目链接:Codeforces 416C Booking System


题目大意:餐馆有n批客人,每批客人给出人数和消费金额。然后餐馆有m个桌子,给出每个桌子可以坐的人数。问说在这一时刻,餐馆可以最大盈利多少,并且接待尽量多的客人。


解题思路:将客人按照消费金额、人数排序,然后桌子按照从小到大排序。消费高的客人尽量找最小满足的桌子给他们。注意输出的时候客人有序号,桌子同样有序号,不能打乱。


#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int N = 1005;
struct state {
	int id, val, sum;
	state () {
	}
}g[N], t[N];
int n, m, v[N];

bool cmpS (const state& a, const state& b) {
	if (a.val != b.val) return a.val > b.val;
	return a.sum > b.sum;
}

bool cmpT (const state& a, const state& b) {
	return a.sum < b.sum;
}

void init () {
	memset(v, 0, sizeof(v));

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &g[i].sum, &g[i].val);
		g[i].id = i;
	}

	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%d", &t[i].sum);
		t[i].id = i;
	}

	sort(g, g + n, cmpS);
	sort(t, t + m, cmpT);
}

void solve () {
	int ans = 0, cnt = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (g[i].sum <= t[j].sum) {
				cnt++;
				ans += g[i].val;
				v[g[i].id] = t[j].id + 1;
				t[j].sum = 0;;
				break;
			}
		}
	}
	printf("%d %d\n", cnt, ans);
	for (int i = 0; i < n; i++) {
		if (v[i]) printf("%d %d\n",	i + 1, v[i]);
	}
}

int main () {
	init ();
	solve ();
	return 0;
}


你可能感兴趣的:(Codeforces 416C Booking System(贪心))