#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1000005
#define N2 1000000
int a[N];
int n;
inline int lowbit(int x)
{
return x & (-x);
}
int add(int x, int v)
{
while (x < N)
{
a[x] += v;
x += lowbit(x);
}
return 0;
}
int Sum(int x)
{
int sum = 0;
while (x > 0)
{
sum += a[x];
x -= lowbit(x);
}
return sum;
}
struct node
{
int v;
int id;
};
int cmp(node a, node b)
{
return a.v < b.v;
}
node nn[N];
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
while (scanf("%d", &n) + 1)
{
memset(a, 0, sizeof(a));
memset(nn, 0, sizeof(nn));
int it;
long long ans = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &it);
nn[i].v = it;
nn[i].id = i;
}
sort(nn + 1, nn + n + 1, cmp);
for (int i = n; i > 0; i--)
{
it = nn[i].id;//离散化排序,逆序数不变
ans += Sum(it);//求一次小于他的数的个数
add(it, 1);//添加进去
}
cout<<ans<<endl;
}
return 0;
}
Frosh Week
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1700 Accepted Submission(s): 560
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
求存在重复元素序列的逆序数,
<pre name="code" class="cpp">#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1000005
#define N2 1000000
int a[N];
int n;
inline int lowbit(int x)
{
return x & (-x);
}
int add(int x, int v)
{
while (x < N)
{
a[x] += v;
x += lowbit(x);
}
return 0;
}
int Sum(int x)
{
int sum = 0;
while (x > 0)
{
sum += a[x];
x -= lowbit(x);
}
return sum;
}
struct node
{
int v;
int id;
};
int cmp(node a, node b)
{
if (a.v != b.v)
return a.v < b.v;
return a.id < b.id;
}
node nn[N];
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int k;
while (scanf("%d%d", &n, &k) + 1)
{
memset(a, 0, sizeof(a));
memset(nn, 0, sizeof(nn));
int it;
long long ans = 0;
int nowid = 1;
scanf("%d", &it);
nn[1].v = it;
nn[1].id = nowid;
for (int i = 2; i <= n; i++)
{
scanf("%d", &it);
nn[i].v = it;
if (nn[i].v == nn[i - 1].v)
nn[i].id = nowid;
else
nn[i].id = ++nowid;
}
sort(nn + 1, nn + n + 1, cmp);
for (int i = n; i > 0; i--)
{
it = nn[i].id;
ans += Sum(it - 1);
add(it, 1);
}
cout << ans << endl;
}
return 0;
}
STL版求逆序数
<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
const int N = 100000;
int n, m, a[N], cnt[N];
int main()
{
while (scanf("%d", &n)+1)
{
std::vector <int> value;
for (int i = 0; i < n; ++ i)
{
scanf("%d", a + i);
value.push_back(a[i]);
}
std::sort(value.begin(), value.end());
value.erase(std::unique(value.begin(), value.end()), value.end());
for (int i = 0; i < n; ++ i)
{
a[i] = std::lower_bound(value.begin(), value.end(), a[i]) - value.begin();
}
memset(cnt, 0, sizeof(cnt));
long long result = 0;
for (int i = 0; i < n; ++ i)
{
for (int j = a[i] + 1; j < n; j += ~j & j + 1)
{
result += cnt[j];
}
for (int j = a[i]; j >= 0; j -= ~j & j + 1)
{
cnt[j] ++;
}
}
cout<<result<<endl;
}
return 0;
}