题目链接:

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\n", 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\n", 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\n", ans);
}
return 0;
}

UVA 11858 Frosh Week 逆序对统计的更多相关文章

  1. 51nod1779 逆序对统计

    1779 逆序对统计 基准时间限制:1 秒 空间限制:131072 KB  lyk最近计划按顺序做n道题目,每道题目都分为很多分数档次,lyk觉得这些题太简单了,于是它想到了一个好玩的游戏. lyk决 ...

  2. [hdu5225]逆序对统计

    题目:给定一个1到n的排列,求字典序小于这个排列的所有排列的逆序对数之和. 思路:既然是求字典序小于这个排列的,不妨将排列根据和它前k位相同来分类,然后枚举第k+1位的数(小于原序列第k+1位的数), ...

  3. 51nod 1779逆序对统计(状压DP)

    按照插入数的大小排序, 然后依次进行dp. 用一个状态表示n个数是否被选了 10110 就是表示第1.3.4个位置都选了 那么如果此时这个数该填到5这个位置,那么必定会造成一个逆序(因为下一个数会填到 ...

  4. SPOJ COWPIC(逆序对变形题)

    SPOJ COWPIC 题目链接 题意:一个序列,相邻能够交换.问最少交换几次使得变成循环的1-n的当中一种 思路:对于原来正常的变换成1-n而言,答案就是逆序对了,而多了这么一个变形,事实上仅仅须要 ...

  5. 逆序对 -- cogs1438 火柴排队

    题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vimiQkqjU [题目描述] 样例一输入: 4 2 3 1 4 3 2 1 4 样例二 ...

  6. UVA 11990 ``Dynamic'' Inversion 动态逆序对

    ``Dynamic'' Inversion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/index ...

  7. HDU 3743 Frosh Week(归并排序求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...

  8. 紫书 习题 8-20 UVa 1620 (找规律+求逆序对)

    这道题看了半天没看出什么规律, 然后看到别人的博客, 结论是当n为奇数且逆序数为奇数的时候 无解, 否则有解.但是没有给出证明, 在网上也找到详细的证明--我也不知道是为什么-- 求逆序对有两种方法, ...

  9. 【CQOI2011】动态逆序对 BZOJ3295

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

随机推荐

  1. 英特尔® 实感™ SDK 架构

    英特尔® 实感™ SDK在架构上完全不同于其前代系统 — 英特尔® 感知计算 SDK. 如果您使用英特尔感知计算 SDK 进行应用开发,会很快发现,全新的 SDK 可提供增强型编程模式,从而可通过几种 ...

  2. WIN7实用的运行命令

    运行命令主要是DOS操作系统的运行方式.为方便用户的操作,微软公司将一些常用的命令,如DIR,CD等命令全部集成在系统里面:存放这些内部命令的文件是“Command”(文件后缀.com).它与IO.s ...

  3. Exchange之准备AD及域

    1.         若有旧版本的Exchange 2003,则需要执行以下命令: setup.com /PrepareLegacyExchangePermissions 2.         准备架 ...

  4. ios警告:Category is implementing a method which will also be implemented by its primary class 引发的相关处理

    今天在处理项目中相关警告的时候发现了很多问题,包括各种第三方库中的警告,以及各种乱七八糟的问题  先说说标题中的问题  Category is implementing a method which ...

  5. MongoDB中的group

    在Mongodb的查询中,有类似于SQL中group by功能的group函数.两者的功能有些类似,但是区别也是比较明显的. 对于SQL来说,group by的作用就是安装依据列来将数据表中的记录分成 ...

  6. ajax学习计划

    来自http://segmentfault.com/a/1190000004322487?utm_source=Weibo&utm_medium=shareLink&utm_campa ...

  7. 15个最新加速 Web 开发的框架和工具

    我们为开发人员挑选了15个最新的 Web 开发框架,你肯定尝试一下这些新鲜的框架,有的可能略微复杂,有的提供了很多的配置选项,也有一些窗口小部件和界面交互的选择.他们将帮助你创建更优秀的网站,提供给用 ...

  8. 网易面试题:和为n连续正数序列

    题目: 输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. 继续做些题目,看到这是网易面试题,于是 ...

  9. mysql数据库的简单操作

    首先进入mysql:mysql -u root -p 1.建库: create database 库名称; 例如:create database mydata;(创建一个名为“mydata”的库): ...

  10. 《linux 网卡别名的添加和绑定》RHEL6

    网卡别名的配置: 这个和ifconfig临时修改网卡ip 差不多,但是不一样.都是临时的,只要重启电脑就没了. 配永久的ip别名: cp ifcfg-eth0  ifcfg-eth0:0 vim if ...