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. Tokumx 安装指南(做法如同MongoDB)

    安装说明系统环境:Centos-6.3安装软件:mongodb-linux-x86_64-2.2.2.tgz下载地址:http://www.mongodb.org/downloads安装机器:192. ...

  2. 【转】Netty那点事(四)Netty与Reactor模式

    [原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch4-reactor.md 一:Netty.NIO.多线程? 时 ...

  3. mac下烦人的eclipse安装svn插件

    eclipse作为一个鸡肋般的java ide,颇有食之无味弃之可惜之感.最近公司统一对电脑做了一些处理,搞的我的eclipse都不能用了.重新安装了一下,各种maven.svn,代码格式什么的依赖神 ...

  4. iOS 改变tableview cell的背景色

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; cell.selectedBackgroundView ...

  5. 用Bootstrap 写了个站点

    近期发现vdceye的站点有些丑陋,就找了一个bootstrap工具,又一次把站点写了一遍 这个工具果然好用 http://vdceye.com/

  6. HDU 5481 Desiderium 动态规划

    Desiderium Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=548 ...

  7. 安卓模拟器Android SDK Manager 无法获取SDK列表的解决办法

    1.打开运行Android SDK Manager ,Tool菜单,选择Options,打开设置菜单,勾选“Force https://...sources to be fetched using h ...

  8. 玩转iOS开发 - 简易的实现2种抽屉效果

    BeautyDrawer BeautyDrawer 是一款简单易用的抽屉效果实现框架,集成的属性能够对view 滑动缩放进行控制. Main features 三个视图,主视图能够左右滑动.实现抽屉效 ...

  9. javascript---遇到关于this的相关问题(解决this)(持续更新中...)

    1.在原型中使用this <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  10. windows平台下php版本问题–VC6/VC9和TS/NTS

    php下载页面中提供了4个下载版本,是vc6/vc9 与 TS/NTS的组合 VC6:legacy Visual Studio 6 compiler,就是使用这个编译器编译的.        VC9: ...