Codeforces 831-E.Cards Sorting(线段树)
来源:互联网 发布:淘宝平铺拍摄布光 编辑:程序博客网 时间:2024/06/01 13:06
记录一个菜逼的成长。。
题目链接
题目大意:
给你一个
循环遍历数组,当当前数是序列里的最小值时删除这个数,问需要遍历多少次才能使数组为空。
Solve
用线段树维护区间里没有被删除的数的数量。(树状数组也行,个人习惯线段树。。
用数组记录每个数出现的位置。
因为删除肯定从小到大删除,所以从小到大遍历数。
从当前数中找到第一个比前一个数的位置大的位置,然后从这个位置开始去删除这个数的所有位置。
这个复杂度应该是
#include <bits/stdc++.h>using namespace std;#define ALL(v) (v).begin(),(v).end()#define pb push_back#define fi first#define se second#define lson t<<1,l,mid#define rson t<<1|1,mid+1,r#define seglen(t) (node[t].r-node[t].l+1)#define lowbit(x) (x)&(-x)typedef long long LL;typedef pair<int,int> PII;const int maxn = 100001;vector<int>a[maxn];int n;stack<int>st;struct Node{ int l,r,sum;}node[maxn<<2];void pushup(int t){ node[t].sum = node[t<<1].sum + node[t<<1|1].sum;}void build(int t,int l,int r){ node[t].l = l; node[t].r = r; if(l == r){ node[t].sum = 0; return ; } int mid = (l + r) >> 1; build(lson); build(rson); pushup(t);}void update(int t,int ind,int v){ if(node[t].l == node[t].r && node[t].l == ind){ node[t].sum += v; return ; } int mid = (node[t].l + node[t].r) >> 1; if(ind <= mid)update(t<<1,ind,v); else update(t<<1|1,ind,v); pushup(t);}int query(int t,int l,int r){ if(l <= node[t].l && r >= node[t].r){ return node[t].sum; } int ret = 0; int mid = (node[t].l + node[t].r) >> 1; if(l <= mid)ret += query(t<<1,l,r); if(r > mid)ret += query(t<<1|1,l,r); pushup(t); return ret;}LL cal(int l,int r){ if(l > r)return query(1,l,n) + query(1,1,r); return query(1,l,r);}int main(){ while(~scanf("%d",&n)){ build(1,1,n); for( int i = 1,x; i <= n; i++ ){ scanf("%d",&x);a[x].pb(i); update(1,i,1); } LL ans = 0; int pre = 1; for( int i = 1; i < maxn; i++ ){ if(a[i].size() == 0)continue; int pos = lower_bound(ALL(a[i]),pre) - a[i].begin(); //说明所有数的位置都比前一个数的位置小,所以从第一个开始删除 if(pos == a[i].size())pos = 0; for( int j = 0; j < a[i].size(); j++ ){ ans += cal(pre,a[i][pos]); update(1,a[i][pos],-1); pre = a[i][pos]+1; pos = (pos + 1) % a[i].size(); } } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- Codeforces 831-E.Cards Sorting(线段树)
- codeforces 831 E Cards Sorting(bit+stl)
- Codeforces-831E Cards Sorting(树状数组)
- Codeforces #831E: Cards Sorting 题解
- Codeforces Round #424 (Div. 2, )-树状数组|线段树-E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting 线段树/数据结构瞎搞/模拟
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- [模拟][线段树]Codeforces Round #424. B Cards Sorting
- Codeforces-830B:Cards Sorting(树状数组+线段树)
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting
- codeforces830B Cards Sorting -- 线段树
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- codeforces 830B. Cards Sorting
- Codeforces B. Cards Sorting 【瞎搞】
- Codeforces Round #424 (Div. 2) E. Cards Sorting(思维 树状数组)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E Cards Sorting
- CodeForces - 292E 线段树
- Spring异步方法注解@Async
- 2017华为优招面试总结——生命的意义,在于永恒不变的练习
- android studio 在不同fragment之间发送数据如何实现
- 05.24 nginx负载均衡
- 蛇形填数(二人)变形之三角形
- Codeforces 831-E.Cards Sorting(线段树)
- 自定义VS程序异常处理及调试Dump文件(一)
- java多线程之内存可见性-synchronized、volatile
- 微信小程序踩坑总结
- Java集合处理: 两个集合去重复、或者交集
- this call apply的认识
- Linux系统同步时间
- day03学习代码
- 解决SpringMvc前台传Date类型到后台报400错误问题