您的位置:首页 > 其它

树状数组--逆序数--Japan

2017-08-20 22:20 337 查看
求逆序数是树状数组中比较常见的一类问题。逆序数即求某一位置之前有多少个比该位置大的数。

首先求逆序数的代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <iomanip>
#include <string>
using namespace std;
int c[100000],a[100000];
int lowbit(int x)
{
return x&(-x);
}
int Sum(int x)
{
int sum = 0;
while (x > 0)
{
sum += c[x];
x-=lowbit(x);
}
return sum;
}
void Add(int x,int v)
{
while (x < 100000)
{
c[x] += v;
x += lowbit(x);
}
}
int main()
{
memset(c,0,sizeof(c));
long long ans = 0;
int n,i,j;
scanf("%d",&n);
for (i = 1; i <= n; i++)
scanf("%d",&a[i]);
for (i = 1; i <= n; i++)
{
Add(a[i],1);
ans += Sum(n) - Sum(a[i]);
}
printf("%lld\n",ans);
return 0;
}


最主要用到的还是add和sum函数。

在求逆序数的过程中,如果数据过大,还会用到离散化。之后再做总结。

先写一道逆序数的例题:

Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast
are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of
crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the
East coast and second one is the number of the city of the West coast. 

Output

For each test case write one line on the standard output: 

Test case (case number): (number of crossings)

Sample Input
1
3 4 4
1 4
2 3
3 2
3 1


Sample Output
Test case 1: 5


Source

Southeastern Europe 2006
题意:
在一条水平线上有2n个城市点,分别按顺序分布在两条平行的直线上,编号都是从1到n。现在在上直线与下直线的两个城市点之间建公路,一共建k条公路,问一共有多少个交点。最多只有两条公路交于同一点。

思路:

刚开始看到这个题的时候是没有思路的。。。也不知道如何和逆序数相联系。

看了看博客的分析,才明白了一些。

将获得的所有的y坐标(一条路)从小到大排序,如果y坐标相同,则x小的排在前面。那交点就是看xi前有多少个xj大于xi的就可以了。于是就转换成了求逆序数的问题。

源代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <iomanip>
#include <string>
#define MAX 1000000
using namespace std;
int c[MAX],a[MAX];
int lowbit(int x)
{
return x&(-x);
}
int Sum(int x)
{
int sum = 0;
while (x > 0)
{
sum += c[x];
x-=lowbit(x);
}
return sum;
}
void Add(int x,int v)
{
while (x < MAX)
{
c[x] += v;
x += lowbit(x);
}
}
struct Point
{
int x,y;
}point[MAX];
bool cmp(Point a,Point b)
{
if (a.y == b.y)
return a.x < b.x;
return a.y < b.y;
}
int main()
{
int N;
scanf("%d",&N);
for (int p = 1; p <= N; p++)
{
int n,m,k,i,j;
scanf("%d%d%d",&n,&m,&k);
for (i = 0; i < k; i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
}
sort (point,point + k,cmp);
memset(c,0,sizeof(c));
long long ans = 0;
for (i = 0; i < k; i++)
{
Add (point[i].x,1);
ans += Sum(k) - Sum(point[i].x);
}
printf("Text case %d: %lld\n",p,ans);
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: