ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-01 21:03:31  阅读:181  来源: 互联网

标签:Old Floppy drive Drive int test include mx first


题目描述

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]<=0并且mx[n]<x,则永远也到不了x。输出−1
  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,再进行二分查找。 否则,我们首先要进行几轮循环才可以到达x,循环的轮数d=ceil(1.0∗(x−mx[n])/a[n])。因此我们可以先循环上d轮,即等于+d∗n−1,再进行二分查找。
代码如下
#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; 
}

标签:Old,Floppy,drive,Drive,int,test,include,mx,first
来源: https://blog.csdn.net/li_wen_zhuo/article/details/116332980

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有