codeforces 526 E(神题)

E. Transmitting Levels
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Optimizing the amount of data transmitted via a network is an important and interesting part of developing any network application.

In one secret game developed deep in the ZeptoLab company, the game universe consists of n levels, located in a circle. You can get from level i to levels i - 1 and i + 1, also you can get from level 1 to level n and vice versa. The map of the i-th level description size is ai bytes.

In order to reduce the transmitted traffic, the game gets levels as follows. All the levels on the server are divided into m groups and each time a player finds himself on one of the levels of a certain group for the first time, the server sends all levels of the group to the game client as a single packet. Thus, when a player travels inside the levels of a single group, the application doesn't need any new information. Due to the technical limitations the packet can contain an arbitrary number of levels but their total size mustn't exceed b bytes, where b is some positive integer constant.

Usual situation is that players finish levels one by one, that's why a decision was made to split n levels into m groups so that each group was a continuous segment containing multiple neighboring levels (also, the group can have two adjacent levels, n and 1). Specifically, if the descriptions of all levels have the total weight of at most b bytes, then they can all be united into one group to be sent in a single packet.

Determine, what minimum number of groups do you need to make in order to organize the levels of the game observing the conditions above?

As developing a game is a long process and technology never stagnates, it is yet impossible to predict exactly what value will take constant value b limiting the packet size when the game is out. That's why the developers ask you to find the answer for multiple values of b.

Input

The first line contains two integers n, q (2 ≤ n ≤ 106, 1 ≤ q ≤ 50) — the number of levels in the game universe and the number of distinct values of b that you need to process.

The second line contains n integers ai (1 ≤ ai ≤ 109) — the sizes of the levels in bytes.

The next q lines contain integers bj (), determining the values of constant b, for which you need to determine the answer.

Output

For each value of kj from the input print on a single line integer mj (1 ≤ mj ≤ n), determining the minimum number of groups to divide game levels into for transmission via network observing the given conditions.

Sample test(s)
Input
6 3
2 4 2 1 3 2
7
4
6
Output
2
4
3
Note

In the test from the statement you can do in the following manner.

  • at b = 7 you can divide into two segments: 2|421|32 (note that one of the segments contains the fifth, sixth and first levels);
  • at b = 4 you can divide into four segments: 2|4|21|3|2;
  • at b = 6 you can divide into three segments: 24|21|32|.
自己简直弱成渣。。。

思路:last[i]表示以i结尾的段落的起点,cnt[i]表示从last[i]到i的最小段数

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int N = 1000100;
int n,q;
int a[N*2],last[N*2],cnt[N*2];
long long s[N*2],b;
int main()
{
    scanf("%d %d", &n, &q);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", a + i);
        a[i + n] = a[i];
        last[i] = i;
    }
    for (int i=1;i<=n*2;i++)
        s[i]=s[i-1]+a[i];
    while(q--)
    {
        scanf("%I64d", &b);
        int j = 1;
        for (int i = n + 1; i <= n * 2; i++)
        {
            while (s[i] - s[j] > b)
                j++;
            last[i] = last[j];
            cnt[i] = cnt[j] + 1;
            if (i - last[i] >= n)
            {
                printf("%d\n", cnt[i]);
                break;
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值