zoukankan      html  css  js  c++  java
  • HDU 5358 枚举+尺选

    soda has an integer array a1,a2,,ana1,a2,…,an. Let S(i,j)S(i,j) be the sum of ai,ai+1,,ajai,ai+1,…,aj. Now soda wants to know the value below:
    i=1nj=in(log2S(i,j)+1)×(i+j)
    Note: In this problem, you can consider log20 as 0. 

    InputThere are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case: 

    The first line contains an integer n(1n105), the number of integers in the array. 
    The next line contains nn integers a1,a2,,an(0≤ai≤105).
    OutputFor each test case, output the value.Sample Input

    1
    2
    1 1

    Sample Output

    12



    题意:求一个数组对题目中出现的那个公式的值

    做过好几道枚举的题了,但是遇到题还是想不出来使用枚举。

    根据题目给的数据范围,我们知道sum(1,n)<=1e10 floor(log2(1e10)) = 33
    所以我们枚举log2(sum+1)的值,进行尺选就可以了。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    using namespace std;
    
    const int maxn = 1e5+5;
    ll s[maxn];
    ll low[50], high[50];
    int n, x;
    ll solve(int k){
        if(s[n]<low[k-1])return 0;
        ll l=1, r=1,num=0;
        for(ll j=1;j<=n;j++){
            l = max(l,j);
            while(l<=n && s[l]-s[j-1]<low[k-1])l++;
            r = max(r,l);
            while(r<=n && s[r]-s[j-1]<=high[k-1])r++;
            if(r>l)
                num += (r-l)*j+(l+r-1)*(r-l)/2;
        }
        return num*k;
    }
    int main(){
        int t;
        scanf("%d",&t);
        for(int i=1;i<35;i++){
            low[i] = 1ll<<i;
            high[i] = (1ll<<(i+1))-1;
        }
        low[0]=0,high[0]=1;
        while(t--){
            ll ans = 0;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%d",&x),s[i] = s[i-1]+x;
            for(int i=1;i<35;i++)
                ans += solve(i);
            printf("%lld
    ",ans);
        }
        return 0;
    }
    View Code





  • 相关阅读:
    ASIHttpRequest封装
    算法6-6:哈希表应用之字典应用
    代码杂记(三)
    拥塞避免算法
    使用Unity制作的一个望远镜特效,在狙击手游戏中非经常见
    Ubuntu Linux下安装Oracle JDK
    Java输入输出流(2)
    软考之路--小桥流水人家
    [Java]LeetCode116. 填充同一层的兄弟节点 | Populating Next Right Pointers in Each Node
    [Swift]LeetCode115. 不同的子序列 | Distinct Subsequences
  • 原文地址:https://www.cnblogs.com/kongbb/p/10876289.html
Copyright © 2011-2022 走看看