#702 (Div. 3)G. Old Floppy Drive(前缀和+二分)

Posted li_wen_zhuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#702 (Div. 3)G. Old Floppy Drive(前缀和+二分)相关的知识,希望对你有一定的参考价值。

题目描述

Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with n integers written on it.
Polycarp wrote the numbers from the disk into the a array. It turned out that the drive works according to the following algorithm:
the drive takes one positive number x as input and puts a pointer to the first element of the a array;
after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the a array, the last element is again followed by the first one;
as soon as the sum is at least x, the drive will shut down.
Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you m questions. To answer the i-th of them, you need to find how many seconds the drive will work if you give it xi as input. Please note that in some cases the drive can work infinitely.
For example, if n=3,m=3, a=[1,−3,4] and x=[1,5,2], then the answers to the questions are as follows:
the answer to the first query is 0 because the drive initially points to the first item and the initial sum is 1.
the answer to the second query is 6, the drive will spin the disk completely twice and the amount becomes 1+(−3)+4+1+(−3)+4+1=5.
the answer to the third query is 2, the amount is 1+(−3)+4=2.

Input

The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of each test case consists of two positive integers n, m (1≤n,m≤2⋅105) — the number of numbers on the disk and the number of asked questions.
The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109).
The third line of each test case contains m positive integers x1,x2,…,xm (1≤x≤109).
It is guaranteed that the sums of n and m over all test cases do not exceed 2⋅105.

Output

Print m numbers on a separate line for each test case. The i-th number is:
−1 if the drive will run infinitely;
the number of seconds the drive will run, otherwise.

Example

input
3
3 3
1 -3 4
1 5 2
2 2
-2 0
1 2
2 2
0 1
1 2
output
0 6 2
-1 -1
1 3

题目大意

给定序列a,把a反复制成一个无限序列,然后给m个询问,每次给定x,问a第一个前缀和大于等于x的下标。如果该下标为无穷则输出-1.

题目分析

这道题很明显需要二分搜索,但是因为a[]中有负数,因此a[]的前缀和数组不能直接使用二分搜索。

因此我们我们可以再定义一个mx[]数组,mx[i]=max( sum[1-i] ) //sum[]为a[]的前缀和。然后我们就可以对这个数组进行二分查找了。

对答案的各种情况进行分类讨论:

  1. 如 果 s u m [ n ] < = 0 并 且 m x [ n ] < x , 则 永 远 也 到 不 了 x 。 输 出 − 1 如果 sum[n]<=0 并且 mx[n]<x,则永远也到不了x。输出-1 sum[n]<=0mx[n]<xx1
  2. 如 果 m x [ n ] > = x , 说 明 第 一 轮 循 环 即 可 找 到 答 案 , 直 接 利 用 二 分 查 找 即 可 。 如果mx[n]>=x,说明第一轮循环即可找到答案,直接利用二分查找即可。 mx[n]>=x
  3. 否 则 , 我 们 首 先 要 进 行 几 轮 循 环 才 可 以 到 达 x , 循 环 的 轮 数 d = c e i l ( 1.0 ∗ ( x − m x [ n ] ) / a [ n ] ) 。 因 此 我 们 可 以 先 循 环 上 d 轮 , 即 等 于 + d ∗ n − 1 , 再 进 行 二 分 查 找 。 否则,我们首先要进行几轮循环才可以到达x,循环的轮数d=ceil(1.0*(x-mx[n])/a[n])。因此我们可以先循环上d轮,即等于+d*n-1,再进行二分查找。 xd=ceil(1.0(xmx[n])/a[n])d+dn1
代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=2e5+5,INF=1e9+7;
LL a[N],mx[N];
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
    	memset(a,0,sizeof a);		//清空数组
    	memset(mx,0,sizeof mx);
    	int n,m;
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			a[i]+=a[i-1];				//进行前缀和累加
			mx[i]=max(mx[i-1],a[i]);	//记录前缀和的最大值
		}
    	for(int i=1;i<=m;i++)
    	{
    		int x;
    		cin>>x;
    		if(mx[n]<x&&a[n]<=0) cout<<"-1"<<' ';		//情况1
			else if(mx[n]>=x) cout<<lower_bound(mx+1,mx+1+n,x)-mx-1<<' ';//情况2
			else		//情况3
			{
				LL ans=ceil(1.0*(x-mx[n])/a[n]);
				x-=ans*a[n];
				ans=ans*n-1;
				ans+=lower_bound(mx,mx+1+n,x)-mx;
				cout<<ans<<' ';
			}
		}
		cout<<endl;
	}
	return 0; 
}

以上是关于#702 (Div. 3)G. Old Floppy Drive(前缀和+二分)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #644 (Div. 3) G. A/B Matrix

Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)

Codeforces Round #702 (Div. 3)-E. Accidental Victory-迭代器跳数

Codeforces Round #731 (Div. 3) G. How Many Paths? 强连通分量缩点+拓扑

Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem

Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) G. The Tree