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 star from someplace(xi means Starting point coordinate),and they possible have different speed(V means speed).so it possibly takes place to overtake(include staring the same point ). now he want 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 integer 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
Sample Output
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstring>
#define N 1000010
using namespace std;
int a[N],t[N];
long long cnt;
struct ss
{
int x;
int v;
}sss[N];
int cmp(ss a,ss b)
{
if(a.x!=b.x)return a.x<b.x;
else return a.v>b.v;
}
void merge_sort(int *a,int *t,int x,int y)
{
if(y-x>1)
{
int m=x+(y-x)/2;
int p=x,q=m,i=x;
merge_sort(a,t,x,m);
merge_sort(a,t,m,y);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q]))t[i++]=a[p++];
else
{
t[i++]=a[q++];
cnt+=m-p;
}
}
for(i=x;i<y;i++)a[i]=t[i];
}
}
int main()
{
int n,i;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)scanf("%d%d",&sss[i].x,&sss[i].v);
sort(sss,sss+n,cmp);
for(i=0;i<n;i++)a[i]=sss[i].v;
cnt=0;
merge_sort(a,t,0,n);
printf("%lld\n",cnt);
}
return 0;
}
也可用树状数组写
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define low(x) x&(-x)
#define N 1000010
#define ll long long
struct ss
{
int x,v;
}p[N];
int n;
ll c[N];
void add(int i,int val)
{
while(i<=N)
{
c[i]+=val;
i+=low(i);
}
}
ll getsum(int i)
{
ll sum=0;
while(i>0)
{
sum+=c[i];
i-=low(i);
}
return sum;
}
bool cmp(ss a,ss b)
{
if(a.x!=b.x)return a.x<b.x;
return a.v>b.v;
}
void inti()
{
memset(c,0,sizeof(c));
int i;
for(i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].v);
sort(p+1,p+n+1,cmp);
}
int main()
{
int t,i,k=1;
while(~scanf("%d",&n))
{
inti();
long long ans=0;
for(i=1;i<=n;i++)
{
add(p[i].v+1,1);
ans+=i-getsum(p[i].v+1);
}
printf("%lld\n",ans);
}
return 0;
}
注意问题
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstring>
#define N 1000010
using namespace std;
int a[N],t[N];
long long cnt;
struct ss
{
int x;
int v;
}sss[N];
int cmp(ss a,ss b)
{
if(a.x!=b.x)return a.x<b.x;
else return a.v>b.v;
}
void merge_sort(int *a,int *t,int x,int y)
{
if(y-x>1)
{
int m=x+(y-x)/2;
int p=x,q=m,i=x;
merge_sort(a,t,x,m);
merge_sort(a,t,m,y);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q]))t[i++]=a[p++];
else
{
t[i++]=a[q++];
cnt+=m-p;
}
}
for(i=x;i<y;i++)a[i]=t[i];
}
}
int main()
{
int n,i,k;
while(~scanf("%d%d",&n,&k))
{
for(i=0;i<n;i++)scanf("%d",&a[i]);
cnt=0;
merge_sort(a,t,0,n);
if(k<=cnt)printf("%I64d\n",cnt-k);
else printf("0\n");
}
return 0;
}