zoukankan      html  css  js  c++  java
  • High Load Database(二分+前缀和)

    Henry profiles a high load database migration script. The script is the list of n transactions. The i-th transaction consists of ai queries. Henry wants to split the script to the minimum possible number of batches, where each batch contains either one transaction or a sequence of consecutive transactions, and the total number of queries in each batch does not exceed t.
    Unfortunately, Henry does not know the exact value of t for the production database, so he is going to estimate the minimum number of batches for q possible values of t: t1, t2, ... , tq. Help Henry to calculate the number of transactions for each of them.

    输入

    The first line contains a single integer n — the number of transactions in the migration script (1 ≤ n ≤ 200 000).
    The second line consists of n integers a1, a2,... , an — the number of queries in each transaction (1 ≤ ai; ∑ai ≤ 106).
    The third line contains an integer q — the number of queries (1 ≤ q ≤ 100 000).
    The fourth line contains q integers t1, t2,... , tq (1 ≤ ti ≤∑ai).

    输出

    Output q lines. The i-th line should contain the minimum possible number of batches, having at most ti queries each. If it is not possible to split the script into the batches for some ti, output “Impossible”
    instead.
    Remember that you may not rearrange transactions, only group consecutive transactions in a batch.

    样例输入 Copy

    6
    4 2 3 1 3 4
    8
    10 2 5 4 6 7 8 8
    

    样例输出 Copy

    2
    Impossible
    4
    5
    4
    3
    3
    3

    题目意思:就是说有一群连续的进制4 2 3 1 3 4,就是给你一个篮子,最需要几个篮子可以装完。
    这个题就是数据大一点,nlg(n)lg(n)可以解决,就是要记忆化一下,不然要超时
    #include<bits/stdc++.h>
    using namespace std;
    const int N = 4000010;
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int a[N],f[N],sum[N];
    int main()
    {
        int n;
        int max1 = 0;
        n=read();
        for(int i=1;i<=n;i++)
        {
            a[i]=read();
            max1=max(a[i],max1);
            sum[i]=sum[i-1]+a[i];
        }
        int q;
        q=read();
        while(q--)
        {
            int x;
            x = read();
            if(x < max1) puts("Impossible");
            else{
                if(f[x]){
                    printf("%d
    ",f[x]);
                }
                else{
                    int ans = 0 ;
                    int pos = 0 , i = 1 ;
                    while(i<=n)
                    {
                        int val=x+sum[pos];
                        int j=upper_bound(sum+i+1,sum+n+1,val)-sum;
                        ans++;
                        pos=j-1;
                        i=j;
                    }
                    f[x]=ans;
                    printf("%d
    ",ans);
                }
            } 
        }
        return 0;
    }
     
    
    
    



  • 相关阅读:
    Javascript对象中关于setTimeout和setInterval的this介绍
    javascript中setInterval中第一个参数加引号与不加引号的区别
    如何使用定时器settimeout、setInterval执行能传递参数的函数(转)
    如何在html5的canvas画布中绘制gif动态图片
    如何学好C++语言
    MongoDB 数据迁移和同步
    Google论文之三----MapReduce
    手写LinkedList实现(基于双链表)
    手写LinkedList实现(基于单链表)
    手写ArrayList集合框架
  • 原文地址:https://www.cnblogs.com/lipu123/p/13796303.html
Copyright © 2011-2022 走看看