http://www.notonlysuccess.com/index.php/segment-tree-complete/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=5555;
int sum[maxn<<2];
int seq[maxn];
void pushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(r==l)
{
sum[rt]=0;
return;
}
int m=(r+l)>>1;
build(lson);
build(rson);
}
void update(int L,int d,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=d;
return;
}
int m=(r+l)>>1;
if(L<=m) update(L,d,lson);
else update(L,d,rson);
pushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
int ret=0;
if(L<=l&&r<=R){return sum[rt];}
int m=(l+r)>>1;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
memset(sum,0,sizeof(sum));
build(0,n-1,1);
memset(seq,0,sizeof(seq));
for(int i=0;i<n;i++)
{
scanf("%d",&seq[i]);
ans+=i-query(0,seq[i],0,n-1,1);
//cout<<ans<<" ";
//cout<<endl;
update(seq[i],1,0,n-1,1);
}
int cur=ans;
for(int i=0;i<n;i++)
{
cur+=n-seq[i]*2-1;
ans=min(ans,cur);
}
cout<<ans<<endl;
}
return 0;
}

最新文章

  1. 初中级Javascript程序员必修学习目录
  2. 2016开发一个app需要多少钱?app开发需要哪些成本-app开发问题汇总-广州达到信息
  3. flex+AS3编程规范
  4. .net发送邮件代码示例
  5. Spring 初学 1
  6. io系统
  7. 程序ajax请求公共组件app-jquery-http.js中url参数部分的项目应用
  8. HashMap如何工作 - Java
  9. C#判断画的图形是不是三角形
  10. ORACLE分页SQL语句(转载)
  11. Spring MVC的原理及配置详解
  12. bzoj5104: Fib数列
  13. [firefox] Scrapbook Plus的改进版Scrapbook X
  14. 【咸鱼教程】JsZip压缩与解压教程
  15. systemd学习笔记
  16. [2019BUAA软件工程]第0次个人作业
  17. CCTableView(一)
  18. 浏览器好玩的的 console.log
  19. Qt之操作数据库(SQLite)实例
  20. asp.net viewstate 数据大导致错误

热门文章

  1. linux 文件的权限说明
  2. hdu 5335 Walk Out 搜索+贪心
  3. [JZOJ 5895] [NOIP2018模拟10.5] 旅游 解题报告 (欧拉回路+最小生成树)
  4. [雅礼NOIP2018集训 day4]
  5. 关于如何让cell一直保持选中?
  6. JS循环 for while 全局/局部变量 短路
  7. Springboot如何利用http请求控制器
  8. css兼容性问题总结
  9. js Date() 时间函数处理 关于 toLocaleDateString()
  10. C语言实现简化的正则表达式