zoukankan      html  css  js  c++  java
  • Kattis

    这里写图片描述

    这里写图片描述

    题意
    有一堆人 要给他们的朋友 买一个生日礼物,然后 每个人 给出自己的最大负担额度 并且给出礼物总价 然后要给出一种解决方案 尽量让 所有人的支出都接近平均,如果实在无法平均,那就让 先来的人 多处

    思路
    用贪心的思路,我们先将负担额度排序,第一个关键字是负担额度,第二个关键字是进来的先后次序,按双关键字排序,先按负担额度从小到大,若负担额度相等,再按进来的先后次序从大到小

    然后 比较每一个人的负担额度 与 平均值的关系,如果小于等于平均值 那么它就出这个负担额度的钱,如果大于平均值,就出平均值的钱,然后这个平均值也是在变的 每次更新之后 sum - 这个人出的钱 并且 人数 - 1
    然后 每次更新平均值 ,这样就能保证 在不平均的情况下,后面的人出的钱多

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <climits>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    
    const double PI  = 3.14159265358979323846264338327;
    const double E   = exp(1);
    const double eps = 1e-6;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e2 + 5;
    const int MOD  = 1e9 + 7;
    
    struct Node
    {
        int num, id;
    }q[maxn];
    
    bool comp(Node x, Node y)
    {
        if (x.num == y.num)
            return x.id > y.id;
        return  x.num < y.num;
    }
    
    bool cmp(Node x, Node y)
    {
        return x.id < y.id;
    }
    
    int main()
    {
        int t;
        cin >> t;
        while (t--)
        {
            int p, n;
            scanf("%d%d", &p, &n);
            int sum = 0;
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &q[i].num);
                q[i].id = i;
                sum += q[i].num;
            }
            if (sum < p)
                printf("IMPOSSIBLE");
            else if(sum == p)
            {
                for (int i = 0; i < n; i++)
                    {
                        if (i)
                            printf(" ");
                        printf("%d", q[i].num);
                    }
            }
            else
            {
                sort(q, q + n, comp);
                sum = p;
                int ave;
                for (int i = 0; i < n; i++)
                {
                    ave = sum / (n - i);
                    q[i].num = min(ave, q[i].num);
                    sum -= q[i].num;
                }
                sort(q, q + n, cmp);
                for (int i = 0; i < n; i++)
                {
                    if (i)
                        printf(" ");
                    printf("%d", q[i].num);
                }
            }
            printf("
    ");
        }
    } 
    
    
    
    
  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433241.html
Copyright © 2011-2022 走看看