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("%d\n",sum);
92     }
93     return 0;
94 }
时间: 2024-08-07 00:11:35

HDU 2689 Sort it【树状数组】的相关文章

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it 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 

HDU 2689 Sort it(树状数组求逆序数)

Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3418    Accepted Submission(s): 2478 Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从

HDU 1892 二维树状数组

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3485    Accepted Submission(s): 1103 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno

HDU 5775:Bubble Sort(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is the code of Bubble Sort in C++. for(int i=1;i<=N;++i) for(int j=N,t;j>i;—j) if(P[j-1] > P

HDU 5775 Bubble Sort(树状数组)

题目链接:HDU 5775 题面: Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 709    Accepted Submission(s): 418 Problem Description P is a permutation of the integers from 1 to N(index startin