Problem 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
 
分析题意后发现是一个求逆序数的问题
给一列数A[1],A[2],…,A[n],求它的逆序数,即有多少个有序对(i,j),使得i<j但A[i]>A[j]。
受归并排序启发,用分治法解决该问题,把序列分成元素个数尽量相等的两半,递归统计i和j均在左边或者均在右边的逆序数,再统计i在左边,但j在右边的逆序数。
对于i在左边,j在右边的分类,按照j的不同把这些“跨越两边”的逆序对进行分类,只要对于右边的每个j,统计左边比它大的元素个数cnt,则所有cnt之和便是答案。归并排序时,当右边的A[j]复制到辅助数组ass中时,左边还没来得及复制到ass的那些数就是左边所有比A[j]大的数。此时cnt加上左边元素个数mid-p即可(左边所剩的元素在区间[p,mid)中,因此元素个数为mid-p)。
 
归并排序:
(1)划分问题:把序列分成元素个数尽量相等的两半。
(2)递归求解:把两半元素分别进行归并排序。
(3)合并问题:把两个有序表合并成一个。合并时每次只需要把两个序列的最小元素加以比较,删除其中的较小元素并加入合并后的新表即可。
 
注意小细节
(1)代码中使用的左闭右开区间,用左闭右开区间来表示一个范围,好处是在处理“数组分割”时比较自然,区间[low,high)被分成的是[low,mid)和[mid,high),不需要在任何地方加减1(主函数中调用时high要赋上n+1),另外,空区间表示为[x,x),比[x,x-1]顺眼多了。
(2)分界点mid=low+(high-low)/2而不是(low+high)/2,数学上两者相等,但在计算机中却有差别,运算符“/”的取整是朝零方向的取整,用low+(high-low)/2来确保分界点总是靠近区间起点(虽然在这题中并非必要),还有一点,使用mid=low+(high-low)/2还可以避免low+high在low和high很大时可能会出现的溢出。
(3)/2用>>1实现
 #include <stdio.h>
#define MAX 1000000 int arr[MAX+],ass[MAX+];
__int64 cnt; void MergeSort(int low,int high){
int mid,p,q,i;
if(high-low==)
return;
mid=low+((high-low)>>);
MergeSort(low,mid);
MergeSort(mid,high);
p=low,q=mid,i=low;
while(p<mid||q<high){
if(q>=high||(p<mid&&arr[p]<=arr[q]))
ass[i++]=arr[p++];
else{
ass[i++]=arr[q++];
cnt+=mid-p;
}
}
for(i=low;i<high;i++)
arr[i]=ass[i];
} void scani(int &num){
char ch;
int flag;
while(ch=getchar(),(ch>''||ch<'')&&(ch!='-'));
if(ch=='-')
flag=-,num=;
else
flag=,num=ch-'';
while(ch=getchar(),ch<=''&&ch>='')
num=num*+ch-'';
num*=flag;
} int main(){
int n,i;
while(~scanf("%d",&n)){
for(i=;i<=n;i++)
scani(arr[i]);
cnt=;
MergeSort(,n+);
printf("%I64d\n",cnt);
}
return ;
}

Frosh Week的更多相关文章

  1. HDU 3743 Frosh Week (线段树+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 Frosh Week Time Limit : 2000/1000ms (Java/Other) ...

  2. UVA 11858 Frosh Week 逆序对统计

    题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/H Frosh Week Time Limit:8000MSMemory Limi ...

  3. 暑假集训(2)第六弹 ----- Frosh Week(UVA11858)

    H - Frosh Week Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  4. 问题 C: Frosh Week(2018组队训练赛第十五场)(签到)

    问题 C: Frosh Week 时间限制: 4 Sec  内存限制: 128 MB提交: 145  解决: 63[提交][状态][讨论版][命题人:admin] 题目描述 Professor Zac ...

  5. Frosh Week(归并排序求逆序数)

    H - Frosh Week Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Desc ...

  6. HDU 3743 Frosh Week(归并排序求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...

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

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

  8. Frosh Week HDU3743(逆序数)

    离散化加 求逆序数: 求逆序数的方法 一个是归并排序  一个是树状数组 #include<bits/stdc++.h> using namespace std; int n; struct ...

  9. Prerequisites?[HDU1144]

    Prerequisites?Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. map函数原理

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #map函数 #map函数会对一个序列对象中的每一个元素应用被传入的函数,并返回一个包含了所有函数调用结果的一 ...

  2. 通过Java发送邮件和接收邮件的工具类

    一.第一种 使用SMTP协议发送电子邮件 第一步:加入mail.jar包 (1)简单类型 package com.souvc.mail; import java.util.Date; import j ...

  3. Java下Elasticsearh应用指南

    简介 本文针对在Java下操作elasticsearch给出应用示例,主要涉及创建连接,构建索引以及检索数据3个部分. 环境 1)elasticsearch2.4.4, 2)jdk1.8. 客户端连接 ...

  4. .NET Remoting三种信道Http,Tcp,IPC和Web Service的访问速度比较(转)

    Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...

  5. TouchID 指纹解锁

    概述 TouchID 指纹解锁 详细 代码下载:http://www.demodashi.com/demo/10701.html 一.软硬件支持 指纹验证功能的最低硬件支持为iPhone5s, iPa ...

  6. word使用宏定义来统一设置图片大小

    1. 首先手动拖拽将图片调到需要的格式,点击图片在格式选项中查看图片的宽高 2. 视图中点击宏新建 3. 编辑框中输入以下代码并保存,由于我只需要统一宽度,所以将统一高度的代码注释 Sub 图片格式统 ...

  7. Python 的 pass 语句

    Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. 例子1: if __name__ == '__main__': pass 例子2: # 输出 ...

  8. Windows Server 2008的远程控制修改端口,谨防非法远程连接

    1.首先在Windows Server 2008服务器系统桌面上依次单击“开始”/“运行”命令,在弹出的系统运行对话框中,输入字符串命令“regedit”,单击回车键后,打开对应系统的注册表编辑界面; ...

  9. C# 默认参数/可选参数需要注意

    在使用C#的默认参数/可选参数的时候,需要注意,参数的默认值是在编译的时候,自动加入调用方的. 如我有这样一个方法: public class Name { public void Test(Bool ...

  10. Android判断当前网络是否可用--示例代码

    Android判断当前网络是否可用--示例代码 分类: *07 Android 2011-05-24 13:46 7814人阅读 评论(4) 收藏 举报 网络androiddialogmanagern ...