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); }}
- nefu700 Car race game(离散化+线段树)
- Car race game
- Car race game 树状数组
- NEFU 700 Car race game 树状数组
- HDU 5372 Segment Game(线段树+离散化)
- Car race game 归并排序求逆序数
- 线段树+离散化
- 离散化 + 线段树
- 线段树离散化
- 线段树离散化
- HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
- Codeforces 605D. Board Game (BFS+线段树套SBT+离散化)
- POJ_2528 线段树+离散化
- HDU3577 离散化+线段树
- poj2528 线段树+离散化
- poj2528线段树+离散化
- hdu4325线段树+离散化
- POJ2528 离散化线段树
- opencv鼠标响应事件学习整理
- 建立多线程服务器
- Maxthon广告猎手规则,简简单单屏蔽广告
- jQuery响应式水平导航栏插件 - HorizontalNav
- 【Notepad++背景颜色主题方案选择与展现,全局设置背景设置】
- nefu700 Car race game(离散化+线段树)
- 迭代器
- EDA与CAD
- hadoop源码分析系列之(一)——org.apache.hadoop.conf包
- 查看iptables内建表名称
- 和菜鸟一起学c之函数中堆栈及运行内存情况
- 第9周--项目1-Complex类
- 关于struts与spring整合的一个小案例入门
- Android双机(网络和USB)调试及其完美ROOT