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
后来发现是没处理相同元素(我好傻逼啊。。。。)
离散化的时候,很重要的一项,当然是相同的元素,离散化的之后也要变成相同的。。。
上道题过了纯粹是数据水。。。
/*************************************************************************
> File Name: code/sgu/180.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年08月06日 星期四 16时40分53秒
************************************************************************/
1#include<iostream>
2#include<iomanip>
3#include<cstdio>
4#include<algorithm>
5#include<cmath>
6#include<cstring>
7#include<string>
8#include<map>
9#include<set>
10#include<queue>
11#include<vector>
12#include<stack>
13#define y0 abc111qqz
14#define y1 hust111qqz
15#define yn hez111qqz
16#define j1 cute111qqz
17#define tm crazy111qqz
18#define lr dying111qqz
19using namespace std;
20#define REP(i, n) for (int i=0;i<int(n);++i)
21typedef long long LL;
22typedef unsigned long long ULL;
23const int inf = 0x7fffffff;
24const int N=7E4+7;
25struct Q
26{
27 int val;
28 int id;
29}q[N];
30int c[N];
31int ref[N];
32int n;
33bool cmp(Q a,Q b)
34{
35 if (a.val<b.val)
36 return true;
37 return false;
38}
1int lowbit( int x)
2{
3 return x&(-x);
4}
5void update( int x,int delta)
6{
7 for ( int i = x; i < N ; i=i+lowbit(i) )
8 {
9 c[i] = c[i] + delta;
10 }
11}
12int Sum( int x)
13{
14 int res =0;
15 for ( int i = x; i >= 1 ; i = i-lowbit(i))
16 {
17 res = res + c[i];
18 }
19 return res;
20}
21int main()
22{
23 while (scanf("%d",&n)!=EOF)
24 {
1 memset(c,0,sizeof(c));
2 for ( int i = 1; i <= n ; i++ )
3 {
4 scanf("%d",&q[i].val);
5 q[i].id = i ;
6 }
7 sort(q+1,q+n+1,cmp);
8 for ( int i = 1; i <= n ; i++ )
9 {
10 if (q[i].val!=q[i-1].val)
11 {
12 ref[q[i].id]=i;
13 }
14 else
15 {
16 ref[q[i].id]=ref[q[i-1].id];
17 }
18 }
1 for ( int i = 1 ;i <= n ; i ++) cout<<ref[i]<<endl;
2 LL ans = 0;
3 for ( int i = 1 ; i <= n ; i++ )
4 {
5 update(ref[i],1);
6 ans = ans + i-Sum(ref[i]);
7 }
8 cout<<ans<<endl;
9 }
return 0;
}