Codeforces Round #424 E. Cards Sorting 线段树/数据结构瞎搞/模拟
来源:互联网 发布:白夜追凶剧情分析知乎 编辑:程序博客网 时间:2024/06/06 21:06
第一眼感觉是个水题
过程确实也无脑,但是细节麻烦。。。
就是循环找最小值,删除,算步数而已
不过转移位置的计算我试了好几种方法,才写出一个对的。。
提交时一度抱着求求你让我过吧这种心态(
#include<bits/stdc++.h>#include<stdio.h>#include<algorithm>#include<queue>#include<string.h>#include<iostream>#include<math.h>#include<set>#include<map>#include<vector>#include<iomanip>using namespace std;#define ll long long#define pb push_back#define FOR(a) for(int i=1;i<=a;i++)const int inf=0x3f3f3f3f;const int maxn=1e5+9; const int mod=1e9+7;int arr[maxn];int now;struct NODE{int minn;int minid;int cnt;}ST[maxn<<2];void pushup(int rt){ST[rt].cnt=ST[rt<<1].cnt+ST[rt<<1|1].cnt;ST[rt].minn=min(ST[rt<<1].minn,ST[rt<<1|1].minn);if(ST[rt<<1].minn<=ST[rt<<1|1].minn)ST[rt].minid=ST[rt<<1].minid;else ST[rt].minid=ST[rt<<1|1].minid;}void build(int l,int r,int rt){if(l==r){ST[rt].minn=arr[l];ST[rt].minid=l;ST[rt].cnt=1;return;}int m=l+r>>1;build(l,m,rt<<1);build(m+1,r,rt<<1|1);pushup(rt);}void update(int a,int l,int r,int rt){if(l==r){ST[rt].minn=inf;ST[rt].cnt=0;return;}int m=l+r>>1;if(a<=m)update(a,l,m,rt<<1);else update(a,m+1,r,rt<<1|1);pushup(rt);}int query(int a,int b,int l,int r,int rt){if(a<=l&&b>=r)return ST[rt].cnt;int m=l+r>>1;int ret=0;if(a<=m)ret+=query(a,b,l,m,rt<<1);if(b>m)ret+=query(a,b,m+1,r,rt<<1|1);return ret;}int query2(int a,int b,int l,int r,int rt){if(a<=l&&b>=r && ST[rt].minn==ST[1].minn)return ST[rt].minid;else if(a<=l && b>=r)return -1;int m=l+r>>1;int a1=-1,a2=-1;if(a<=m)a1=query2(a,b,l,m,rt<<1);if(b>m)a2=query2(a,b,m+1,r,rt<<1|1);if(a1==-1 && a2==-1)return -1;else if(a1!=-1)return a1;else return a2;}int main(){int n;scanf("%d",&n);FOR(n)scanf("%d",&arr[i]);build(1,n,1);now=0;ll ans=0;for(int i=1;i<=n;i++){//删n轮//int nex=ST[1].minid;int nex=query2(now,n,1,n,1);//if(now==10)cout<<"www"<<nex<<endl;if(nex==-1)nex=query2(1,now,1,n,1);//if(now==10)cout<<"qqq"<<nex<<endl;//cout<<now<<" "<<nex<<endl; if(nex>now){ans+=query(now,nex,1,n,1);now=nex;update(nex,1,n,1);}else{ans+=query(now,n,1,n,1)+query(1,nex,1,n,1);now=nex;update(nex,1,n,1);}}printf("%lld\n",ans);}
阅读全文
0 0
- Codeforces Round #424 E. Cards Sorting 线段树/数据结构瞎搞/模拟
- [模拟][线段树]Codeforces Round #424. B Cards Sorting
- Codeforces Round #424 (Div. 2, )-树状数组|线段树-E. Cards Sorting
- Codeforces Round #424 Div2 E. Cards Sorting
- Codeforces Round #424 E. Cards Sorting
- Codeforces 831-E.Cards Sorting(线段树)
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- Codeforces B. Cards Sorting 【瞎搞】
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- Codeforces Round #424 (Div. 2) E. Cards Sorting(思维 树状数组)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E Cards Sorting
- Codeforces Round #424 (Div. 2)C. Jury Marks 暴力 D. Office Keys 二分 E. Cards Sorting 树状数组
- Codeforces-830B:Cards Sorting(树状数组+线段树)
- codeforces 831 E Cards Sorting(bit+stl)
- Codeforces-831E Cards Sorting(树状数组)
- Codeforces #831E: Cards Sorting 题解
- codeforces830B Cards Sorting -- 线段树
- Codeforces Round #441 D. Sorting the Coins(线段树)
- Arraylist forEach,removeIf方法的使用
- KD-tree的原理以及构建与查询操作的python实现
- Centos6、7安装Java(jdk+mysql+tomcat)环境傻瓜式教程
- 新文档
- 死锁
- Codeforces Round #424 E. Cards Sorting 线段树/数据结构瞎搞/模拟
- 基础编程题目集-厘米换算英尺英寸
- levaldb写入数据(6)
- java多线程中wait()和sleep()方法的区别
- poj 3320
- C++实现第一个Windows程序设计Hello World
- Memcache的使用与安全
- 《计算机操作系统原理—linux实例分析》_肖竞华_第二章_知识点
- [POJ 1062]昂贵的聘礼