暑假集训(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 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

问题分析:显然这是一道求逆序数的题目,可用归并排序求解。

 1 #include "cstdio"
 2
 3 int num[1000005];
 4 int T[1000005];
 5 __int64 sum;
 6 void mset(int n)
 7 {
 8   for (int i=1;i<=n;i++)
 9     scanf ("%d",&num[i]);
10 }
11 void px(int x,int y)
12 {
13    if (y-x > 1)
14    {
15        int m = x + (y-x)/2;
16        int p = x,q = m,i = x;
17        px(x,m);
18        px(m,y);
19        while (p < m || q < y)
20        {
21            if (q >= y || (p < m && num[p] <= num[q]))
22                  T[i++] = num[p++];
23            else
24            {
25                T[i++] = num[q++];
26                sum += m-p;
27            }
28        }
29       for (i=x;i<y;i++)
30          num[i] = T[i];
31    }
32 }
33 int main()
34 {
35     int n;
36     while (~scanf ("%d",&n))
37     {
38            sum=0;
39            mset(n);
40            px(1,n+1);
41             for (int i=1;i<=n;i++)
42         printf ("%d ",num[i]);
43            printf ("%I64d\n",sum);
44     }
45
46     return 0;
47 }

时间: 2024-12-15 01:34:30

暑假集训(2)第六弹 ----- Frosh Week(UVA11858)的相关文章

暑假集训(1)第六弹 -----简单计算器(Hdoj1237)

Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔.没有非法表达式.当一行中只有0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即该表达式的值,精确到小数点后2位. Sample Input 1 + 2 4 + 2 * 5 - 7 / 11 0 Sample Output 3.00 13.36 问题分析:难

暑假集训(4)第六弹——— 组合(poj1067)

题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走上这条路不过数日,好 歹你也帮助过许多生物脱离困境啊.怎么就好人......等等,你翻了翻包裹,拿出了一个颇为古旧的黄铜瓶,瓶口用锡 纸封着,这瓶子是上次那只青蛙送的,它说危难之际,打开瓶口便可解围.“娑殚之瓶!“,谁知刚拿出瓶子,就听到小A 惊讶的呼声,“它也许能帮我们脱离困境,不过恐怕会有些危险

暑假集训(2)第五弹 ----- Who&#39;s in the Middle(poj2388)

G - Who's in the Middle Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median

暑假集训(2)第七弹 -----今年暑假不AC(hdu2037)

J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了. 作为球迷,一定想看尽量

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

暑假集训(1)第八弹 -----Catch the cow(Poj3984)

Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. Input 一个5 × 5的二维数组,表示一个迷宫.数据保证有唯一解. Output 左上角到右下角的最短路径,格式如样例所示. Sa

暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m

暑假集训(4)第五弹——— 数论(hdu1222)

题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A又迟迟不现身,FFF团团长连下七道圣火令追杀你们,最先赶到地,机械化部队,它们除了智能不高外,可以说是无懈可击.这正是你要利用的一点,利用他们的行动轨迹,躲藏起来. 问题分析:首先用辗转相除法求得gcd(n,m),若n>m 则gcd(n,m)为一可逃反之,非一可逃. 1 #include "c

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;