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 ...
随机推荐
- Codeforces 939.E Maximize!
E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- poj1006 生理周期
生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138947 Accepted: 44597 Descripti ...
- VLFeat在matlab和vs中安装
转:http://blog.csdn.net/u011718701/article/details/51452011 博主最近用vlfeat库做课题,网上搜索使用方法,一大片都会告诉你说:run(/v ...
- 「电脑应用」在mac上使用aria2
Chrome 一. 需要工具: Chrome浏览器,Aria2GUI,Chrome里BaiduExporter插件. 首先先明确一件事情,平时生活中使用的所有工具都建议到官方下载,此处用到的几样提供给 ...
- python 获取文件md5
def GetFileMd5(filename): if not os.path.isfile(filename): return myhash = hashlib.md5() f = file(fi ...
- 前端PHP入门-003-echo离不开它,注释看人品
不要自以为是,瞪着双眼看是没有用的!因为你不是天才! 永远不要自欺欺人,写不出来代码就是不会. 不要问为什么?我头疼! 我接触的人当中,以为自己已经很很很牛X了,总是问为什么这么写? [心中吐槽]:我 ...
- Tensorboard教程:高维向量可视化
Tensorflow高维向量可视化 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 强烈推荐Tensorflow实战Google深度学习框架 实验平台: Tensorflow1.4. ...
- HDU 2298 三分
斜抛从(0,0)到(x,y),问其角度. 首先观察下就知道抛物线上横坐标为x的点与给定的点的距离与角度关系并不是线性的,当角度大于一定值时可能会时距离单调递减,所以先三分求个角度范围,保证其点一定在抛 ...
- new Date('2014/04/30') 和 new Date('2014-04-30') 的区别
new Date('2014/04/30') Wed Apr 30 2014 00:00:00 GMT+0800 (中国标准时间) new Date('2014-04-30'); Wed Apr 30 ...
- IP判断 (字符串处理)
关于IP合法性判断的题目,每个oj上的约束条件不尽相同,我就根据自己做过的题目吧所有的约束条件汇总到一块,到时候做题时只需要把多余的越是条件删掉即可 题目描述: 对于IP我们总会有一定的规定,合法的I ...