zoukankan      html  css  js  c++  java
  • SPOJ:Help BTW(二分)

    BTW wants to buy a gift for her BF and plans to buy an integer array. Generally Integer arrays are costly and hence bought the cheapest one from the market. Her BF is very judgemental and assess the quality of the array by just looking at the smallest element in the array. Hence, she decided to improve the quality of the array. Increment operation on array elements are very costly and would take ONE FULL DAY to complete. Also, Increment operations can be done in parallel on at most M consecutive array elements. She only has K days left. Help BTW calculate the maximum possible “quality” of the array she can attain.

    (BTW BTW is the name of the character :P )

    Input

    Very first line contains T – (number of test cases)

    First line in each test case contains N – (size of the array BTW has bought from the market), M, K (days left)

    Second line in each test case contains N integers (values of the initial array BTW bought from the market)

    Output

    Print the maximum possible “quality” she can attain.

    Constraints

    1<=T<=100

    1<=N<=10^5

    0<=M<=N

    0<=K<=10^9

    0<=Values of array<=10^9

    Example

    Sample test case 1

    Input

    3 2 1

    2 2 3

    Output

    3

    Sample test case 2

    Input

    3 2 1

    2 3 2

    Output

    2

      

    题意:有N个物品,排成一排,给个物品有一个价值,给定M,K,表示有K次机会,每次将一段连续的不超过M长度的区间的物品价值都加1。求将最小价值最大化是。

    思路:开始一看100组数据,二分复杂度是(T*N*log(Min+K))。。。以为过不了,想其他方法又想不出,二分试一试居然过了。。。
               给长度为M的区间都加一个值的操作是:delta数据记录累加前缀和,如果位置i加值val,那么位置i+M减值val,然后就可以二分乱搞了。
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long 
    const int maxn=100010;
    const int inf=2000000000;
    ll sum[maxn],tsum[maxn],delta[maxn],N,M,K;
    bool check(ll Mid){
        ll tmp=K;
        for(int i=1;i<=N;i++) tsum[i]=sum[i],delta[i]=0;
        for(int i=1;i<=N;i++){
            delta[i]+=delta[i-1];
            if(tsum[i]+delta[i]<Mid&&tmp){
                ll t=min(tmp,Mid-tsum[i]-delta[i]);
                delta[i]+=t;
                if(M+i<=N) delta[M+i]-=t;
                tmp-=t;
            }
            tsum[i]+=delta[i];
            if(tsum[i]<Mid) return false;
        }
        return true;
    }
    int main()
    {
        int T,i,j;
        ll Min;
        scanf("%d",&T);
        while(T--){
            scanf("%lld%lld%lld",&N,&M,&K); Min=inf;
            for(i=1;i<=N;i++){
               scanf("%lld",&sum[i]);
               if(sum[i]<Min) Min=sum[i];
            }
            ll L=Min,R=Min+K,Mid,ans=Min;
            while(L<=R){    
                Mid=(L+R)>>1LL;    
                if(check(Mid)) ans=Mid,L=Mid+1;
                else R=Mid-1;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
      
  • 相关阅读:
    Android之dialog
    android上下文菜单(ContextMenu)
    Android中Handler的使用2
    Intent 各种跳转 .
    Android之Adapter用法总结
    android之Menu 实例与详解
    android学习之FrameLayout
    Andriod: 在xml布局中使用自定义属性
    你软考了吗?
    菜鸟从零学习数据库(三)——存储过程
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8977492.html
Copyright © 2011-2022 走看看