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. Windows Server 2003 IIS设置完全篇

    一.启用Asp支持Windows Server 2003 默认安装,是不安装 IIS 6 的,需要另外安装.安装完 IIS 6,还需要单独开启对于 ASP 的支持. 第一步,启用Asp,进入:控制面板 ...

  2. VMware+Windgb+Win7内核驱动调试

    com1被占用了,需要用com2

  3. flink和spark stream等框架的对比

    参考这篇文章: https://www.sohu.com/a/196257023_470008 我们当时的目标就是要设计一款低延迟.exactly once.流和批统一的,能够支撑足够大体量的复杂计算 ...

  4. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  5. 运用CSS改进网站设计的3个技巧

    CSS是一种分离表述编码和实际内容的一种最有用的方式,也是一种使搜索引擎最优化的方式,通常,网站设计中会建立一整套的css样式标准,这些标准存储在一个扩展名为css的单独文件中.然后利用HTML标签设 ...

  6. 你对position的了解有多少?

    此文根据Steven Bradley的<How Well Do You Understand CSS Positioning?>所译,整个译文带有我自己的理解与思想,如果译得不好或不对之处 ...

  7. IOS程式语法之block的使用掌握

    在现阶IOBlock 是iOS在4.0之后新增的程式语法,严格来说block的概念并不算是基础程式设计的范围,对初学者来说也不是很容易了解,但是在iOS SDK 4.0之后,block几乎出现在所有新 ...

  8. Cognos访问权限之让拒绝更友善

    关于cognos的访问权限之前我也做了不少总结,但是由于时间关系加上用户也只要实现功能就好,我们做的效果就是像很多人一样,就那样就好了.但是有很多事情,只要你肯动脑筋,你会发现,你还可以做的更好,下面 ...

  9. 【Nodejs】cheerio简单示例

    cheerio的API挺多,我也了解有限,欲知详情请参考 “通读cheerio API”. 下面就事论事聊聊它的基本使用. 比如说在某网页中有这么一段HTML: </tbody> < ...

  10. 云端软件平台 封装了诺基亚PC套件无法找到驱动怎么办

    1 在设备管理器中可以看到你的手机驱动器位感叹号. 2 右键→更新驱动程序→从列表指定位置安装→搜索位置选择C:\ProgramFiles\Nokia\ConnectivityCableDriver ...