ICPC Russia High Load Database

H. High Load Database

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.

Input
The first line contains a single integer n — the number of transactions in the migration script (1≤n≤200000).

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≤100000).

The fourth line contains q integers t1,t2,…,tq (1≤ti≤∑ai).

Output
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.

Example
inputCopy
6
4 2 3 1 3 4
8
10 2 5 4 6 7 8 8
outputCopy
2
Impossible
4
5
4
3
3
3


先求前缀和之后二分,但是很明显可以卡掉。

但是我们想一下,如果我们记忆化最坏复杂度,可以发现:

q*(n+n/2+n/3+…+n/n)*log(n) ,里面不就是一个调和级数吗?

所以复杂度为: q * logn * logn

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e5+10;
int n,q,a[N],res[N*10],vis[N],mx,s,x;
int find(int l,int r){
	int ll=l;
	while(l<r){
		int mid=l+r+1>>1;
		if(a[mid]-a[ll-1]<=x)	l=mid;
		else	r=mid-1;
	}
	return l;
} 
inline int solve(int x){
	s=0; int L=1,ans=0; a[n+1]=x;
	while(L<=n){
		L=find(L,n)+1;	ans++;
	}
	return ans;
}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){scanf("%d",&a[i]); mx=max(mx,a[i]);	a[i]+=a[i-1];}
	cin>>q;
	while(q--){
		scanf("%d",&x);	if(x<mx){puts("Impossible");	continue;}
		if(res[x]){printf("%d\n",res[x]);	continue;}
		res[x]=solve(x);	printf("%d\n",res[x]);
	}
	return 0;
}
全部评论

相关推荐

就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
真烦好烦真烦:豆包润色了自己没看看吗,再说了,都说豆包是愚蠢且勤快的大学生,ds才是聪明的研究生,怎么敢让豆包写论文的
你们的毕业论文什么进度了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务