POJ 3067 Japan(树状数组+逆序数)
2016-10-25 19:43
267 查看
Japan
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
Sample Output
Test case 1: 5
题意:在东西两侧分别有n和m个城市,现在要在两侧城市间建k条公路,问这些公路的交叉点有多少个
思路:对给出的公路,按其中一侧排序,然后用树状数组求另一侧的逆序对数,所有逆序对数就是答案
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5005000;
int bit
;
struct node
{
int x, y;
}p
;
int lowbit(int k)
{
return k&-k;
}
int cmp(node a,node b)
{
if(a.x!=b.x)
{
return a.x<=b.x;
}
else
{
return a.y<=b.y;
}
}
void add(int x)
{
while(x<=N)
{
bit[x]+=1;
x+=lowbit(x);
}
return ;
}
int sum(int x)
{
int ans=0;
while(x)
{
ans+=bit[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
int n, m, k;
memset(p,0,sizeof(p));
memset(bit,0,sizeof(bit));
scanf("%d %d %d", &n, &m, &k);
for(int i=1;i<=k;i++)
{
scanf("%d %d", &p[i].x, &p[i].y);
}
sort(p+1,p+k+1,cmp);
long long ans=0;
for(int i=1;i<=k;i++)
{
add(p[i].y);
ans+=(i-sum(p[i].y));
}
printf("Test case %d: %lld\n",ncase++,ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26287 | Accepted: 7138 |
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
题意:在东西两侧分别有n和m个城市,现在要在两侧城市间建k条公路,问这些公路的交叉点有多少个
思路:对给出的公路,按其中一侧排序,然后用树状数组求另一侧的逆序对数,所有逆序对数就是答案
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5005000;
int bit
;
struct node
{
int x, y;
}p
;
int lowbit(int k)
{
return k&-k;
}
int cmp(node a,node b)
{
if(a.x!=b.x)
{
return a.x<=b.x;
}
else
{
return a.y<=b.y;
}
}
void add(int x)
{
while(x<=N)
{
bit[x]+=1;
x+=lowbit(x);
}
return ;
}
int sum(int x)
{
int ans=0;
while(x)
{
ans+=bit[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
int n, m, k;
memset(p,0,sizeof(p));
memset(bit,0,sizeof(bit));
scanf("%d %d %d", &n, &m, &k);
for(int i=1;i<=k;i++)
{
scanf("%d %d", &p[i].x, &p[i].y);
}
sort(p+1,p+k+1,cmp);
long long ans=0;
for(int i=1;i<=k;i++)
{
add(p[i].y);
ans+=(i-sum(p[i].y));
}
printf("Test case %d: %lld\n",ncase++,ans);
}
return 0;
}
相关文章推荐
- poj--3067 Japan(树状数组+逆序数)
- poj 3067 Japan 树状数组+逆序数变形
- 【原】 POJ 3067 Japan 2D树状数组+逆序数 解题报告
- POJ 3067 Japan (树状数组求逆序数)
- POJ 3067 Japan(经典树状数组)
- POJ 3067 Japan
- poj 3067 Japan(逆序数)
- POJ 3067 Japan
- poj 3067 Japan(树状数组)
- POJ 3067 Japan(树状数组)
- POJ 3067 Japan
- POJ 3067 Japan(树状数组求逆序数)
- poj 3067 japan 树状数组
- poj 3067 Japan 二维树状数组(入门)
- poj 3067 Japan(树状数组求逆序数)
- POJ 3067 Japan(树状数组求逆序对个数)
- POJ 3067 Japan(经典树状数组)
- POJ 3067 Japan
- POJ 3067 Japan 【树状数组 向下更新 向上求和】
- POJ 3067-Japan-树状数组