Codeforces Round #406 (Div. 1) C. Till I Collapse(主席树)
来源:互联网 发布:js正则表达式数字范围 编辑:程序博客网 时间:2024/06/05 10:31
原题链接:http://codeforces.com/contest/786/problem/C
直接用主席树暴力,分析复杂度的时候你会发现,复杂的是收敛于O(knlogn)的,k是一个常数(貌似2或者3?没细算),所以是可以这样暴力过去的。
#include<bits/stdc++.h>using namespace std;const int MAXN=1e5+5;const int M=MAXN*100;int n,q,tot;int a[MAXN];int T[MAXN],lson[M],rson[M],c[M];int build(int l,int r){int root=tot++;c[root]=0;if(l!=r){int mid=(l+r)>>1;lson[root]=build(l,mid);rson[root]=build(mid+1,r);}return root;}int update(int root,int pos,int val){int newroot=tot++,tmp=newroot;c[newroot]=c[root]+val;int l=1,r=n;while(l<r){int mid=(l+r)>>1;if(pos<=mid){lson[newroot]=tot++;rson[newroot]=rson[root];newroot=lson[newroot];root=lson[root];r=mid;}else{rson[newroot]=tot++;lson[newroot]=lson[root];newroot=rson[newroot];root=rson[root];l=mid+1;}c[newroot]=c[root]+val;}return tmp;}int query(int root,int l,int r,int k){int ret=0;if(l==r){if(k==0&&c[root])return l-1;return l;}int mid=(l+r)>>1;if(c[lson[root]]>k){return query(lson[root],l,mid,k);}else{return query(rson[root],mid+1,r,k-c[lson[root]]);}}int la[MAXN];int main(){//freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d",&n);tot=0;for(int i=1;i<=n;i++)scanf("%d",&a[i]);T[n+1]=build(1,n);for(int i=n;i>=1;i--){if(la[a[i]]){T[i]=update(T[i+1],la[a[i]],-1);T[i]=update(T[i],i,1);}else{T[i]=update(T[i+1],i,1);}la[a[i]]=i;}for(int i=1;i<=n;i++){int now=0,ans=0;while(now<n){now=query(T[now+1],1,n,i);ans++;}printf("%d ",ans);}printf("\n");return 0;}
0 0
- Codeforces Round #406 (Div. 1) C. Till I Collapse(主席树)
- Codeforces Round #406 (Div. 1) C. Till I Collapse(可持久化线段树)
- Codeforces 786C Till I Collapse[主席树][二分]
- Codeforces Round #406 (Div. 2) E. Till I Collapse
- Codeforces 787E Till I Collapse 主席树+二分
- Codeforces-786C-Till I Collapse(二分剪枝)
- codeforces 786c Till I Collapse
- Codeforces Round #433 (Div. 1, based on Olympiad of Metropolises) C. Boredom(主席树)
- 【题解】codeforces786C Till I Collapse
- Codeforces Round #429 (Div. 1) D. Destiny(主席树)
- Codeforces Round #225 (Div. 1)C(dfs+线段树)
- Codeforces Round #406 (Div. 2) C 博弈
- 线段树Codeforces Beta Round #99 (Div. 1)C
- Codeforces Round #225 (Div. 1) C 树状数组 || 线段树
- Codeforces Round #225 (Div. 1)-C. Propagating tree(线段树)
- Codeforces Round #316 (Div. 2) C. Replacement(线段树)
- Codeforces Round #207 (Div. 1) C. Compartments (贪心)
- Codeforces Round #272 (Div. 1)C(字符串DP)
- 登录错误多次密码锁定账户一定时间之数据库建表参考
- linux 中shell编程中的test用法
- 马拉松式学习与技术人员的成长性
- hibernate文件配置出现Repeated column in mapping for entity的Mapping异常处理
- LeetCode:Best Time to Buy and Sell Stock
- Codeforces Round #406 (Div. 1) C. Till I Collapse(主席树)
- Java拾遗2:文件传输基础——Java IO流
- 《30天自制操作系统》学习笔记Day1
- Immutable bitmap crash error
- Android搭建HTTP服务,作为MediaPlayer的播放源来播放音频
- 2017湖南多校第五场-A(1043): Biorhythms
- 正则匹配之匹配网页img
- java xml dom4j 创建 修改 解析
- 右键新建没有Word怎么办?右键新建添加Word方法!