• 「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)

  • 相关阅读:
    第六章:随机化(续1)
    第六章:随机化
    PAT甲组 1010 Radix (二分)
    关于我的2019年度总结
    Codeforces 567D:One-Dimensional Battle Ships(二分)
    Codeforces 567C:Geometric Progression(DP)
    Codeforces 567B:Berland National Library(模拟)
    HDU 4790:Just Random(容斥)
    Codeforces 450C:Jzzhu and Chocolate(贪心)
    Codeforces 450E:Jzzhu and Apples(构造,数学)
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/11751299.html
Copyright © 2020-2023  润新知