HDUOJ---3743Frosh Week(BIT+离散化)
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
University of Waterloo Local Contest 2010.10.02
1 /*
2 树状数组求逆序数
3 */
4 #include<stdio.h>
5 #include<string.h>
6 #include<stdlib.h>
7 #define maxn 1000000
8 int nn;
9 __int64 tol;
10 int aa[maxn+5];
11
12 struct node
13 {
14 int id;
15 int val;
16 }stu[maxn+5];
17 //低位操作
18 int lowbit(int x)
19 {
20 return x&(-x);
21 }
22
23 void ope(int x)
24 {
25 while(x<=nn)
26 {
27 aa[x]++;
28 x+=lowbit(x);
29 }
30 }
31
32 __int64 sum(int x)
33 {
34 __int64 ans=0;
35 while(x>0)
36 {
37 ans+=aa[x];
38 x-=lowbit(x);
39 }
40 return ans;
41 }
42 int cmp(void const *a,void const *b)
43 {
44 return (*(struct node *)a).val - (*(struct node *)b).val;
45 }
46 int main()
47 {
48 int i,val;
49 while( scanf("%d",&nn)!=EOF)
50 {
51 tol=0;
52 memset(aa,0,sizeof(int)*(nn+5));
53 for(i=0;i<nn;i++)
54 {
55 scanf("%d",&stu[i].val);
56 stu[i].id=i+1;
57 }
58 qsort(stu,nn,sizeof(struct node),cmp);
59 for(i=0;i<nn;i++)
60 {
61 tol+=sum(nn)-sum(stu[i].id);
62 ope(stu[i].id);
63 }
64 printf("%I64dn",tol);
65 }
66 return 0;
67 }
运用归并排序求解:
递归版
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #define maxn 1000000
5 int aa[maxn+5];
6 int cc[maxn+5];
7 __int64 tol;
8 void merge(int low ,int mid ,int hight)
9 {
10 int i,j,k;
11 i=low;
12 j=mid;
13 k=0;
14 while(i<mid&&j<hight)
15 {
16 if(aa[i]>aa[j])
17 {
18 cc[k++]=aa[j++];
19 tol+=mid-i;
20 }
21 else
22 cc[k++]=aa[i++];
23 }
24 for( ; i<mid; i++)
25 cc[k++]=aa[i];
26 for( ; j<hight ; j++ )
27 cc[k++]=aa[j];
28 k=0;
29 for(i=low;i<hight;i++)
30 aa[i]=cc[k++];
31 }
32 void merge_sort(int st ,int en)
33 {
34 int mid;
35 if(st+1<en)
36 {
37 mid=st+(en-st)/2;
38 merge_sort(st,mid);
39 merge_sort(mid,en);
40 merge(st,mid,en);
41 }
42 }
43
44 int main()
45 {
46 int n,i;
47 while(scanf("%d",&n)!=EOF)
48 {
49 tol=0;
50 for(i=0;i<n;i++)
51 scanf("%d",aa+i);
52 merge_sort(0,n);
53 printf("%I64dn",tol);
54 }
55 return 0;
56 }
非递归版的归并排序
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #define maxn 1000000
5 int aa[maxn+5];
6 int cc[maxn+5];
7 __int64 tol;
8 void merge(int low ,int mid ,int hight)
9 {
10 int i,j,k;
11 i=low;
12 j=mid;
13 k=0;
14 while(i<mid&&j<hight)
15 {
16 if(aa[i]>aa[j])
17 {
18 cc[k++]=aa[j++];
19 tol+=mid-i;
20 }
21 else
22 cc[k++]=aa[i++];
23 }
24 for( ; i<mid; i++)
25 cc[k++]=aa[i];
26 for( ; j<hight ; j++ )
27 cc[k++]=aa[j];
28 k=0;
29 for(i=low;i<hight;i++)
30 aa[i]=cc[k++];
31 }
32 void merge_sort(int st,int en)
33 {
34 int s,t,i;
35 t=1;
36 while(t<=(en-st))
37 {
38 s=t;
39 t=2*t;
40 i=st;
41 while(t+i<=en)
42 {
43 merge(i,i+s,i+t);
44 i+=t;
45 }
46 if(i+s<en)
47 merge(i,i+s,en);
48 }
49 if(st+s<en)
50 merge(st,st+s,en);
51
52 }
53
54 int main()
55 {
56 int n,i;
57 while(scanf("%d",&n)!=EOF)
58 {
59 tol=0;
60 for(i=0;i<n;i++)
61 scanf("%d",aa+i);
62 merge_sort(0,n);
63 printf("%I64dn",tol);
64 }
65 return 0;
66 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Linux回收站机制实现过程及用法详解
- Linux如何基于AIDE检测文件系统完整性
- MySQL压测时Linux中断异常飚高,原来是因为...
- Ubuntu20.04防火墙设置简易教程(小白)
- linux上搭建私有Git服务器的详细教程
- Centos7 安装 Mysql8教程
- linux下快速列出局域网中所有主机名(计算机名)的脚本
- 详解Linux重定向用法
- Zabbix基于snmp实现监控linux主机
- 快速解决linux下中文输入法问题
- Linux实现文件内容去重及求交并差集
- Linux rpm、yum指令及使用方法详解
- Linux下遇到PyCurl的错误解决方法
- Linux cut 命令详解
- linux下安装ffmpeg的详细教程