「CF161B」Discounts

传送门
Luogu

解题思路

贪心地想一想,我们肯定要让凳子去给价格越高的商品打半价,那么我们就先按照价格排序,但是要优先把凳子排在前面。
然后我们发现一条凳子肯定只能给价格小于等于它本身的物品打半价,所以我们就尽量把所有凳子单独放一个购物车,但是要注意判断一下凳子数量和购物车数量的关系,这里不啰嗦了。

细节注意事项

  • 分类讨论的过程不能少,但要是想做到码量少就要细心一点

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
	s = 0; int f = 0; char c = getchar();
	while (!isdigit(c)) f |= c == '-', c = getchar();
	while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
	s = f ? -s : s;
}

const int _ = 1010;

int n, k; struct node { int c, p, id; }t[_];
inline bool cmp(const node& x, const node& y)
{ return x.p == y.p ? x.c > y.c : x.p < y.p; }
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	read(n), read(k);
	int cnt = 0; 
	for (rg int i = 1; i <= n; ++i) {
		read(t[i].c), read(t[i].p);
		t[i].id = i, cnt += t[i].p == 1;
	}
	sort(t + 1, t + n + 1, cmp);
	double ans = 0.0;
	for (rg int i = 1; i <= min(cnt, k - 1); ++i)
		ans += t[i].c * 0.5;
	for (rg int i = min(cnt, k - 1) + 1; i <= n; ++i)
		ans += t[i].c * 1.0;
	if (cnt > k - 1) {
		int mn = 2147483647;
		for (rg int i = k; i <= n; ++i)
			mn = min(mn, t[i].c);
		ans -= mn * 0.5;
	}
	printf("%.1lf
", ans);
	for (rg int i = 1; i <= k - 1; ++i)
		printf("1 %d
", t[i].id);
	printf("%d ", n - k + 1);
	for (rg int i = k; i <= n; ++i)
		printf("%d%c", t[i].id, " 
"[i == n]);
	return 0;
}

完结撒花 (qwq)

原文地址:https://www.cnblogs.com/zsbzsb/p/11751299.html