HDU 2689 Sort it【树状数组】
Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4672 Accepted Submission(s): 3244
Problem Description
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need. For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.
Output
For each case, output the minimum times need to sort it in ascending order on a single line.
Sample Input
3
1 2 3
4
4 3 2 1
Sample Output
0
6
Author
WhereIsHeroFrom
Source
ZJFC 2009-3 Programming Contest
Recommend
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689
分析:好吧,我智障了,听说有种很快的写法,但好像跑的速度没我快?应该是我代码跑的最快吧,写法也是最复杂的,这题我是用树状数组乱搞的,反正15ms,相当快啊!
下面给出AC代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 inline int read()
5 {
6 int x=0,f=1;
7 char ch=getchar();
8 while(ch<'0'||ch>'9')
9 {
10 if(ch=='-')
11 f=-1;
12 ch=getchar();
13 }
14 while(ch>='0'&&ch<='9')
15 {
16 x=x*10+ch-'0';
17 ch=getchar();
18 }
19 return x*f;
20 }
21 inline void write(int x)
22 {
23 if(x<0)
24 {
25 putchar('-');
26 x=-x;
27 }
28 if(x>9)
29 {
30 write(x/10);
31 }
32 putchar(x%10+'0');
33 }
34 const int N=1001;
35 int n;
36 int num[N];
37 int c[N];
38 struct node
39 {
40 int x;
41 int id;
42 }q[N] ;
43 bool cmp(node a,node b)
44 {
45 return a.x<b.x;
46 }
47 int lowbit(int x)
48 {
49 return x&(-x);
50 }
51 int getsum(int x)
52 {
53 int s=0;
54 while(x>0)
55 {
56 s+=c[x];
57 x-=lowbit(x);
58 }
59 return s;
60 }
61 void add(int x,int y)
62 {
63 while(x<=n)
64 {
65 c[x]+=y;
66 x+=lowbit(x);
67 }
68 }
69 int main()
70 {
71 while(scanf("%d",&n)!=EOF)
72 {
73 memset(c,0,sizeof(c));
74 memset(num,0,sizeof(num));
75 for(int i=1;i<=n;i++)
76 {
77 scanf("%d",&q[i].x);
78 q[i].id=i;
79 }
80 sort(q+1,q+1+n,cmp);
81 for(int i=1;i<=n;i++)
82 {
83 num[q[i].id]=i;
84 }
85 int sum = 0;
86 for(int i=1;i<=n;i++)
87 {
88 add(num[i],1);
89 sum+=getsum(n)-getsum(num[i]);
90 }
91 printf("%dn",sum);
92 }
93 return 0;
94 }
- 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 数组属性和方法