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. zoj 2860 四边形优化dp

    Breaking Strings Time Limit: 2 Seconds        Memory Limit: 65536 KB A certain string-processing lan ...

  2. SQL Server基础知识三十三问 (7-14)

    8. 一般什么时候使用update_statistics命令? 答:  这个命令基本上是在很多数据被处理过了之后才使用的. 如果大量的删除, 修改, 或这大量的数据插入已经发生了, 那么index就需 ...

  3. Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架

    一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket ...

  4. IOS中的动画——Core Animation

    一.基础动画 CABasicAnimation //初始化方式 CABasicAnimation * cabase=[CABasicAnimation animation]; //通过keyPath设 ...

  5. scala 学习笔记十 元组

    1.元组初始化 2.元组作为函数返回值 3.元组拆包 上面168行 ,单个val后面跟着一个由五个标识符构成的元组,表示对ff返回的元组进行拆包 上面174行,将整个元组捕获到单个val或var中,那 ...

  6. CI框架入门中的简单MVC样例

    最简单的CI模型: 注意:模型须要用到数据库 配置文件在appcation/config.php 这里我们要用到数据库,须要将databases.php中的 相关參数填写一下,详细不再赘述. 直接进入 ...

  7. 【OpenCV】解析OpenCV中copyMakerBorder函数

    Use the OpenCV function :copy_make_border:`copyMakeBorder <>` to set the borders (extra paddin ...

  8. Systemd 三部曲 之 PHP7

    安装编译php7时需要的依赖包 : yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-deve ...

  9. ios Mac 地址获取

    //mac address #include <sys/socket.h> // Per msqr #include <sys/sysctl.h> #include <n ...

  10. Socket 之 传值方式

    一.传送 DataTable 和 DataSet 1.发送端需序列化数据为二进制序列化格式 public static byte[] GetBinaryFormatDataSet(DataSet ds ...