Cards Sorting

思路:

  线段树;

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. #define maxn 100005
  7. #define INF 0x3f3f3f3f
  8. #define maxtree maxn<<2
  9. int n,ai[maxn],val[maxtree],L[maxtree],R[maxtree],Min[maxtree];
  10. int P[maxtree],mid[maxtree];
  11. inline void in(int &now)
  12. {
  13. char Cget=getchar();now=;
  14. while(Cget>''||Cget<'') Cget=getchar();
  15. while(Cget>=''&&Cget<='')
  16. {
  17. now=now*+Cget-'';
  18. Cget=getchar();
  19. }
  20. }
  21. void updata(int now)
  22. {
  23. val[now]=val[now<<]+val[now<<|];
  24. Min[now]=min(Min[now<<],Min[now<<|]);
  25. if(Min[now]==Min[now<<|]) P[now]=P[now<<|];
  26. else P[now]=P[now<<];
  27. }
  28. void build(int now,int l,int r)
  29. {
  30. L[now]=l,R[now]=r;
  31. if(l==r)
  32. {
  33. Min[now]=ai[l],P[now]=l,val[now]=;
  34. return;
  35. }
  36. mid[now]=l+r>>;
  37. build(now<<,l,mid[now]);
  38. build(now<<|,mid[now]+,r);
  39. updata(now);
  40. }
  41. void change(int now,int to)
  42. {
  43. if(L[now]==R[now])
  44. {
  45. val[now]=,Min[now]=INF,P[now]=;
  46. return;
  47. }
  48. if(to<=mid[now]) change(now<<,to);
  49. else change(now<<|,to);
  50. updata(now);
  51. }
  52. int query(int now,int l,int r)
  53. {
  54. if(L[now]>=l&&R[now]<=r) return val[now];
  55. int res=;
  56. if(l<=mid[now]) res+=query(now<<,l,r);
  57. if(r>mid[now]) res+=query(now<<|,l,r);
  58. return res;
  59. }
  60. int get(int now,int l,int r)
  61. {
  62. if(L[now]>=l&&R[now]<=r) return P[now];
  63. int tmp1=,tmp2=;
  64. if(l<=mid[now]) tmp1=get(now<<,l,r);
  65. if(r>mid[now]) tmp2=get(now<<|,l,r);
  66. if(tmp2&&tmp1)
  67. {
  68. if(ai[tmp1]<ai[tmp2]) return tmp1;
  69. else return tmp2;
  70. }
  71. if(tmp2) return tmp2;
  72. if(tmp1) return tmp1;
  73. return ;
  74. }
  75. int main()
  76. {
  77. in(n);
  78. for(int i=;i<=n;i++) in(ai[i]);
  79. int l=,r=n;while(l<r) swap(ai[l],ai[r]),l++,r--;
  80. build(,,n);
  81. int now=n,tmp1,tmp2,tmp;
  82. long long ans=;
  83. for(int i=;i<=n;i++)
  84. {
  85. tmp1=,tmp2=,tmp1=get(,,now);
  86. if(now<n) tmp2=get(,now+,n);
  87. if(tmp1&&tmp2)
  88. {
  89. if(ai[tmp2]<ai[tmp1]) tmp=tmp2;
  90. else tmp=tmp1;
  91. }
  92. else if(tmp1) tmp=tmp1;
  93. else tmp=tmp2;
  94. if(tmp<=now) ans+=query(,tmp,now);
  95. else ans+=query(,,now)+query(,tmp,n);
  96. change(,tmp),now=tmp;
  97. }
  98. cout<<ans;
  99. return ;
  100. }

AC日记——Cards Sorting codeforces 830B的更多相关文章

  1. AC日记——Card Game codeforces 808f

    F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...

  2. AC日记——Success Rate codeforces 807c

    Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  3. AC日记——T-Shirt Hunt codeforces 807b

    T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  4. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  5. AC日记——Broken BST codeforces 797d

    D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...

  6. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  7. AC日记——Maximal GCD codeforces 803c

    803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...

  8. AC日记——Vicious Keyboard codeforces 801a

    801A - Vicious Keyboard 思路: 水题: 来,上代码: #include <cstdio> #include <cstring> #include < ...

  9. AC日记——Valued Keys codeforces 801B

    801B - Valued Keys 思路: 水题... 来,上代码: #include <cstdio> #include <cstring> #include <io ...

随机推荐

  1. pycrypto 安装

    https://www.dlitz.net/software/pycrypto/ 下载pycrypto-2.6.1.tar.gz,解压后 python setup.py build python se ...

  2. 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 ...

  3. 金牌架构师:我们是这样设计APP数据统计产品的

    前言:近期,智能大数据服务商“个推”推出了应用统计产品“个数”,今天我们就和大家来谈一谈个数实时统计与AI数据智能平台整合架构设计. 很多人可能好奇,拥有数百亿SDK的个推,专注消息推送服务多年,现在 ...

  4. ubuntu如何杀死进程

    一.得到所有进程 先用命令查询出所有进程 ps -ef 二.杀死进程 我们使用ps -ef命令之后,就会得到一些列进程信息,有进程pid什么的,如果你要杀死莫个进程的话,直接使用命令   kill   ...

  5. 使用cron命令配置定时任务(cron jobs)

    原文 http://www.cnblogs.com/end/archive/2012/02/21/2361741.html 开机就启动cron进程的设置命令:chkconfig --add crond ...

  6. synchronized 加锁Integer对象(数据重复)详解

    场景描述:多线程输出1到100,对静态Integer对象加锁,synchronized代码块中操作Integer对象,发生线程安全问题(数据重复) 代码: public class MyRunnabl ...

  7. zoj 2314 Reactor Cooling (无源汇上下界可行流)

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

  8. 爬虫实战--使用Selenium模拟浏览器抓取淘宝商品美食信息

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.common.exce ...

  9. 基于bootstrap物资管理系统后台模板——后台

    链接:http://pan.baidu.com/s/1geKwVMN 密码:0utl

  10. CSS 中 nth-child 和 nth-of-type 的区别

    假设有如下代码结构,想要查找 Piggy 那个 p <section> <h1>Words</h1> <p>Little</p> <p ...