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. Android图片加载框架最全解析(二),从源码的角度理解Glide的执行流程

    在本系列的上一篇文章中,我们学习了Glide的基本用法,体验了这个图片加载框架的强大功能,以及它非常简便的API.还没有看过上一篇文章的朋友,建议先去阅读 Android图片加载框架最全解析(一),G ...

  2. Python3.6学习笔记(二)

    Python 的高级特性 切片 对于指定索引范围取值的操作,Python提供了slice方法,类似于Excel中数据透视表的切片器. >>> L = ['Michael', 'Sar ...

  3. max-sum-of-sub-matrix-no-larger-than-k

    根据上一篇文章提到的参考文档: https://leetcode.com/discuss/109749/accepted-c-codes-with-explanation-and-references ...

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

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

  5. 使用Spring Security和OAuth2实现RESTful服务安全认证

    这篇教程是展示如何设置一个OAuth2服务来保护REST资源. 源代码下载github. (https://github.com/iainporter/oauth2-provider)你能下载这个源码 ...

  6. 理解js中的new

    new 操作符 在有上面的基础概念的介绍之后,在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassic ...

  7. scala 学习笔记九 定义操作符

    Scala中方法名可以包含几乎所有字符,还可以对操作符+赋予新的含义 上面例子中136行,用下划线来表示“缺省初始化值” 上面151行和153行都是通过圆点表示法进行调用 157行和159行用中缀表示 ...

  8. 我所认识的PCA算法的princomp函数与经历 (基于matlab)

    我接触princomp函数,主要是因为实验室的项目需要,所以我一接触的时候就希望快点学会怎么用. 项目中需要利用PCA算法对大量数据进行降维. 简介:主成分分析 ( Principal Compone ...

  9. 正则 js分转元带千分符号

    可以通过缩放来进行分到元的转换,同时使用正则对处理后的数字进行千分位格式化 方法1:(不丢失精度) function Fen2Yuan( num ) { if ( typeof num !== &qu ...

  10. 微信小程序app配置指南

    //app.json页面 { //页面注册,有几个页面都要在pages里面注册 "pages":[ "pages/index/index", "pag ...