Educational Codeforces Round 52 (Rated for Div. 2) C. Make It Equal

题目:https://codeforces.com/contest/1065/problem/C

这道题是服了,我的算法是贪心模拟,贪心的找切片,写的比较慢,因为是模拟,所以考虑的比较多,而且变量比较多。。。。。。
赛后居然被hack了。。。好气,为什么呢,就是少考虑了初始时高度都一样,每次都是不考虑特殊情况。。。希望自己能够吃个教训。。。
还有我觉得自己脑子里都是暴力,模拟,看了大佬的代码,人家的算法根本 不用写那么长时间,思路还比较清晰。。。。。

我的垃圾代码和思路:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1);
const double eps = 1e-8;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 2 * 1e5 + 5;

LL a[N];
LL n,k;

int main()
{
    scanf("%lld %lld",&n,&k);
    LL sum = 0;
    for(int i = 0;i < n;++i){
        scanf("%lld",&a[i]);
        sum += a[i];
    }
    sort(a,a + n);
    sum -= a[0] * n;
    int h = a[0];
    LL ans = 0;
    int res = 0;
    //cout << sum << endl;
    for(LL i = 1;i < n;++i){
        if(sum == 0){
            break;
        }
        if(sum <= k){
            res++;
            break;
        }
        ans += (a[i] - h) * (n - i);
        if(ans >= k){
            res++;
            ans -= (a[i] - h) * (n - i);
            int x = h;
            while((ans + (h - x) * (n - i)) <= k){
                h++;
            }
            h--;
            sum -= (ans + (h - x) * (n - i));
            ans = 0;
            i--;
        }else{
            h = a[i];
        }
    }
    printf("%d\n",res);
    return 0;
}

他把每一个高度有的立方体块哈希到一个数组里,然后开始从末尾开始计算在哪个高度需要进行切

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1);
const double eps = 1e-8;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 2 * 1e5 + 5;

int a[N];

int main()
{
    int n,k;
    memset(a,0,sizeof(a));
    scanf("%d %d",&n,&k);
    for(int i = 1;i <= n;++i){
        int x;
        scanf("%d",&x);
        a[x]++;
    }
    for(int i = 200000;i >= 0;--i) a[i] += a[i + 1];
    LL sum = 0;
    int res = 0;
    for(int i = 200000;i >= 0;--i){
        if(a[i] == n) break;
        sum += a[i];
        if(sum > k){
            sum = 0;res++;i++;
        }
    }
    if(sum) res++;
    printf("%d\n",res);
    return 0;
}


猜你喜欢

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