思路:我们只需坚守一个原则,本来就在左边的坚决不把它换到右边。也就是相邻的两个数,左边小,右边大,那么就不调换。这样对每个数,只要统计左边比它大的数的个数。可以从后面开始用树状数组统计比它小的数的个数是一样的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define Maxn 1000010
#define lowbit(x) (x&(-x))
using namespace std;
int C[Maxn],num[Maxn],n,r[Maxn];
void init()
{
memset(C,,sizeof(C));
}
int cmp(int a,int b)
{
return num[a]<num[b];
}
int Sum(int pos)
{
int sum=;
while(pos>)
{
sum+=C[pos];
pos-=lowbit(pos);
}
return sum;
}
void update(int pos)
{
while(pos<=n)
{
C[pos]++;
pos+=lowbit(pos);
}
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=;i<=n;i++)
scanf("%d",num+i);
for(i=;i<=n;i++)
r[i]=i;
sort(r+,r+n+,cmp);
int cnt=;
int temp=num[r[]];
num[r[]]=++cnt;
for(i=;i<=n;i++)
{
if(num[r[i]]!=temp) temp=num[r[i]],num[r[i]]=++cnt;
else num[r[i]]=temp;
}
__int64 ans=;
for(i=n;i>=;i--)
{
ans+=(__int64)Sum(num[i]);
update(num[i]);
}
printf("%I64d\n",ans);
}
return ;
}

最新文章

  1. Ubuntu13.04安装历险记--Mono,Nginx,Asp.Net一个都不能少
  2. css3 实现逐帧动画
  3. java日期类型转换总结date timestamp calendar string
  4. Java compiler level does not match the version of the installed Java project facet. springmvc1 和 Target runtime Apache Tomcat v7.0 is not defined.
  5. es增量自定义更新的脚本
  6. 为什么SSL证书流量暴增?
  7. 将Sublime Text3添加到右键菜单中
  8. OpenCV(7)-图像直方图
  9. dedeCMS修改文章更新发布时间问题
  10. Netty4具体解释二:开发第一个Netty应用程序
  11. oracle创建主键序列和在ibatis中应用
  12. git常见操作和常见错误
  13. C++、Java语法差异对照表
  14. 面向对象内置方法之--__str__、__call__、__del__
  15. (转)JMeter学习逻辑控制器
  16. PCF学习知识
  17. 爬虫3 requests基础之下载图片用content(二进制内容)
  18. 如何在phpstorm中查看yaf框架源码
  19. 强制windows系统重启at命令
  20. e620. Activating a Keystroke When Any Component in the Window Has Focus

热门文章

  1. JNI调用测试
  2. 绑定线程到特定CPU处理器
  3. VC中监测程序运行时间(二)-毫秒级
  4. 开源的读取Excel文件组件-ExcelDataReader
  5. 超轻量级spring模板方案
  6. 最长不下降子序列nlogn算法详解
  7. idea 搭建java项目
  8. Python的包管理工具Pip
  9. IOS 日期选择
  10. mapreduce2运行流程