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
 
问题分析:显然这是一道求逆序数的题目,可用归并排序求解。
 #include "cstdio"

 int num[];
int T[];
__int64 sum;
void mset(int n)
{
for (int i=;i<=n;i++)
scanf ("%d",&num[i]);
}
void px(int x,int y)
{
if (y-x > )
{
int m = x + (y-x)/;
int p = x,q = m,i = x;
px(x,m);
px(m,y);
while (p < m || q < y)
{
if (q >= y || (p < m && num[p] <= num[q]))
T[i++] = num[p++];
else
{
T[i++] = num[q++];
sum += m-p;
}
}
for (i=x;i<y;i++)
num[i] = T[i];
}
}
int main()
{
int n;
while (~scanf ("%d",&n))
{
sum=;
mset(n);
px(,n+);
for (int i=;i<=n;i++)
printf ("%d ",num[i]);
printf ("%I64d\n",sum);
} return ;
}

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

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

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

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

    Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.   Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算 ...

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

    J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64 ...

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

    题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A ...

  5. 暑假集训(4)第八弹——— 组合(hdu1524)

    题意概括:你已经赢得两局,最后一局是N个棋子往后移动,最后一个无法移动的玩家失败. 题目分析:有向无环图sg值游戏,尼姆游戏的抽象表达.得到每个棋子的sg值之后,把他们异或起来,考察异或值是否为0. ...

  6. 暑假集训(4)第七弹——— 组合(hdu1850)

    题意概括:你赢得了第一局.魔鬼给出的第二局是,如果有N堆牌,先手的人有几种可能胜利. 问题分析:尼姆游戏,先得到n堆牌的数量异或和,再将异或和与每一个牌组的数量异或,如果结果小于原牌组数量 则可能++ ...

  7. 暑假集训(4)第四弹 -----排列,计数(hdu1465)

    题意概括:嗯,纵使你数次帮助小A脱离困境,但上一次,小A终于还是失败了.那数年的奔波与心血,抵不过轻轻一指,便彻底 湮灭,多年的友谊终归走向末路.这一切重击把小A彻底击溃! 不为什么,你到底还是要继续 ...

  8. 暑假集训(4)第三弹 -----递推(Hdu1799)

    问题描述:还记得正在努力脱团的小A吗? 他曾经最亲密的战友,趁他绘制贤者法阵期间,暗中设下鬼打墙将小A 围困,并准备破坏小A正在绘制的法阵.小A非常着急.想阻止他的行动.而要阻止他,必须先破解鬼打墙. ...

  9. 暑假集训(3)第四弹 -----Frogger(Poj2253)

    题意梗概:青蛙王子最近喜欢上了另一只经常坐在荷叶上的青蛙公主.不过这件事不小心走漏了风声,被某fff团团员知 道了,在青蛙王子准备倾述心意的那一天,fff团团员向湖泊中注入大量的充满诅咒力量的溶液.这 ...

随机推荐

  1. Java输出考试前三名

    实现输出考试成绩的前三名 要求: 1. 考试成绩已保存在数组 scores 中,数组元素依次为 89 , -23 , 64 , 91 , 119 , 52 , 73 2. 要求通过自定义方法来实现成绩 ...

  2. 关于 JavaScript 数据类型判断

    在 JavaScript 中,有 undefined.null.number.string.boolean 五种基本数据类型,另外,有一种复杂数据类型 object ,类似于 C# 中值类型.引用类型 ...

  3. Ubuntu字体设置

    Ubuntu所带的字体不是很好看,比较模糊,现修改为微软雅黑 Win7安装分区下的 \windows\fonts\文件夹下,复制msyh.ttf和msyhhd.ttf到/home/m/msfonts文 ...

  4. Codeforces Round #324 (Div. 2) A. Olesya and Rodion 水题

    A. Olesya and Rodion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/p ...

  5. SQL Server查询所有的表名/空间占用量/行数

    select object_name(id) tablename, 8*reserved/1024 reserved, rtrim(8*dpages)+'kb' used, 8*(reserved-d ...

  6. 爱维帮---LVS

    http://www.aiweibang.com/yuedu/125232503.html

  7. Shell脚本文件操作

    Linux Shell http://baike.baidu.com/link?url=2LxUhKzlh5xBUgQrS0JEc61xi761nvCS7SHJsa1U1SkVbw3CC869AoUC ...

  8. 学习笔记之DB2 9 Fundamentals 730

    Sequence中cache的影响,每新建一个连接,next value值增加increment * cache.如果加上order,则会按顺序生成值. increment cache ; Conne ...

  9. CSS字体大小设置时的参考(转)

    from:http://blog.sina.com.cn/s/blog_51cd580b0100gg6y.html font-size 设置的绝对关键字: 以下几个绝对字体大小的设置是有效的.当然他们 ...

  10. win10.10 激活

    最近几天一直有同学来找我说,为什么用密钥或kms神龙版都无法激活win8/8.1.win10,于是我就让这些同学给我截图,结果他们激活出错的信息基本都一样,都是错误:“0XC004F074 软件授权服 ...