UVa11858 - Frosh Week(归并排序求逆序数)

本文介绍了一种通过归并排序算法计算学生队伍排列中逆序数的方法,以确定重新排列所需的最少交换次数。这种方法可以应用于诸如新生周团队建设游戏等场景,帮助快速计算出获胜队伍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 Specification

Input contains several test cases. For each test case, 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.

Sample Input

3
3
1
2

Output Specification

For each test case, output a line containing the minimum number of swaps required to arrange the students in increasing order by student number.

Output for Sample Input

2
结果用long 表示,用int提交几次总是WA

归并排序求逆序数

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main implements Runnable
{
	private static final boolean DEBUG = false;
	private PrintWriter cout;
	private Scanner cin;
	private int n;
	private int[] num, tmp;
	
	private void init()
	{
		try {
			if (DEBUG) {
				cin = new Scanner(new BufferedInputStream(new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new Scanner(new BufferedInputStream(System.in));
			}
			
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private boolean input()
	{
		if (!cin.hasNextInt()) return false;
		
		n = cin.nextInt();
		num = new int[n];
		for (int i = 0; i < n; i++) {
			num[i] = cin.nextInt();
		}
		
		tmp = new int[n];
		return true;
	}
	
	private long mergesort(int l, int r, int[] a)
	{
		if (l >= r) return 0;
		
		int m = (l + r) >> 1;
		long s1 = mergesort(l, m, a);
		long s2 = mergesort(m + 1, r, a);
		
		int p = l, q = m + 1;
		int k = l;
		long sum = s1 + s2;
		
		while (p <= m || q <= r) {
			if (p > m || (q <= r && a[p] > a[q])) {
				tmp[k++] = a[q++];
				sum += m + 1 - p;
			} else {
				tmp[k++] = a[p++];
			}
		}
		
		for (int i = l; i <= r; i++) {
			a[i] = tmp[i];
		}
		
		return sum;
	}
	
	private void solve()
	{
		long ans = mergesort(0, n - 1, num);
		cout.println(ans);
		cout.flush();
	}
	
	public void run()
	{
		init();
		
		while (input()) {
			solve();
		}
		
	}
	
	public static void main(String[] args)
	{
		new Thread(new Main()).start();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值