nefu700 Car race game(离散化+线段树)

来源:互联网 发布:安卓服务端客户端源码 编辑:程序博客网 时间:2024/05/22 21:34

Car race game

Time Limit 1000ms

Memory Limit 65536K

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).<="" font="">

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

22 12 252 69 43 14 99 175 56 105 63 109 109 52 2

sample_output

167

hint

source


思路:先把其速度或位置离散化,然后排序,用线段树查找之前会被超车的数目。

          坚决不能用MAP啊,TLE了,郁闷的很啊。


#include<iostream>#include<cstring>#include<cstdio>#include<map>#include<algorithm>using namespace std;const int mm=1e5+9;///map<int,int>mp;class node{  public:int x,v;}f[mm];int p[mm],in[mm];int n,kos;bool cmp(node a,node b){  if(a.x^b.x)return a.x>b.x;  else return a.v<b.v;}int lowbit(int x){  return x&(-x);}int sum(int end){    int sum = 0;    while(end > 0)   {      sum += in[end];      end -= lowbit(end);   }   return sum; } //增加某个元素的大小 void add(int pos, int num) {  if(pos==0)return;    while(pos <= kos)   {      in[pos] += num;      pos += lowbit(pos);   } }int blook(int l,int r,int num){  int mid=(l+r)/2;  while(l<=r)  {    if(p[mid]==num)return mid+1;    else if(p[mid]>num)r=mid-1;    else l=mid+1;    mid=(l+r)/2;  }  return -1;}int main(){   while(~scanf("%d",&n))  { /// mp.clear();    for(int i=0;i<n;++i)      {scanf("%d%d",&f[i].x,&f[i].v);       p[i]=f[i].v;      }    sort(p,p+n);    int ppp=0;    for(int i=1;i<n;++i)      if(p[i]!=p[ppp])p[++ppp]=p[i];    kos=ppp+1;    for(int i=0;i<n;++i)      f[i].v=blook(0,ppp,f[i].v);    sort(f,f+n,cmp);    memset(in,0,sizeof(in));    long long ans=0;    for(int i=0;i<n;++i)    {      ans+=sum(f[i].v-1);      add(f[i].v,1);    }    printf("%lld\n",ans);  }}





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 转圈晕了想吐怎么办 我爸总是骂我妈怎么办 转圈转的想吐怎么办 大便干燥拉不出来怎么办 吹完头发很干燥怎么办 腿摔伤了结痂疼怎么办 蹭wifi被禁止后怎么办 电脑wifi给拉黑怎么办 电脑想用无线网怎么办 中路被对方打崩了怎么办 英雄联盟队友太坑怎么办 匹配被王者虐了怎么办 lol碰到嘴臭的怎么办 小婴儿脾气大怎么办呢? 玩游戏输入法会跳出出来怎么办 逆水寒fps太低怎么办 我dcj没地速怎么办 电焊看久眼睛疼怎么办 装修忘了窗帘盒怎么办? 纹眉导致眼肿了怎么办 哭泣引起的眼肿怎么办 在酒店忘记拉窗帘了怎么办 湿气重喉咙有痰怎么办 眼睛上火了肿了怎么办 陌陌直播没人看怎么办 陌陌直播没人气怎么办 我真的爱上你了怎么办 弯腰时间久了腰疼怎么办 斗鱼pk输的怎么办 领导当着人骂我怎么办 被老板骂了应该怎么办 三星s7关机键掉了怎么办 主播遇到黑粉怎么办 在工作单位突然死亡怎么办 孕7月半夜脚抽筋怎么办 上单对上两个射手怎么办 游戏本玩游戏掉帧怎么办 手机开直播很卡怎么办 小孩小鸡被虫子咬了怎么办 小鸡仔不吃食了怎么办 小鸡的腿瘸了怎么办