传送门:http://www.ifrog.cc/acm/problem/1052
思路:
因为车一点在人的右上角,可以变化一下左标,把人的左边变为原点,其它点就在第一想象了。然后发现所有经过原点共线的点,在最前面被看到的点都是互质的,所以对于所有的点(xi,yi),用(xigcd(xi,yi),fracyigcd(xi,yi))来作为它的代表员,这样就只需要看不同的代表员有多少个,用set维护这种二元组,set.size()。
复杂度为O(nlogn)
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
set<pair<int,int> >st;
int gcd(int x,int y)
{
return y ? gcd(y,x%y) : x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int a,b,n;
scanf("%d%d%d",&a,&b,&n);
st.clear();
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x-=a,y-=b;
int g=gcd(x,y);
st.insert(make_pair(x/g,y/g));
}
printf("%d\n",(int)st.size());
}
return 0;
}
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <set>
using namespace std;
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)
template<class T> T sqr(T x){ return x * x; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
set<double>s;
int main(){
int T;
read(T);
while(T--){
s.clear();
int n;
int a, b;
read(a), read(b), read(n);
int x, y;
while(n--){
read(x), read(y);
double w = 1.0*(y-b)/(x-a);
s.insert(w);
}
printf("%d\n", s.size());
}
return 0;
}
描述:
1052 - See car
Time Limit:2s Memory Limit:64MByte
Submissions:578Solved:218
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
20 0 31 1 2 23 30 0 41 12 22 34 6
SAMPLE OUTPUT
12
SOLUTION