1052 - See car
Time Limit:2s Memory Limit:64MByte
Submissions:569Solved:215
DESCRIPTION
You are the god of cars, standing at (a, b) point.There are some cars at point (xi,yi)(xi,yi). If lots of cars and you are in one line, you can only see the car that is nearest to yourself. How many cars can you see?
It is guaranteed that xi>axi>a and yi>byi>b.
INPUT
There are multiple test cases.The first line is a number T (T
≤11 ≤11), which means the number of cases.For each case, first line has three integers
a,b,n(−109≤a,b≤109,0≤n≤105)a,b,n(−109≤a,b≤109,0≤n≤105).next
nn lines, each line contains two integer
(xi,yi)(−109≤xi,yi≤109)(xi,yi)(−109≤xi,yi≤109), which means the position of car.
OUTPUT
one line --- the number of car that you can see.
SAMPLE INPUT
2
0 0 3
1 1
2 2
3 3
0 0 4
1 1
2 2
2 3
4 6
SAMPLE OUTPUT
1
2
SOLUTION
原题链接:http://www.ifrog.net/acm/problem/1052
题意:给出自己的所在点(x,y),再给出其他车的所在坐标(xi,yi),(xi>x,yi>y),问你从自己所在位置最多能看到多少辆车,每一个方向只能看到这个方向的第一辆车。
就是说以(x,y)为起点的射线数量,给了那个条件就更简单了,消除了斜率为0和不存在的情况。这样的话判断一下斜率有多少种就可以了。
对于斜率的存储方法可以有三种:
1:double 型数组,这个很简单,拍下序就可以了。
2:map<double,int>mp;
3:set<double>se。
对于下面两种容器,最后统计的时候大小的时候可以有个小优化。
AC代码1:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
double a[maxn];
int main()
{
//freopen("data.txt","r",stdin);
int T;
cin>>T;
while(T--)
{
int x,y,t;
cin>>x>>y>>t;
int xx,yy;
int k=0;
while(t--)
{
cin>>xx>>yy;
a[k++]=(yy-y)*1.0/(xx-x);
}
sort(a,a+k);
int ans=1;
for(int i=1;i<k;i++)
{
if(a[i]!=a[i-1])
ans++;
}
cout<<ans<<endl;
}
return 0;
}
AC代码2:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
const int maxn=1e5+5;
double a[maxn];
int main()
{
//freopen("data.txt","r",stdin);
int T;
cin>>T;
map<double,int>mp;
while(T--)
{
int x,y,t;
cin>>x>>y>>t;
int xx,yy;
mp.clear();
while(t--)
{
cin>>xx>>yy;
double k=(yy-y)*1.0/(xx-x);
if(mp.count(k)==0)
mp[k]=1;
}
cout<<mp.size()<<endl;
}
return 0;
}
AC代码3:
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
const int maxn=1e5+5;
double a[maxn];
int main()
{
//freopen("data.txt","r",stdin);
int T;
cin>>T;
set<double>se;
while(T--)
{
int x,y,t;
cin>>x>>y>>t;
int xx,yy;
se.clear();
int ans=0;
while(t--)
{
cin>>xx>>yy;
double k=(yy-y)*1.0/(xx-x);
if(se.find(k)==se.end())
{
ans++;
se.insert(k);
}
}
//cout<<se.size()<<endl;
cout<<ans<<endl;
}
return 0;
}
尊重原创,转载请注明出处: http://blog.csdn.net/hurmishine