您的位置:首页 > 其它

POJ3067(树状数组逆序对)

2015-03-21 17:16 302 查看

Japan

Time Limit: 1000MS Memory Limit: 65536K

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

Analyse:

这道题很好理解,容易到我都不需要再用有道网页翻译了。。

大意是小日本有两列岛,分别标号1~n,给你k组数据,表示x与y之间有一条道路,问你一共有多少个交点。

读懂题后。大家开始划拉。。

突然!张**提出了一个惊人的想法“逆序对!”。But why?!”@#$#^@#^&#”…

然后我继续开始瞎想。。。

First idea:

·如果两个岛屿之间有两条相交的道路,则把东西沿岸看作是一组平行线的话,把另一条平行线旋转90度后(即可以想象成一个坐标系的第一象限)其焦点个数应该不会变化。Bingo!

然后,想啊想

突然,一组数据突然让我吓了一跳

1 3

2 2

3 1

因为这组数据交点重合,不过还好,题意是这种算三个交点。哈,正好满足我的方法(不信可以用坐标系试一下)

#include<stdio.h>
#include<algorithm>
#define MAXN 500000
using namespace std;
struct node
{
long long x,y;
double k;
}a[MAXN];
long long t,n,m,k;
long long add=1;
long long cmp(node a,node b)
{
return (double)a.k > (double)b.k ? 1 : 0;
}
int main()
{
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(long long i=1;i<=k;i++)
{
scanf("%I64d%I64d",&a[i].x,&a[i].y);
a[i].k=(double)a[i].y/(double)a[i].x;
}
sort(a+1,a+1+k,cmp);
long long cnt=0;
for(long long i=2;i<=k;i++)
{
for(long long j=1;j<=i-1;j++)
{
if((a[i].y<a[j].y&&a[i].x>a[j].x)||(a[i].x<a[j].x&&a[i].y>a[j].y))
{
cnt++;
}
}

}
printf("Test case %I64d: %I64d\n",add++,cnt);
}
return 0;
}


BUT!!! Time Limit Exceeded!!

这让我情何以堪。。

Right idea:

然后,我看到了正解。。

按照x先排一下序(小大皆可,我用的是升序),其实很简单,排完序后再求一下y的逆序对即可(因为首先我的y是按照x升序排列的,所以我下一个x一定是在原来的下面,如果我下一个y比这个y小,那么一定会相交。总结一下,就是求逆序对

当然,求逆序对有好多种方法,如归并排序和树状数组,但显然本题需要用树状数组,虽然稍复杂,不过更快(如果题中k的范围能小点。。早A了。。。)

然后,然后就很简单了~

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 1000000
using namespace std;
struct node
{
long long x,y;
}a[MAXN];
long long data[MAXN];
long long c[MAXN<<1];
long long t,n,m,k;
long long ok=1,ans=0;
long long lowbit(long long x)
{
return x&(-x);
}
long long getsum(long long x)
{
long long sum=0;
while(x)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
void fix(long long x,long long val)
{
while(x<=m)
{
c[x]+=val;
x+=lowbit(x);
}
}
long long cmp(node a,node b)
{
if(a.x!=b.x)
{
return a.x < b.x ? 1 : 0;
}
else
{
return a.y < b.y ? 1 : 0;
}
}
int main()
{
scanf("%I64d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(data,0,sizeof(data));
memset(c,0,sizeof(c));
ans=0;
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(long long i=1;i<=k;i++)
{
scanf("%I64d%I64d",&a[i].x,&a[i].y);
}
sort(a+1,a+1+k,cmp);
for(long long i=1;i<=k;i++)
{
data[i]=a[i].y;
}
for(long long i=k;i>=1;i--)
{
ans+=getsum(data[i]-1);
fix(data[i],1);
}
printf("Test case %I64d: %I64d\n",ok++,ans);
}
return 0;
}


A3067的艰辛之旅:



我和小伙伴们(以及老师的小记录):



Final:

Created with Raphaël 2.1.2数学方法求斜率?逆序对?数据范围!!A啦o_Oyesno
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj