zoukankan      html  css  js  c++  java
  • BestCoder Round #34B——数学——Building Blocks

    Problem Description

    After 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 2 1",but not "3 1 1 3".

    You are request to calculate the minimum blocks should LeLe move.

    Input

    There are multiple test cases, about 100 cases.

    The first line of input contains three integers n,W,H(1n,W,H50000).n indicate n piles blocks.

    For the next line ,there are n integers A1,A2,A3,,An indicate the height of each piles. (1Ai50000)

    The height of a block is 1.

    Output

    Output the minimum number of blocks should LeLe move.

    If there is no solution, output "-1" (without quotes).

    Sample Input
    4 3 2
    1 2 3 5
    4 4 4
    1 2 3 4
    Sample Output
    1
    -1
    Hint
    In first case, LeLe move one block from third pile to first pile.
    大意:搭积木,连续的要3个,高度要是2,共4组,每次放能放到左边或者右边,不能放到两组之间,开long long 
    把区间移动
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int inf = 0x3f3f3f3f;
    typedef long long ll;
    int main()
    {
        int n,w,h;
        int a[110];
        while(~scanf("%d%d%d",&n,&w,&h)){
            ll l ,r, sum;
            l = r = sum =0;
            memset(a,0,sizeof(a));
                for(int i = w + 1; i <= w + n;i++){
                    scanf("%d",&a[i]);
                    sum += a[i];
                }
                if(sum < w*h){
                        printf("-1
    ");
                        continue;
                }
                int min1 = inf;
                for(int i = 1; i <= 2*w + n;i++){
                 if(a[i] - h > 0)
                 r += a[i] - h;
                 else  l += a[i] - h;
                 if(i >= w){
                      l = -l;
                     int temp = max(l,r);
                     min1 = min(min1,temp);
                 l = -l;
                if(a[i-w+1]-h > 0)
                r -= a[i-w+1]-h;
                else l -= a[i-w+1] - h;
               }
            }
            printf("%d
    ",min1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    数据库分表之Mybatis+Mysql实践
    mysql中You can't specify target table for update in FROM clause错误
    SQL中的limit用法
    在电脑端打开apk文件
    mysql进阶(五)数据表中带OR的多条件查询
    Java之——汉字转换拼音(大小写)
    数据库查询模糊匹配
    produces在@requestMapping中的使用方式和作用
    JSONP跨域请求数据报错 “Unexpected token :”的解决办法
    C# TcpClient的Connect超时处理(Timeout)
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4363634.html
Copyright © 2011-2022 走看看