HDU-3473Minimum Sum

简介:
Problem Description
You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you some intervals [l, r]. For each interval, you need to find a number x to make  as small as possible!
 

Input
The first line is an integer T (T <= 10), indicating the number of test cases. For each test case, an integer N (1 <= N <= 100,000) comes first. Then comes N positive integers x (1 <= x <= 1,000, 000,000) in the next line. Finally, comes an integer Q (1 <= Q <= 100,000), indicting there are Q queries. Each query consists of two integers l, r (0 <= l <= r < N), meaning the interval you should deal with.

 

Output
For the k-th test case, first output “Case #k:” in a separate line. Then output Q lines, each line is the minimum value of  . Output a blank line after every test case.
 

Sample Input

  
  
2 5 3 6 2 2 4 2 1 4 0 2 2 7 7 2 0 1 1 1
 

Sample Output

  
  
Case #1: 6 4 Case #2: 0 0
 

Author
standy
 

Source
 

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:   3474   1828   3397   3333   3472


被杭电的输出坑了 好久。。。。

printf lld 就WA 要I64d才行。。。。。真吭。!


易得使绝对值和最小就是中位数,能够參考坐标上的点到两点间距离之和最小的原理。


这道题让我对划分树的原理理解更加深刻了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
#define md(x, y) (((x)+(y))>>1)
const int maxn = 100000+10;
typedef long long LL;
int n,m;
int ls;
int num[maxn];
int seg[20][maxn];
int lftnum[20][maxn];
LL lfts[20][maxn];
LL presum[maxn];
LL lsum;
void build(int L,int R,int dep){
    if(L==R)return;
    int mid = md(L,R);
    int key = num[mid];
    int lcnt = mid-L+1;
    for(int i = L; i <= R; i++){
        if(seg[dep][i] < key)
            lcnt--;
    }
    int lp = L,rp = mid+1;
    for(int i = L; i <= R; i++){
        if(i==L){
            lftnum[dep][i] = 0;
            lfts[dep][i] = 0;
        }else{
            lfts[dep][i] = lfts[dep][i-1];
            lftnum[dep][i] = lftnum[dep][i-1];
        }
        if(seg[dep][i] < key){
            lftnum[dep][i]++;
            lfts[dep][i] += seg[dep][i];
            seg[dep+1][lp++] = seg[dep][i];
        }
        else if(seg[dep][i] > key){
            seg[dep+1][rp++] = seg[dep][i];
        }
        else{
            if(lcnt>0){
                lcnt--;
                lftnum[dep][i]++;
                lfts[dep][i] += seg[dep][i];
                seg[dep+1][lp++] = seg[dep][i];
            }else{
                seg[dep+1][rp++] = seg[dep][i];
            }
        }
    }

    build(L,mid,dep+1);
    build(mid+1,R,dep+1);
}

LL query(int L,int R,int ll,int rr,int dep,int k){
    if(ll == rr)
        return seg[dep][ll];
    int mid = md(ll,rr);
    int ncnt,ucnt;
    LL tsum = 0;
    if(L==ll){
        ncnt = 0;
        tsum = lfts[dep][R];
        ucnt = lftnum[dep][R]-ncnt;
    }else{
        ncnt = lftnum[dep][L-1];
        ucnt = lftnum[dep][R]-ncnt;
        tsum = lfts[dep][R]-lfts[dep][L-1];
    }
    if(ucnt >= k){
        L = ll + ncnt;
        R = ll + ncnt + ucnt-1;
        return query(L,R,ll,mid,dep+1,k);
    }else{
        int aa = L-ll-ncnt;
        int bb = R-L-ucnt+1;
        L = mid+aa+1;
        R = mid+aa+bb;
        ls += ucnt;
        lsum += tsum;
        return query(L,R,mid+1,rr,dep+1,k-ucnt);
    }
}
int main(){

    int ncase,T=1;
    cin >>ncase;
    while(ncase--){
        scanf("%d",&n);
        presum[0] = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d",&num[i]);
            seg[0][i] = num[i];
            presum[i] = presum[i-1]+num[i];
        }
        sort(num+1,num+n+1);
        build(1,n,0);
        scanf("%d",&m);
        printf("Case #%d:\n",T++);
        while(m--){
            int a,b,k;
            scanf("%d%d",&a,&b);
            ++a;++b;
            k = (b-a)/2+1;
            lsum = 0;
            ls = 0;
            int rs = 0;
            int t = query(a,b,1,n,0,k);
            LL rsum = presum[b]-presum[a-1]-t-lsum;
            rs = b-a-ls;
            LL ans = rsum-lsum+t*(ls-rs);
            printf("%I64d\n",ans);
        }
        printf("\n");
    }
    return 0;
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5280977.html,如需转载请自行联系原作者

相关文章
LeetCode 64. Minimum Path Sum
给定m x n网格填充非负数,找到从左上到右下的路径,这最小化了沿其路径的所有数字的总和。 注意:您只能在任何时间点向下或向右移动。
102 0
LeetCode 64. Minimum Path Sum
LeetCode 209. Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
116 0
LeetCode 209. Minimum Size Subarray Sum
LeetCode 39. Combination Sum
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。
72 0
LeetCode 39. Combination Sum
[LeetCode]--64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or
1099 0
LeetCode 216 Combination Sum III(Backtracking)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51935033 翻译 找出所有的k个数字相加得到数字n的组合,只有1到9的数字可以被使用,并且每个组合间需要是不同的数字集。
730 0