H - Frosh Week

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

AC代码:

#include<iostream>//归并排序
#include<cstdio>
#include<algorithm>
#include<cstring>
# define M 1000010
using namespace std;
long long int cnt;
int A[M],T[M];
int y,m;
void merge_sort(int*A,int x,int y,int *T)
{
  if(y-x>1)
  {
   int m=x+(y-x)/2;//划分
   int p=x,q=m,i=x;
   merge_sort(A,x,m,T);//递归求解
   merge_sort(A,m,y,T);//递归求解
   while(p<m||q<y)
   {
     if(q>=y||(p<m&&A[p]<=A[q]) )T[i++]=A[p++];//从左半数组复制到临时空间
	 else
	 {
		 T[i++]=A[q++];//从右半数组复制到临时空间
		 cnt+=m-p;
	 }
   }
	 for(i=x;i<y;i++)
		 A[i]=T[i];//从辅助空间复制回A数组
  }
}

int main()
{
	int n;

	while(scanf("%d",&n)!=EOF)
	{
	    cnt=0;
		memset(A,0,sizeof(A));
		memset(T,0,sizeof(T));
		int i=0;
		while(n--)
		{
	scanf("%d",&A[i++]);
		}
    merge_sort(A,0,i,T);
		printf("%lld\n",cnt);

	}

return 0;

}

分析:问题的实质是一个逆序对,用归并排序的方法解决

分治三步法:1.划分问题:把序列分成元素个数尽量相等的两部分

2.递归求解:把两半元素分别求解

3.合并问题:把两个有序表合并成一个

时间: 2024-11-23 11:35:22

H - Frosh Week的相关文章

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

H - Frosh Week Crawling in process... Crawling failed 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 team

2016暑假集训训练2 H题 Frosh Week

求逆序数的傻逼题. 用归并排序或者树状数组或者线段树 归并排序代码: # include<stdio.h> # define MAXN 500100 int a[MAXN],b[MAXN]; long long ans; void merge(int l, int r) { int k, p, q; if(l == r) return; int mid= (l+r)>>1; merge(l,mid); merge(mid+1,r); p = l; q = mid+1; k = l;

Frosh Week

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 cr

HDU 3743 Frosh Week (归并排序)

Frosh Week Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1919    Accepted Submission(s): 619 Problem Description During Frosh Week, students play various fun games to get to know each other a

《linux 内核全然剖析》 include/asm/io.h

include/asm/io.h #define outb(value,port) __asm__ ("outb %%al,%%dx"::"a" (value),"d" (port)) //宏定义outb用汇编实现了在端口地址port处写入值value //使用的寄存器是al,一个byte长度,而端口port使用的是2byte长度地址来标记的寄存器,注意这里寄存器的使用 #define inb(port) ({ unsigned char _v;

CUDA gputimer.h头文件

#ifndef __GPU_TIMER_H__ #define __GPU_TIMER_H__ struct GpuTimer { cudaEvent_t start; cudaEvent_t stop; GpuTimer() { cudaEventCreate(&start); cudaEventCreate(&stop); } ~GpuTimer() { cudaEventDestroy(start); cudaEventDestroy(stop); } void Start() {

2017.5 校内预选赛 A H

题目描述: 目前图像识别是一项非常热门的技术,最流行的莫不过是深度学习的图像识别,识别率甚至能达到99%以上.当然,对于简单的图像来说,深度学习是没有必要的.比如如果要识别安徽拼音的首字母A和H,就可以不用深度学习就可以判断.现在有一些只含A或者H的图像,你知道该如何识别吗? 输入描述: 第一行输入正整数T,表示数据的组数. 每组数据中,第一行是两个正整数n和m,表示图像的大小. 接下来有n行,每行m个字符,只可能为'.'或者'#'.'.'表示白色,'#'表示黑色.'#'会通过上下左右或者左上左

编译过程中,termcap.h 文件找不到路径 licli.a终于生成

编译过程中,termcap.h      文件找不到路径 查看是linux  源码下找不到termcap.h文件 安装了所有关于*cap*的源码包也不起作用 今天终于解决了这个问题,搜termcap.h  发现一篇文章,如下 ----------------------------------------------------------------------------------------- 安装minicom2.3出现termcap.h错误解决方法 2010-05-06 17:12:

使用javah生成.h文件, 出现无法访问android.app,Activity的错误的解决

在工程ndk22/bin/classes中 运行javah  com.cn.ndk22.Ndk22.Activity ,出现了.h文件 我在bin/classes目录中 ,就是无法访问, : 错误:无法访问android.app.Activity 找不到android.app.Activity 如下图所示 于是我cmd定位到ndk/src,中运行 javah com.heima.ndk.ndkActivity, 成功了就能成功了 ...我也不知道为什么.,如下图 总结:  使用javah生成.h