HDU 5191 Building Blocks (模拟)

题目链接:?pid=5191题目大意:n堆木块,每个木块高为1,,第i堆高ai,现在要求组成连续w个高度为h的木堆,最少移动几个木块,注意不能添加木块,且移动时,不可移动置两已存在的堆之间题目分析:枚举区间w的位置,因为要考虑ai的最小值都大于h的情况,因此我们需要将区间分成三段[1 – w], [w+1, w+n], [w+n, w+w+n],动态维护区间w,用t1,t2表示当前区间内需要加入的和需要移出的木块数,区间每移动一次,头尾两个值要修改,下面给出样例1动态维护的过程: 0 0 0 1 2 3 5 0 0 0t1 6 6 6 5 3 1 0 2 4 6 t2 0 0 0 0 0 1 4 4 3 0交的时候用C++交#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const MAX = 150005;ll a[MAX];int main(){ll n, w, h;while(scanf("%I64d %I64d %I64d", &n, &w, &h) != EOF){ll sum = 0;memset(a, 0, sizeof(a));for(int i = w + 1; i <= w + n; i++){scanf("%I64d", &a[i]);sum += a[i];}if(sum < h * w){printf("-1\n");continue;}ll t1 = w * h, ans = w * h, t2 = 0;for(int i = w + 1; i <= w + w + n; i++){if(a[i – w] < h)t1 -= (h – a[i – w]);elset2 -= (a[i – w] – h);if(a[i] < h)t1 += (h – a[i]);elset2 += (a[i] – h);ans = min(ans, max(t1, t2));}printf("%I64d\n", ans);}}

想做你的有缘人,可是我知道结果是惨淡的,但还是心存希望!

HDU 5191 Building Blocks (模拟)

相关文章:

你感兴趣的文章:

标签云: