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. Splunk 会议回想: 大数据的关键是机器学习

    作者 Jonathan Allen ,译者 张晓鹏 Splunk的用户大会已经接近尾声.三天时间的会议里,共进行了160多个主题研讨.涵盖了从安全.运营到商业智能.甚至包含物联网,会议中一遍又一遍出现 ...

  2. spring mvc跨域设置(全局)

    //--------------第一步//spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans imple ...

  3. hdu 1348 Wall (凸包模板)

    /* 题意: 求得n个点的凸包.然后求与凸包相距l的外圈的周长. 答案为n点的凸包周长加上半径为L的圆的周长 */ # include <stdio.h> # include <ma ...

  4. STL坑汇总

    1. Q:vector的push_back()方法到底做了些什么? 为什么声明写的是void push_back (const value_type& val); A:的确,乍一看,似乎pus ...

  5. Visual studio C++ MFC之列表控件CListCtrl Control

    背景 本篇旨在MSDN帮助文档下总结列表控件CListCtrl Control的使用,并列出碰到的具体问题. 正文 列表型控件List Control的类是ClistCtrl,具体成员对象详见链接,以 ...

  6. lucene 搜索优化,个人实战经验总结

    1.IndexSearch.IndexReader等都是线程安全的类,多线程并发使用是没有问题的.不到忘不得以,尽量避免重新实例化,他们实例化是很花费时间的,看一下他们的构造原理你就知道了.   2. ...

  7. vs mvc 视图中找不到 viewdata viewbag的解决方案

    1.查看views下的web.config文件是否存在 2.检查config中system.web.mvc ,version中版本号与自己的vs内置mvc版本一致 迁移项目可能有此问题

  8. HTML5特性

    HTML5规范围绕如何使用新增标记定义了大量Javascript API,其中一些API与DOM重叠,定义了浏览器应该支持DOM拓展. 1.与类相关的扩充 HTML5新增了getElementsByC ...

  9. 脱星摘帽刺激 ST板块表现出众

    年报及业绩预告不断公布,在脱星摘帽.资产重组等一系列利好的刺激下,ST板表现出众.随着上市公司2015年财报披露的推进,*ST公司的命运也将浮出水面,近日多家有望“摘帽”的公司大多都走出了不错的行情, ...

  10. filebeat.service

    # # filebeat systemd service # [Unit] Description=Filebeat Documentation=https://www.elastic.co/guid ...