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;
}
全部评论

相关推荐

头像
02-21 16:31
长沙理工大学
大家好,今天分享一个很贴合目前校招时间段的提问:Up你好,本人双非本科大四,软件工程专业。大学前两年因为感觉前端好学,岗位也多选择学习前端。但那时比较懒散,课也多,所以前端也没有学多好。后来互联网寒冬,觉得出去不好找工作。就在大三下开始准备考研,但在去年10月份放弃考研(因为家里的一些事故,一个半月没有复习考研),处理好后,剩70多天感觉考不上值得上的学校。所以干脆准备就业,但感觉前端这个方向特别凉,于是换成了Linux&nbsp;c++方向(为此拒绝了一个前端实习)10月底到现在复习了c语言,学习了C++语法,特性,包括STL这些。学习了Linux系统编程进程线程,网络编程tcp/udp,多路转接,l...
牛客230000345号:毕业入坑两年,提点参考的东西吧,建议边找边备研,学历才是第一生产力,后期如果你要职业发展,这是最基本的几个了,工作和晋升除了项目经验,不就是比的派个人学历、吹牛能力和一堆头衔了(晋升的话,派系很重要)。 工作方面,不了解服务端,但是你可以看招聘,其实相比来说qt在客户端和服务端都可以用到,而且跨平台兼容性好,而且qt不就是c+++吗(学好c++,用哪个框架都不头痛),qt不只是给你个UI界面,封装的很多东西都是可以借鉴的。看你想去哪个城市,现在长沙软件行情不好,真心建议没上岸可以去深圳看看,长沙这边工资对标深圳砍半(眼泪流下来),长沙不少大一点私企面试的也开始卷学历卷项目(双非泪奔),如果想去国企你要能吹当然也可以(其实国企也就那12%的公积金了,并不稳定,但是稳定穷是肯定的)。 想去好一点的,建议把基础打牢,学历一定要提高(长期发展一定要,国内还是不少地方学历论的),如果有实习期建议能参与公司项目就参与,不然只会被拷打,最好从项目或者demo里把设计模式、指针、特性、模板、多线程实现并发并行、通讯协议、数据库这些基本的学会一部分,建议再学学qml和Linux,最好学一点嵌入式(Linux用在嵌入式板挺多的),掌握一门脚本语言(Python,Python,Python)和git或者svn代码管理,没签合同(不是三方),你还是校招生,校招只有一次(当然也可以说是本科一次,硕士一次,博士一次),用了错过就没有了,好多公司最喜欢招应届生了,一张白纸(又便宜又容易被PUA)。 最后,其实纠结这么多,不如第一份工作就选你最喜欢的编程语言、框架和操作系统,反正都是牛马,也不一定只吃一家喂的草
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务