码迷,mamicode.com
首页 > 其他好文 > 详细

Frosh Week

时间:2015-08-09 17:08:57      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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


题解:求逆序数问题,我用树状数组和归并来做的,因为没有告诉编号范围,所以先离散化在做。例如:7,3,5,77离散化之后:3,1,2,4,他们的逆序数一样的。可以理解为将原来的数改成离散后的数。

树状数组:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define mem(a) memset(a,0,sizeof(a));

using namespace std;

struct Node
{
	int pos;
	long long value;
	bool operator< (Node t) const
	{
		return value < t.value;
	}
};

Node a[1000005];
int b[1000005];
int c[100005];

void update(int x,int n,int plus)
{
	while(x <= n)
	{
		c[x] += plus;
		x += x & (-x);
	}
}

long long getSum(int x)
{
	long long res = 0;
	while(x > 0)
	{
		res += c[x];
		x -= x & (-x);
	}
	
	return res;
}

int main()
{
	int n;
	while(scanf("%d",&n) != EOF)
	{
		for(int i = 1;i <= n;i++)
		{
			scanf("%d",&a[i].value);
			a[i].pos = i;
		}
		
		sort(a + 1,a + n + 1);
		
		for(int i = 1;i <= n;i++)
		{
			b[a[i].pos] = i;
		}
		
		mem(c);
		long long sum = 0;
		for(int i = 1;i <= n;i++)
		{
			update(b[i],n,1);
			sum += i - getSum(b[i]);
		}
		
		printf("%lld\n",sum);
	}
	
	return 0;
}

归并:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define mem(a) memset(a,0,sizeof(a));

using namespace std;

struct Node
{
	int pos;
	long long value;
	bool operator< (Node t) const
	{
		return value < t.value;
	}
};

Node a[1000005];
int b[1000005];
int c[100005];

long long res;

void merge(int l,int r)
{
	int mid = (l + r) >> 1;
	int low = l;
	int high = mid + 1;
	int k = l;
	while(low <= mid && high <= r)
	{
		if(b[low] <= b[high])
		{
			c[k++] = b[low++];
		}
		else
		{
			c[k++] = b[high++];
			res += mid - low + 1;
		}
	}
	for(int i = low;i <= mid;i++)
	{
		c[k++] = b[i];
	}
	for(int i = high;i <= r;i++)
	{
		c[k++] = b[i];
	}
	for(int i = l;i < k;i++)
	{
		b[i] = c[i];
	}
}

void mergeSort(int l,int r)
{
	if(l < r)
	{
		int mid = (l + r) >> 1;
		mergeSort(l,mid);
		mergeSort(mid + 1,r);
		merge(l,r);
	}
}

int main()
{
	int n;
	while(scanf("%d",&n) != EOF)
	{
		for(int i = 1;i <= n;i++)
		{
			scanf("%d",&a[i].value);
			a[i].pos = i;
		}
		
		sort(a + 1,a + n + 1);
		
		for(int i = 1;i <= n;i++)
		{
			b[a[i].pos] = i;
		}
		
		res = 0;
		mergeSort(1,n);
		
		printf("%lld\n",res);
	}
	
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Frosh Week

标签:

原文地址:http://blog.csdn.net/wang2534499/article/details/47377535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!