codeforces819B Mister B and PR Shifts -- 线段树
来源:互联网 发布:淘宝网商银行存钱吗 编辑:程序博客网 时间:2024/06/18 09:16
令
考虑
当
dj+=pi−i+j , j∈[0,pi−i] dj+=i−pi+j , j∈[pi−i+1,n−i] dj+=pi−i+n−j , j∈[n−i+1,n−1]
当
dj+=i−pi+j , j∈[0,n−i] dj+=pi−i+n−j , j∈[n−i+1,n+pi−i] dj+=i−pi−n+j , j∈[n−i+pi+1,n−1]
令
时间复杂度加了读优才过。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; if(p1==p2){ p2=(p1=buf)+fread(buf,1,100000,stdin); if(p1==p2)return EOF; } return *p1++;}inline void Read(int& x){ char c=nc(); for(;c<'0'||c>'9';c=nc()); for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());}#define N 1000010#define ll long longll c[2][N<<2],p[2][N<<2],Tmp,Ans=1e18,d[N];int i,j,k,n,m,x;inline void Push(int x,int l){ int l1=l+1>>1,l2=l>>1; if(p[0][x]){ if(l1==1) c[0][x<<1]+=p[0][x]; p[0][x<<1]+=p[0][x]; if(l2==1) c[0][x<<1|1]+=p[0][x]; p[0][x<<1|1]+=p[0][x]; p[0][x]=0; } if(p[1][x]){ if(l1==1) c[1][x<<1]+=p[1][x]; p[1][x<<1]+=p[1][x]; if(l2==1) c[1][x<<1|1]+=p[1][x]; p[1][x<<1|1]+=p[1][x]; p[1][x]=0; }}inline void Update(int x,int l,int r,int L,int R,int y,bool d){ if(l>R||r<L)return; if(l>=L&&r<=R){ if(l==r)c[d][x]+=y; p[d][x]+=y; return; } int Mid=l+r>>1; Update(x<<1,l,Mid,L,R,y,d); Update(x<<1|1,Mid+1,r,L,R,y,d);}inline void GetAns(int x,int l,int r){ if(l==r){ d[l]=c[0][x]+1ll*l*c[1][x]; return; } Push(x,r-l+1); int Mid=l+r>>1; GetAns(x<<1,l,Mid); GetAns(x<<1|1,Mid+1,r);}inline void Add(int l,int r,int x,int y){ if(l>r)return; Update(1,0,n-1,l,r,x,0); Update(1,0,n-1,l,r,y,1);}int main(){ Read(n); for(i=1;i<=n;i++){ Read(x); if(x>=i)Add(0,x-i,x-i,-1),Add(x-i+1,n-i,i-x,1),Add(n-i+1,n-1,x-i+n,-1);else Add(0,n-i,i-x,1),Add(n-i+1,n-i+x,x-i+n,-1),Add(n-i+x+1,n-1,i-x-n,1); } GetAns(1,0,n-1); for(i=0;i<n;i++)if(d[i]<Ans)Ans=d[i],x=i; printf("%I64d %d\n",Ans,x); return 0;}
阅读全文
0 0
- codeforces819B Mister B and PR Shifts -- 线段树
- Codeforces 819 B. Mister B and PR Shifts
- Codeforces 819B. Mister B and PR Shifts
- #421 Div.2 D. Mister B and PR Shifts
- CodeForces-820D Mister B and PR Shifts
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts(瞎搞)
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts 思维,乱搞
- CF 820D Mister B and PR Shifts 差分(区间+k*(x-L)+b)
- Codeforces Round #421 Mister B and PR Shifts(暴力瞎搞)
- Codeforces Round 421 Div.2 D. Mister B and PR Shifts 思维 乱搞
- Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts
- Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts
- CodeForces 820D Mister B and PR Shifts(思维题)
- codeforces819bMister B and PR Shifts
- Mister B and Book Reading
- Mister B and Book Reading
- CF820A-Mister B and Book Reading
- CF820B-Mister B and Angle in Polygon
- React Native Animated实现的走马灯/轮播动画小示例
- maven Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core
- Web前端布局总体架构
- Amazon AWS 使用问题
- 剑指Offer面试题33把数组排成最小的数,面试题34丑数
- codeforces819B Mister B and PR Shifts -- 线段树
- 麦克风阵列波束形成
- Atom measures
- [AtCoder ARC077 .F] [字符串] [乱搞] SS
- python笔记
- Android屏幕适配的一个另类方法....
- Java Web实现统计网站用户访问量并持久化的问题
- NodeJS、npm安装配置,指定npm安装目录,可移植的node以及模块
- HDU5925-Coconuts