其他分享
首页 > 其他分享> > #双指针#洛谷 7405 [JOI 2021 Final] 雪玉

#双指针#洛谷 7405 [JOI 2021 Final] 雪玉

作者:互联网

题目传送门


分析

无论怎样刮风,雪球的相对位置不会改变,
实际上每一个空段都由左右两个雪球瓜分(边界空段除外),
那么按照空段长度从小到大排序,用双指针找到恰好第一个未瓜分的位置


代码

#include <cstdio>
#include <cctype>
#include <queue>
#include <algorithm>
#define rr register
using namespace std;
typedef long long lll; const int N=200011;
lll n,Q,lst,a[N],ans[N],rk[N],west[N],step[N],east[N];
inline lll iut(){
	rr lll ans=0,f=1; rr char c=getchar();
	while (!isdigit(c)) f=(c=='-')?-f:f,c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans*f;
}
inline void print(lll ans){
	if (ans>9) print(ans/10);
	putchar(ans%10+48);
}
bool cmp(int x,int y){return a[x]<a[y];}
signed main(){
	n=iut()-1,Q=iut(),lst=iut();
	for (rr int i=1;i<=n;++i)
	    a[i]=iut()-lst,lst+=a[i],rk[i]=i;
	sort(rk+1,rk+1+n,cmp),lst=0;
	for (rr int i=1;i<=Q;++i){
		step[i]=iut(),lst+=step[i];
		if (lst<0) west[i]=max(west[i-1],-lst);
		    else west[i]=west[i-1];
		if (lst>0) east[i]=max(east[i-1],lst);
		    else east[i]=east[i-1];
	}
	ans[1]+=west[Q],ans[n+1]+=east[Q];
	for (rr int i=1,j=1;i<=n;++i){
		for (;j<=Q&&west[j]+east[j]<a[rk[i]];++j);
		if (j>Q){
			for (rr int o=i;o<=n;++o)
			    ans[rk[o]]+=east[Q],ans[rk[o]+1]+=west[Q];
			break;
		}
		ans[rk[i]]+=east[j-1],ans[rk[i]+1]+=west[j-1];
		ans[rk[i]+(step[j]<0)]+=a[rk[i]]-west[j-1]-east[j-1];
	}
	for (rr int i=1;i<=n+1;++i)
	    print(ans[i]),putchar(10);
	return 0;
}

标签:include,洛谷,rr,int,lll,雪玉,ans,east,2021
来源: https://www.cnblogs.com/Spare-No-Effort/p/15423189.html