H.Car Race Game | |||||
| |||||
Description | |||||
Bob is a game programming specialist. In his new car race game, there are some racers (n means the amount of racers (1<=n<=100000)). Racers start from someplace (xi means Starting point coordinate), and they possibly have different speed (V means speed). So it possibly takes place to overtake (include starting at the same point). Now he wants to calculate the maximal amount of overtaking. | |||||
Input | |||||
The first line of the input contains an integer n-determining the number of racers. Next, n lines follow. Each line contains two integers Xi and Vi. (Xi means the ith racer's Starting point coordinate, Vi means the ith racer's speed.0<Xi, Vi<1000000). | |||||
Output | |||||
For each data set in the input print on a separate line, on the standard output, the integer that represents the maximal amount of overtaking. | |||||
Sample Input | |||||
2 2 1 2 2 5 2 6 9 4 3 1 4 9 9 1 7 5 5 6 10 5 6 3 10 9 10 9 5
2 2
| |||||
Sample Output | |||||
1 6 7 | |||||
Hint | |||||
Including the same starting points~ | |||||
Author | |||||
BH |
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int x[100010],y;
struct mp
{
int x,v;
};
bool cmp(mp a,mp b)
{
if(a.x==b.x) return a.v>b.v;
return a.x<b.x;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int u,int v)
{
while(u<=y)
{
x[u]+=v;
u+=lowbit(u);
}
}
long long getsum(int u)
{
long long s=0;
while(u>0)
{
s+=x[u];
u-=lowbit(u);
}
return s;
}
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
mp a[n];long long sum=0; y=0;
memset(x,0,sizeof(x));
for(i=0; i<n; i++)
{
scanf("%d%d",&a[i].x,&a[i].v);
if(y<a[i].v) y=a[i].v;
}
sort(a,a+n,cmp);
for(i=0; i<n; i++)
{
add(a[i].v,1);
sum+=(i-getsum(a[i].v)+1);
}
printf("%lld\n",sum);
}
return 0;
}
78319 | H | Accepted | G++ | 292ms | 2144k | 975B | 2014-03-16 15:18:15 |