sgu 180 - Inversions (离散化+树状数组)
- Inversions
**Time Limit:**250MS **Memory Limit:**4096KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
180. Inversions
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=iA[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1…AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5
2 3 1 5 4
Output
3
一直wa 2
后来发现是没处理相同元素(我好傻逼啊。。。。)
离散化的时候,很重要的一项,当然是相同的元素,离散化的之后也要变成相同的。。。
上道题过了纯粹是数据水。。。
1/*************************************************************************
2 > File Name: code/sgu/180.cpp
3 > Author: 111qqz
4 > Email: rkz2013@126.com
5 > Created Time: 2015年08月06日 星期四 16时40分53秒
6 ************************************************************************/
7
8#include<iostream>
9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x7fffffff;
31const int N=7E4+7;
32struct Q
33{
34 int val;
35 int id;
36}q[N];
37int c[N];
38int ref[N];
39int n;
40bool cmp(Q a,Q b)
41{
42 if (a.val<b.val)
43 return true;
44 return false;
45}
46
47int lowbit( int x)
48{
49 return x&(-x);
50}
51void update( int x,int delta)
52{
53 for ( int i = x; i < N ; i=i+lowbit(i) )
54 {
55 c[i] = c[i] + delta;
56 }
57}
58int Sum( int x)
59{
60 int res =0;
61 for ( int i = x; i >= 1 ; i = i-lowbit(i))
62 {
63 res = res + c[i];
64 }
65 return res;
66}
67int main()
68{
69 while (scanf("%d",&n)!=EOF)
70 {
71
72 memset(c,0,sizeof(c));
73 for ( int i = 1; i <= n ; i++ )
74 {
75 scanf("%d",&q[i].val);
76 q[i].id = i ;
77 }
78 sort(q+1,q+n+1,cmp);
79 for ( int i = 1; i <= n ; i++ )
80 {
81 if (q[i].val!=q[i-1].val)
82 {
83 ref[q[i].id]=i;
84 }
85 else
86 {
87 ref[q[i].id]=ref[q[i-1].id];
88 }
89 }
90
91 for ( int i = 1 ;i <= n ; i ++) cout<<ref[i]<<endl;
92 LL ans = 0;
93 for ( int i = 1 ; i <= n ; i++ )
94 {
95 update(ref[i],1);
96 ans = ans + i-Sum(ref[i]);
97 }
98 cout<<ans<<endl;
99 }
100
101 return 0;
102}