poj 3067
樹狀數組求逆序數的應用:
?
Japan| Time Limit:?1000MS | ? | Memory Limit:?65536K |
| Total Submissions:?17874 | ? | Accepted:?4819 |
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 1Sample Output
Test case 1: 5題意很好理解,就是對v進行降序排列,然后求逆序數的個數就是交點的個數:
代碼如下:
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include<map>
#include<ctype.h>
#define ll long long
#define loop1(k) for(int i=1;i<=k;i++)
#define loop2(k) for(int j=1;j<=k;j++)
#define pr(i) printf("%d\n",i);
using namespace std;
int n,m,k;
struct node{
int a;
int b;
}edge[1001000];
int cc[1001000];
bool cmp(node c,node d ){
if(c.a==d.a)
return c.b<d.b;
return c.a<d.a;
}
int low(int x){
return x&(-1*x);
}
int sum(int x){
int num=0;
while(x>=1){
num+=cc[x];
x-=low(x);
}
return num;
}
void update(int x)
{
while(x<=m)
{
cc[x]++;
x+=low(x);
}
}
int main()
{
int t;
scanf("%d",&t);
int ci=1;
while(t--){
memset(cc,0,sizeof(int)*m+15);
scanf("%d%d%d",&n,&m,&k);
memset(cc,0,sizeof(int)*m+15);
for(int i=1;i<=k;i++)
scanf("%d%d",&edge[i].a,&edge[i].b);
sort(edge+1,edge+1+k,cmp);
__int64 ans = 0;
for(int i=1;i<=k;i++){
update(edge[i].b);
ans+=sum(m)-sum(edge[i].b);
}
printf("Test case %d: %I64d\n", ci, ans);
ci++;
}
return 0;
}
?
轉載于:https://www.cnblogs.com/keephungry/p/3330441.html
總結
- 上一篇: 公主病 - 百度百科
- 下一篇: [置顶] Z-STACK之OSAL_Nv