博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Frosh Week(归并排序求逆序数)
阅读量:7139 次
发布时间:2019-06-28

本文共 1939 字,大约阅读时间需要 6 分钟。

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存答案会溢出。
网上很多都是用树状数组做的,但是归并排序不是正好解决这问题的么,很简单。。。
 
1 #include 
2 #include
3 using namespace std; 4 5 const int N=1000005; 6 int num[N]; 7 int temp[N]; 8 long long ans; 9 10 void merge(int left,int mid,int right)11 {12 int i=left,j=mid+1;13 int p=0;14 while (i<=mid&&j<=right)15 {16 if (num[i]<=num[j]) temp[p++]=num[i++];17 else18 {19 ans+=mid-i+1; //前半段剩下的数的个数都多了一个逆序数20 temp[p++]=num[j++];21 }22 }23 while (i<=mid) temp[p++]=num[i++];24 while (j<=right) temp[p++]=num[j++];25 j=left;26 for (i=0;i
View Code

 

 

 

转载于:https://www.cnblogs.com/haoabcd2010/p/5690997.html

你可能感兴趣的文章
[家里蹲大学数学杂志]第237期Euler公式的美
查看>>
android Animation 动画效果介绍
查看>>
java获取对象属性类型、属性名称、属性值
查看>>
C语言中的可变参数函数 三个点“…”printf( const char* format, ...)
查看>>
念念不忘,ASP.NET MVC显示WebForm网页或UserControl控件
查看>>
对datatable操作,查询
查看>>
积累的VC编程小技巧之打印相关
查看>>
自己不做出点样子,人家想拉你一把都不知你的手在哪里。
查看>>
[JS12] 统计访问次数
查看>>
log4j 使用笔记整理中
查看>>
android hook getdeceiveid
查看>>
FireDAC 下的 Sqlite [3] - 获取数据库的基本信息
查看>>
Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本号的开发环境
查看>>
ISO/IEC 14496 文档内容简介, MPEG标准
查看>>
Android解析Json速度最快的库:json-smart
查看>>
Form_Form标准控件Folder开发解析(案列)
查看>>
启明星请假系统里,计算工作日的实现
查看>>
唐伯虎
查看>>
php excel类 phpExcel使用方法介绍
查看>>
第 7 章 门面模式【Facade Pattern】
查看>>