博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF1175F The Number of Subpermutations
阅读量:4702 次
发布时间:2019-06-09

本文共 1391 字,大约阅读时间需要 4 分钟。

考虑算出\(nxt_i\)表示以\(i\)为左端点,不出现重复的数最远可以到的位置

然后枚举左端点\(l\),枚举右端点\(r\),如果当前区间内的最大值\(mx>r-l+1\),我们就将右端点移到\(mx+l-1\)的位置上。

由于答案是\(O(n)\)的,所以总复杂度就是\(O(n\log n)\)

代码:

#include
#include
#include
#include
#include
#include
using namespace std;#define rg registervoid read(int &x){ char ch;bool ok; for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1; for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;}const int maxn=3e5+10;int n,a[maxn],ans,nxt[maxn],mx[maxn*4];bool vis[maxn];void change(int x,int l,int r,int a,int b){ if(l==r)return mx[x]=b,void();int mid=(l+r)>>1; if(a<=mid)change(x<<1,l,mid,a,b);else change(x<<1|1,mid+1,r,a,b); mx[x]=max(mx[x<<1],mx[x<<1|1]);}int get(int x,int l,int r,int a,int b){ if(a<=l&&b>=r)return mx[x];int mid=(l+r)>>1,ans=0; if(a<=mid)ans=max(get(x<<1,l,mid,a,b),ans); if(b>mid)ans=max(ans,get(x<<1|1,mid+1,r,a,b)); return ans;}int main(){ read(n);int l=1; for(rg int i=1;i<=n;i++)read(a[i]),nxt[i]=n,change(1,1,n,i,a[i]); for(rg int i=1;i<=n;i++) if(!vis[a[i]])vis[a[i]]=1; else { while(a[l]!=a[i])vis[a[l]]=0,nxt[l]=i-1,l++; nxt[l]=i-1,l++; } for(rg int i=1;i<=n;i++){ for(rg int j=i;j<=nxt[i];j++){ int now=get(1,1,n,i,j); if(now>j-i+1)j+=(now-(j-i+1))-1;else ans++; } } printf("%d\n",ans);}

转载于:https://www.cnblogs.com/lcxer/p/11505753.html

你可能感兴趣的文章
POJ 1273 Drainage Ditches
查看>>
POJ 1733 Parity game
查看>>
js晋级篇——前端内存泄漏探讨
查看>>
Drupal 出错的解决办法
查看>>
js正则
查看>>
thrift源码阅读笔记
查看>>
iOS UI基础-4.0应用程序管理
查看>>
在CentOS 6.4 x86_32中使用Rhythmbox听MP3
查看>>
sys模块
查看>>
BZOJ3073 : [Pa2011]Journeys
查看>>
BZOJ2213 : [Poi2011]Difference
查看>>
聊聊javascript的事件
查看>>
对象基础
查看>>
【MySQL比知必会】第九章 使用正则表达式进行搜索
查看>>
查看Oracle数据库名和实例名的命令
查看>>
20155302 2016-2017-2 《Java程序设计》第4周总结
查看>>
java Html 转 PDF
查看>>
C++ 标准库 permutation
查看>>
关于PCB开窗
查看>>
【蓝桥杯单片机07】彻底理解51单片机的中断系统
查看>>