Frosh Week

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1545    Accepted Submission(s): 497

Problem 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
 
Source

 /*
树状数组求逆序数
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1000000
int nn;
__int64 tol;
int aa[maxn+]; struct node
{
int id;
int val;
}stu[maxn+];
//低位操作
int lowbit(int x)
{
return x&(-x);
} void ope(int x)
{
while(x<=nn)
{
aa[x]++;
x+=lowbit(x);
}
} __int64 sum(int x)
{
__int64 ans=;
while(x>)
{
ans+=aa[x];
x-=lowbit(x);
}
return ans;
}
int cmp(void const *a,void const *b)
{
return (*(struct node *)a).val - (*(struct node *)b).val;
}
int main()
{
int i,val;
while( scanf("%d",&nn)!=EOF)
{
tol=;
memset(aa,,sizeof(int)*(nn+));
for(i=;i<nn;i++)
{
scanf("%d",&stu[i].val);
stu[i].id=i+;
}
qsort(stu,nn,sizeof(struct node),cmp);
for(i=;i<nn;i++)
{
tol+=sum(nn)-sum(stu[i].id);
ope(stu[i].id);
}
printf("%I64d\n",tol);
}
return ;
}

运用归并排序求解:

递归版

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1000000
int aa[maxn+];
int cc[maxn+];
__int64 tol;
void merge(int low ,int mid ,int hight)
{
int i,j,k;
i=low;
j=mid;
k=;
while(i<mid&&j<hight)
{
if(aa[i]>aa[j])
{
cc[k++]=aa[j++];
tol+=mid-i;
}
else
cc[k++]=aa[i++];
}
for( ; i<mid; i++)
cc[k++]=aa[i];
for( ; j<hight ; j++ )
cc[k++]=aa[j];
k=;
for(i=low;i<hight;i++)
aa[i]=cc[k++];
}
void merge_sort(int st ,int en)
{
int mid;
if(st+<en)
{
mid=st+(en-st)/;
merge_sort(st,mid);
merge_sort(mid,en);
merge(st,mid,en);
}
} int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
tol=;
for(i=;i<n;i++)
scanf("%d",aa+i);
merge_sort(,n);
printf("%I64d\n",tol);
}
return ;
}

非递归版的归并排序

代码:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1000000
int aa[maxn+];
int cc[maxn+];
__int64 tol;
void merge(int low ,int mid ,int hight)
{
int i,j,k;
i=low;
j=mid;
k=;
while(i<mid&&j<hight)
{
if(aa[i]>aa[j])
{
cc[k++]=aa[j++];
tol+=mid-i;
}
else
cc[k++]=aa[i++];
}
for( ; i<mid; i++)
cc[k++]=aa[i];
for( ; j<hight ; j++ )
cc[k++]=aa[j];
k=;
for(i=low;i<hight;i++)
aa[i]=cc[k++];
}
void merge_sort(int st,int en)
{
int s,t,i;
t=;
while(t<=(en-st))
{
s=t;
t=*t;
i=st;
while(t+i<=en)
{
merge(i,i+s,i+t);
i+=t;
}
if(i+s<en)
merge(i,i+s,en);
}
if(st+s<en)
merge(st,st+s,en); } int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
tol=;
for(i=;i<n;i++)
scanf("%d",aa+i);
merge_sort(,n);
printf("%I64d\n",tol);
}
return ;
}

HDUOJ---3743Frosh Week(BIT+离散化)的更多相关文章

  1. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  2. 项目安排(离散化+DP)

    题目来源:网易有道2013年校园招聘面试二面试题 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的 ...

  3. P1774 最接近神的人_NOI导刊2010[树状数组 逆序对 离散化]

    题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上方用古代文写着“神的殿堂”.小FF猜想里面应该就有王室的 ...

  4. 洛谷P1462 通往奥格瑞玛的道路[二分答案 spfa 离散化]

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  5. POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  6. POJ2528Mayor's posters[线段树 离散化]

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59683   Accepted: 17296 ...

  7. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  8. HDU 3743 Frosh Week (线段树+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 Frosh Week Time Limit : 2000/1000ms (Java/Other) ...

  9. Hihocoder 1079 离散化

    离散化这里有很多种方式 利用结构体记录最初的索引在按位置排序再记录排名即为离散的位置再按索引排回来 或者用数组记录排序后直接对原位置二分直接去找离散应在的位置 或者对数组排序后直接map 3 20 1 ...

  10. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

随机推荐

  1. java自动创建多级目录

    // 创建文件上传路径 public static void mkdir(String path) { File fd = null; try { fd = new File(path); if (! ...

  2. Lua简明教程

    这几天系统地学习了一下Lua这个脚本语言,Lua脚本是一个很轻量级的脚本,也是号称性能最高的脚本,用在很多需要性能的地方,比如:游戏脚本,nginx,wireshark的脚本,当你把他的源码下下来编译 ...

  3. GNU GRUB

    Introduction GNU GRUB is a Multiboot boot loader. It was derived from GRUB, the GRand Unified Bootlo ...

  4. mac下java的安装和升级以及相关环境设置

    安装:brew cask install java8 如果存在多个java,可以设置JAVA_HOME指定java版本 打开终端,执行/usr/libexec/java_home -V 查看MAC下J ...

  5. 解决:HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面

    错误重现: 在发布网站的过程中,虽然不是第一次发布了,但是还是遇到了很多的问题.为了以后可以轻松解决此类问题还是积累下来比较好. 问题:HTTP 错误 404.2 - Not Found. 由于 We ...

  6. C语言条件编译

    使用与平台有关的C语言函数,可能会使得程序不具有可移植性.比如Socket编程.多线程编程等是与平台有关的. 若想将程序做成平台无关的就需要用到与平台相关的条件编译. 下面转自:http://blog ...

  7. Depth of field --Circle of confusion 推导

    https://en.wikipedia.org/wiki/Circle_of_confusion https://developer.download.nvidia.com/books/HTML/g ...

  8. 搭建个人博客-hexo+github

    自己也算是摸爬滚打搭建成功,然后自己再重新安装部署一遍,把完整步骤分享给大家,同时最后有一些连接,如果我的步骤不行,大家可以参考其他人的(这个有点花费时间,大家提前有个心理准备 - _-) 一.第一步 ...

  9. JAVASCRIPT校验大全[转]

    var IsFireFox = document.getElementById &&! document.all;//判断是否为FireFox //页面里回车到下一控件的焦点 func ...

  10. Android -- VelocityTracker

    VelocityTracker 主要应用于touch event, VelocityTracker通过跟踪一连串事件实时计算出当前的速度. 方法 //获取一个VelocityTracker对象, 用完 ...