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

题目:http://codeforces.com/contest/985/problem/C
题解:排序后贪心,用小和小的数组一个桶,总之消耗长度小的木板,使得最后的到的和最大
为什么写这个呢,因为我耗费了好长时间找bug,不能将思想准确的表达为代码,还是逻辑思维跟不上写代码速度,写到那儿老想先这样写上,再调着改

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100005;
LL a[N];
int main()
{
    LL n,k,l;
    scanf("%lld %lld %lld",&n,&k,&l);
    for(int i = 1;i <= n * k;++i)
    {
        scanf("%lld",&a[i]);
    }
    sort(a + 1,a + n * k + 1);
    int x = upper_bound(a + 1, a + n * k + 1,a[1] + l) - a - 1;
    //cout << x << endl;
    LL sum = 0;
    if(x < n){
        printf("0\n");
        return 0;
    }
    if(x == n){
        for(int i = 1;i <= n;++i){
            sum += a[i];
        }
        printf("%lld\n",sum);
        return 0;
    }
    int i = 1;
    int j = 1;
    while(i <= x){
        //cout << i << " " << j << " " << sum << endl;
        if(x - (i + k - 1) >= (n - j)){
            sum += a[i];
            i = i + k;
            j++;
        }
        else{
        //就是这里浪费了时间,只要加上这个sum += a[i]就对了,每次不能消耗k个较小的数了
        //就进这里了,但一定要加上sum += a[i];还是不仔细
                sum += a[i];
                for(int y = x;y >= x - (n - j) + 1;--y)
                {
                //cout << y << endl;
                    sum += a[y];
                }
            break;
        }
    }
    printf("%lld\n",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/80459498