HDU 5191 Building Blocks (simulation), hdu5191

Source: Internet
Author: User

HDU 5191 Building Blocks (simulation), hdu5191


Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission (s): 829 Accepted Submission (s): 186
Problem DescriptionAfter enjoying the movie, LeLe went home alone. LeLe decided to build blocks.
LeLe has already built N Piles. He wants to move some blocks to make W Consecutive piles with exactly the same height H .
LeLe already put all of his blocks in these piles, which means he can not add any blocks into them. besides, he can move a block from one pile to another or a new one, but not the position betweens two piles already exists. for instance, after one move, "3 2 3" can become "2 2 4" or "3 2 1", but not "3 1 1 3 ".
You are request to calculate the minimum blocks shocould LeLe move. InputThere are multiple test cases, about 100 Cases.
The first line of input contains three integers N, W, H (1 ≤ n, W, H ≤50000) . N Indicate N Piles blocks.
For the next line, there are N Integers A1, A2, A3 ,......, An Indicate the height of each piles. (1 ≤ Ai ≤ 50000)
The height of a block is 1. OutputOutput the minimum number of blocks shocould LeLe move.
If there is no solution, output "-1" (without quotes ).
Sample Input
4 3 21 2 3 54 4 41 2 3 4
 
Sample Output
1-1HintIn first case, LeLe move one block from third pile to first pile. 
SourceBestCoder Round #34

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5191

N-heap blocks, each with a height of 1 and I-heap high ai are required to form a heap with w consecutive heights of h. At least a few blocks can be moved. Note that no wood blocks can be added, when the heap is moved, it cannot be moved between two existing heaps.

Question analysis: the position of the enumerated range w. Considering that the minimum values of ai are greater than h, we need to divide the range into three sections: [1-w], [w + 1, w + n], [w + n, w + n], and dynamic maintenance interval w. t1 and t2 indicate the number of blocks to be added and removed in the current interval, each time the interval is moved, the first value and the last value must be modified. The following is an example of 1 Dynamic maintenance process:
0 0 0 1 2 3 5 0 0
T1 6 6 6 5 3 1 0 2 4 6
T2 0 0 0 0 1 4 4 3 0

Use C ++ for delivery

#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]);            else                t2 -= (a[i - w] - h);            if(a[i] < h)                t1 += (h - a[i]);            else                t2 += (a[i] - h);            ans = min(ans, max(t1, t2));        }        printf("%I64d\n", ans);    }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.