AC日记——Cards Sorting codeforces 830B
思路:
线段树;
代码:
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define maxn 100005
- #define INF 0x3f3f3f3f
- #define maxtree maxn<<2
- int n,ai[maxn],val[maxtree],L[maxtree],R[maxtree],Min[maxtree];
- int P[maxtree],mid[maxtree];
- inline void in(int &now)
- {
- char Cget=getchar();now=;
- while(Cget>''||Cget<'') Cget=getchar();
- while(Cget>=''&&Cget<='')
- {
- now=now*+Cget-'';
- Cget=getchar();
- }
- }
- void updata(int now)
- {
- val[now]=val[now<<]+val[now<<|];
- Min[now]=min(Min[now<<],Min[now<<|]);
- if(Min[now]==Min[now<<|]) P[now]=P[now<<|];
- else P[now]=P[now<<];
- }
- void build(int now,int l,int r)
- {
- L[now]=l,R[now]=r;
- if(l==r)
- {
- Min[now]=ai[l],P[now]=l,val[now]=;
- return;
- }
- mid[now]=l+r>>;
- build(now<<,l,mid[now]);
- build(now<<|,mid[now]+,r);
- updata(now);
- }
- void change(int now,int to)
- {
- if(L[now]==R[now])
- {
- val[now]=,Min[now]=INF,P[now]=;
- return;
- }
- if(to<=mid[now]) change(now<<,to);
- else change(now<<|,to);
- updata(now);
- }
- int query(int now,int l,int r)
- {
- if(L[now]>=l&&R[now]<=r) return val[now];
- int res=;
- if(l<=mid[now]) res+=query(now<<,l,r);
- if(r>mid[now]) res+=query(now<<|,l,r);
- return res;
- }
- int get(int now,int l,int r)
- {
- if(L[now]>=l&&R[now]<=r) return P[now];
- int tmp1=,tmp2=;
- if(l<=mid[now]) tmp1=get(now<<,l,r);
- if(r>mid[now]) tmp2=get(now<<|,l,r);
- if(tmp2&&tmp1)
- {
- if(ai[tmp1]<ai[tmp2]) return tmp1;
- else return tmp2;
- }
- if(tmp2) return tmp2;
- if(tmp1) return tmp1;
- return ;
- }
- int main()
- {
- in(n);
- for(int i=;i<=n;i++) in(ai[i]);
- int l=,r=n;while(l<r) swap(ai[l],ai[r]),l++,r--;
- build(,,n);
- int now=n,tmp1,tmp2,tmp;
- long long ans=;
- for(int i=;i<=n;i++)
- {
- tmp1=,tmp2=,tmp1=get(,,now);
- if(now<n) tmp2=get(,now+,n);
- if(tmp1&&tmp2)
- {
- if(ai[tmp2]<ai[tmp1]) tmp=tmp2;
- else tmp=tmp1;
- }
- else if(tmp1) tmp=tmp1;
- else tmp=tmp2;
- if(tmp<=now) ans+=query(,tmp,now);
- else ans+=query(,,now)+query(,tmp,n);
- change(,tmp),now=tmp;
- }
- cout<<ans;
- return ;
- }
AC日记——Cards Sorting codeforces 830B的更多相关文章
- AC日记——Card Game codeforces 808f
F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...
- AC日记——Success Rate codeforces 807c
Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——T-Shirt Hunt codeforces 807b
T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——Magazine Ad codeforces 803d
803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...
- AC日记——Broken BST codeforces 797d
D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- AC日记——Maximal GCD codeforces 803c
803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...
- AC日记——Vicious Keyboard codeforces 801a
801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...
- AC日记——Valued Keys codeforces 801B
801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...
随机推荐
- pycrypto 安装
https://www.dlitz.net/software/pycrypto/ 下载pycrypto-2.6.1.tar.gz,解压后 python setup.py build python se ...
- Codeforces 894.E Ralph and Mushrooms
E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...
- 金牌架构师:我们是这样设计APP数据统计产品的
前言:近期,智能大数据服务商“个推”推出了应用统计产品“个数”,今天我们就和大家来谈一谈个数实时统计与AI数据智能平台整合架构设计. 很多人可能好奇,拥有数百亿SDK的个推,专注消息推送服务多年,现在 ...
- ubuntu如何杀死进程
一.得到所有进程 先用命令查询出所有进程 ps -ef 二.杀死进程 我们使用ps -ef命令之后,就会得到一些列进程信息,有进程pid什么的,如果你要杀死莫个进程的话,直接使用命令 kill ...
- 使用cron命令配置定时任务(cron jobs)
原文 http://www.cnblogs.com/end/archive/2012/02/21/2361741.html 开机就启动cron进程的设置命令:chkconfig --add crond ...
- synchronized 加锁Integer对象(数据重复)详解
场景描述:多线程输出1到100,对静态Integer对象加锁,synchronized代码块中操作Integer对象,发生线程安全问题(数据重复) 代码: public class MyRunnabl ...
- zoj 2314 Reactor Cooling (无源汇上下界可行流)
Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...
- 爬虫实战--使用Selenium模拟浏览器抓取淘宝商品美食信息
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.common.exce ...
- 基于bootstrap物资管理系统后台模板——后台
链接:http://pan.baidu.com/s/1geKwVMN 密码:0utl
- CSS 中 nth-child 和 nth-of-type 的区别
假设有如下代码结构,想要查找 Piggy 那个 p <section> <h1>Words</h1> <p>Little</p> <p ...