题意:求一个排列的逆序数。

#include<cstdio>
#include<iostream>
#include<algorithm>
const int maxn=1000005;
int maze[maxn],temp[maxn];
long long ans;
void union_set(int l,int mid,int r)
{
int i=l,j=mid+1,k=0;
while(i<=mid&&j<=r){
if(maze[i]>maze[j]){
ans+=mid+1-i;
temp[k++]=maze[j++];
}
else temp[k++]=maze[i++];
}
while(i<=mid)temp[k++]=maze[i++];
while(j<=r) temp[k++]=maze[j++];
for(int i=0;i<k;i++)
maze[l+i]=temp[i]; }
void merge_sort(int l,int r)
{
if(l==r)return ;
int mid=(l+r)/2;
merge_sort(l,mid);
merge_sort(mid+1,r);
union_set(l,mid,r);
}
int main ()
{
int n;
while(~scanf("%d",&n))
{
ans=0;
for(int i=0;i<n;i++)
scanf("%d",&maze[i]);
merge_sort(0, n-1);
printf("%lld\n",ans);
}
return 0;
}

HDOJ3743<分治>的更多相关文章

  1. [bzoj2152][聪聪和可可] (点分治+概率)

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  2. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  3. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  4. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  5. BZOJ 3262 陌上花开 ——CDQ分治

    [题目分析] 多维问题,我们可以按照其中一维排序,然后把这一维抽象的改为时间. 然后剩下两维,就像简单题那样,排序一维,树状数组一维,按照时间分治即可. 挺有套路的一种算法. 时间的抽象很巧妙. 同种 ...

  6. BZOJ 1176 [Balkan2007]Mokia ——CDQ分治

    [题目分析] 同BZOJ2683,只需要提前处理s对结果的影响即可. CDQ的思路还是很清晰的. 排序解决一维, 分治时间, 树状数组解决一维. 复杂度是两个log [代码] #include < ...

  7. BZOJ 2683 简单题 ——CDQ分治

    [题目分析] 感觉CDQ分治和整体二分有着很本质的区别. 为什么还有许多人把他们放在一起,也许是因为代码很像吧. CDQ分治最重要的是加入了时间对答案的影响,x,y,t三个条件. 排序解决了x ,分治 ...

  8. HDU5977 Garden of Eden(树的点分治)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...

  9. Tsinsen A1493 城市规划(DP + CDQ分治 + NTT)

    题目 Source http://www.tsinsen.com/A1493 Description 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了. 刚才说过, 阿狸的国家有n个城市, 现在 ...

随机推荐

  1. jquery收集--php收集所有post数据

    $model = D('Account'); $data = $model->create(); jquery收集数据  sendinvite.serialize() function init ...

  2. tftp常用命令

    root@hbg:/# tftpBusyBox v1.22.1 (2015-12-18 15:33:52 CST) multi-call binary. Usage: tftp [OPTIONS] H ...

  3. bullet_01

    #include <btBulletDynamicsCommon.h> #include <osgViewer/Viewer> #include <map> #in ...

  4. Kettle中spoon.sh在使用时报错

    报错信息: Attempting to load ESAPI.properties via file I/O. Attempting to load ESAPI.properties as resou ...

  5. LeetCode OJ 121. Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. poj 2299 Ultra-QuickSort 逆序对模版题

    用树状数组求逆序数 唯一的坑点就是sum要用long long存 直接贴代码了 以后忘了还能直接看 2333…… PS:和hdu3743代码是一样的,因为两个都是逆序对模版题…… #include&l ...

  7. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  8. chapter11_3 字符串缓冲

    逐行地读取一个文件,典型的代码是: local buff= " " for line in io.lines() do buff = buff .. line .. "\ ...

  9. a标签中使用img后的高度多了几个像素

    a元素下有一个匿名文本,这个文本外有一个匿名行级盒子,它有的默认vertical-align是baseline的,而且往往因为上文line-height的影响,使它有个line-height,从而使其 ...

  10. web开发——水到渠成

    1.tomcat中设置编码格式:      打开server.xml,添加下面红色框框里面的内容.