zoukankan      html  css  js  c++  java
  • UVA 11858 Frosh Week 逆序对统计

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/122094#problem/H

    Frosh Week

    Time Limit:8000MS
    Memory Limit:0KB
    #### 问题描述 > 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 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. #### 输出 > For each test case, 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

    题意

    题目其实就是叫你求逆序对的个数

    题解

    1、数状数组+离散化

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 1e6 + 10;
    typedef long long LL;
    
    LL sumv[maxn];
    int arr[maxn],ha[maxn];
    int n;
    
    void add(int x, int v) {
    	while (x <= n) {
    		sumv[x] += v;
    		x += (x&-x);
    	}
    }
    
    LL sum(int x) {
    	LL ret = 0;
    	while (x > 0) {
    		ret += sumv[x];
    		x -= (x&-x);
    	}
    	return ret;
    }
    
    void init() {
    	memset(sumv, 0, sizeof(sumv));
    }
    
    int main() {
    	while (scanf("%d", &n) == 1 && n) {
    		init();
    		LL ans = 0;
    		for (int i = 0; i < n; i++) {
    			scanf("%d", &arr[i]);
    			ha[i] = arr[i];
    		}
    		sort(ha, ha + n);
    		for (int i = 0; i < n; i++) {
    			int id = lower_bound(ha, ha + n, arr[i]) - ha + 1;
    			ans += sum(n) - sum(id);
    			add(id,1);
    		}
    		printf("%lld
    ", ans);
    	}
    	return 0;
    }
    

    2、分治(归并排序)

    #include<iostream>
    #include<cstdio>
    #include<vector>
    using namespace std;
    
    typedef long long LL;
    const int maxn = 1e6 + 10;
    
    int n;
    int arr[maxn];
    
    int tmp[maxn];
    LL solve(int l, int r) {
    	if (l == r) return 0;
    	int mid = l + (r - l) / 2;
    	LL ret = 0;
    	ret += solve(l, mid);
    	ret += solve(mid + 1, r);
    	int p = l, p1 = l, p2 = mid + 1;
    	while (p <= r&&p1 <= mid&&p2 <= r) {
    		if (arr[p1] < arr[p2]) {
    			tmp[p++] = arr[p1];
    			//ret += p2 - mid - 1;
    			p1++;
    		}
    		else {
    			tmp[p++] = arr[p2];
    			//这里相当于枚举右边的每个元素计算左边比它大的有多少个。
    			ret += mid - p1 + 1;
    			p2++;
    		}
    	}
    	if (p1 > mid) {
    		while (p2 <= r) tmp[p++] = arr[p2],p2++;
    	}
    	else if (p2 > r) {
    		//ret += (mid - p1 + 1)*(r - mid);
    		while (p1 <= mid) tmp[p++] = arr[p1],p1++;
    	}
    	//printf("(%d,%d):%d
    ", l, r, ret);
    	for (int i = l; i <= r; i++) arr[i] = tmp[i];
    	return ret;
    }
    
    int main() {
    	while (scanf("%d", &n) == 1 && n) {
    		for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    		LL ans = solve(0, n - 1);
    		//for (int i = 0; i < n; i++) printf("%d ", arr[i]);
    		//puts("");
    		printf("%lld
    ", ans);
    	}
    	return 0;
    }
  • 相关阅读:
    JAVA中获取路径
    maven 更换阿里镜像、设置本地仓库路径
    Cannot construct instance of `com.jty.entities.Dept` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate or propertybased Creator)
    oracle日期正则表达式
    linux配置jdk
    4月份健身计划
    刚才上了ednchina的blog,发现改版了。竟然登陆不上了
    ②这次将stm32的PC13作为普通i/o口驱动led,不知道能否发生网上提到的现象
    最近画的两块板子。
    RDS的板子推倒重画
  • 原文地址:https://www.cnblogs.com/fenice/p/5679865.html
Copyright © 2011-2022 走看看