H - Frosh Week

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion, such as their height, their birth date, or their student number. This rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required.
 

Input

The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. The following n lines each contain one integer, the student number of each student on the team. No student number will appear more than once. 
 

Output

Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number. 
 

Sample Input

3
3
1
2
 

Sample Output

2
 
这题的意思是第一行一个数,说明有几个数,数量不超过一百万个。然后每一行一个数,这是有顺序的,要排成递增顺序,只能相邻的两两交换,问需要交换几次
 
//vc 6这编译器真的坑了我很多次,好烦啊,长整型long long用不了,只能用 __int64 ,这题int存答案会溢出。
网上很多都是用树状数组做的,但是归并排序不是正好解决这问题的么,很简单。。。

 #include <iostream>
#include <cstdio>
using namespace std; const int N=;
int num[N];
int temp[N];
long long ans; void merge(int left,int mid,int right)
{
int i=left,j=mid+;
int p=;
while (i<=mid&&j<=right)
{
if (num[i]<=num[j]) temp[p++]=num[i++];
else
{
ans+=mid-i+; //前半段剩下的数的个数都多了一个逆序数
temp[p++]=num[j++];
}
}
while (i<=mid) temp[p++]=num[i++];
while (j<=right) temp[p++]=num[j++];
j=left;
for (i=;i<p;i++)
num[j++]=temp[i];
} void mergesort(int left,int right)
{
int mid;
if (left<right)
{
mid=(left+right)/;
mergesort(left,mid); //左边还可以分的话,归并左边
mergesort(mid+,right); //右边可以分的话,归并右边
merge(left,mid,right); //合并两个已经并好的
}
} int main()
{
int n,i;
while (scanf("%d",&n)!=EOF)
{
ans=;
for (i=;i<n;i++)
scanf("%d",&num[i]);
mergesort(,n-);
printf("%lld\n",ans);
}
return ;
}

Frosh Week(归并排序求逆序数)的更多相关文章

  1. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  2. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  3. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  4. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  5. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  6. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  7. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  8. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

  9. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

随机推荐

  1. java 开发中的debug

    mysql 的 级联删除与级联更新 create table student( id int, departmentId int, foreign key(departmentId) referenc ...

  2. lucene: 索引建立完后无法查看索引文件中的数据

    索引建立时      1.对原有索引文件进行建立,是可以访问索引文件中的数据的      2.建立新索引文件,必须等建立完毕后,才可以访问,新建立的文件如果没有建立完是不可以被访问的     如果想建 ...

  3. Jenkins插件开发资料

    原文地址:http://www.ciandcd.com/?p=181 Jenkins plugin 开发: Document http://hudson-ci.org/docs/index.html ...

  4. 集成ueditor工具

    摘要: 摘要: 版权声明:本文为博主原创文章,未经博主允许不得转载. UEditor 是百度的一套开源的在线HTML编辑器. 第一步:去官网看官网文档,了解这个工具如何使用以及下载,本人下载的是1.4 ...

  5. eclipse进行远程调试教程,轻松搞定生产环境问题

    首先你本地Eclipse上要有和部署在远程服务器一至的项目,否则debug的时候会出现代码行错位,难以达到debug的效果.例如:如果你本地的代 码加了行,修改了,或减了一行.而远程服务器上的项目没有 ...

  6. PHP输出xls文件

    PHP输出xls文件 标签: phpexcelxmlcsvborderheader 2008-11-17 09:33 2611人阅读 评论(0) 收藏 举报  分类: WebDev(9)  版权声明: ...

  7. HashMap之原理及死锁

    一.HashMap原理 1.HashMap的本质就是数组和链表.table是一个entry数组,每一个数组元素保存一个Entry节点,而Entry节点内部又连接着同样key的下一个Entry节点,就构 ...

  8. memcached 经常使用命令最全总结大全

    1.首先.总结一下memcached的一些基本设置 -p 监听的port -l 连接的IP地址, 默认是本机 -d start 启动memcached服务 -d restart 重起memcached ...

  9. mysql日期函数 和sql语句扩展information_schema show processlist;

    SELECT NOW(); SELECT CURDATE(); SELECT YEAR(NOW()) SELECT CONCAT("mysqldump ","-uroot ...

  10. Eureka 2.0 闭源--选择Consul???

    在上个月我们知道 Eureka 2.0 闭源了,但其实对国内的用户影响甚小,一方面国内大都使用的是 Eureka 1.X 系列,另一方面 Spring Cloud 支持很多服务发现的软件,Eureka ...