当前位置:   article > 正文

C. Liebig's Barrels( Educational Codeforces Round 44 (Rated for Div. 2))

c. liebig's barrels

题目链接
刚开始想的有点复杂,一直wa了,最后看了大佬们的题解,才恍然大悟,原来就是一道贪心,硬生生做成模拟了,还写的特别麻烦,半天想不出来,最后写出来还是错的,以后遇到这种情况,就得换个思路重新思考,不能一根筋走到黑,到头来还是一场空。

解决本题的思路:
从后往前进行计算,因为要计算最大的容积,所以采用排序后从后往前计算,加上限制条件,从而进行判断。

//贪心。
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>

using namespace std;

//const int maxn=0x7fffffff;

long long a[100000];

int main()
{
    int n,k,l;
    long long ans=0;
    scanf("%d%d%d",&n,&k,&l);
    int sum=0,num=0;
    for(int i=0;i<n*k;i++)
        scanf("%lld",&a[i]);
    sort(a,a+n*k);
    for(int i=n*k-1;i>=1;i--){
        if(a[i]-a[0]<=l&&sum>=k-1){
            num++;
            sum-=k-1;
            ans+=a[i];
        }
        else sum++;
    }
    printf("%I64d\n",num==n-1?ans+a[0]:0);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/912666
推荐阅读
相关标签
  

闽ICP备14008679号