这道题不知道其实是WA了的,后来看来题解,也有诸多疑问。
不懂为什么空白区域要多加一个点,有大神知道的话,求测试数据或者讲解一下,不甚感激。
先贴出我WA的代码
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1100;
struct Point{
int x, y;
double rad;
bool operator < (const Point &rhs) const {
return rad < rhs.rad;
}
}point[maxn], tempPoint[maxn];
int n, color[maxn];
bool left(const Point &a, const Point &b){
return a.x * b.y >= a.y * b.x;
}
int solve(){
if(n <= 2) return n;
int ans = 0;
for(int i = 0; i < n; ++i){
int k = 0;
for(int j = 0; j < n; ++j) if(i != j){
tempPoint[k++].x = point[j].x - point[i].x;
tempPoint[k].y = point[j].y - point[i].y;
if(color[j]){tempPoint[k].x = - tempPoint[k].x; tempPoint[k].y = - tempPoint[k];}
tempPoint[k].rad = atan2(tempPoint[k].y, tempPoint[k].x);
}
sort(tempPoint, tempPoint + k);
int L = 0, R = 0, cnt = 2;
while(L < k){
if(L == R) {R = (R + 1) % k;}
while(L != R && left(tempPoint[L], tempPoint[R])){++cnt, R++;}
ans = max(ans, cnt);
++L;
--cnt;
}
}
return ans;
}
int main(){
while(scanf("%d", &n) == 1 && n){
for(int i = 0; i < n; ++i)
scanf("%d%d%d", &point[i].x, &point[i].y, &color[i]);
printf("%d\n", solve());
}
}
题解
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1100;
struct Point{
int x, y;
double rad;
bool operator < (const Point &rhs) const {
return rad < rhs.rad;
}
}point[maxn], tempPoint[maxn];
int n, color[maxn];
bool left(const Point &a, const Point &b){
return a.x * b.y >= a.y * b.x;
}
int solve(){
if(n <= 2) return n;
int ans = 0;
for(int i = 0; i < n; ++i){
int k = 0;
for(int j = 0; j < n; ++j) if(i != j){
tempPoint[k].x = point[j].x - point[i].x;
tempPoint[k].y = point[j].y - point[i].y;
if(color[j]){tempPoint[k].x = - tempPoint[k].x; tempPoint[k].y = - tempPoint[k].y;} //如果是黑点,将它绕原点旋转180度即可看做白点处理了
tempPoint[k].rad = atan2(tempPoint[k].y, tempPoint[k].x);
++k;
}
sort(tempPoint, tempPoint + k);
int L = 0, R = 0, cnt = 2;
while(L < k){
if(R == L) {R = (R + 1) % k; ++cnt;} //空区域,暂时多计入一个点, 还是太菜了,不懂为什么要多加这个点
while(R != L && left(tempPoint[L], tempPoint[R])){++cnt; R = (R + 1) % k;}
++L;
--cnt;
ans = max(ans, cnt);
}
}
return ans;
}
int main(){
while(scanf("%d", &n) == 1 && n){
for(int i = 0; i < n; ++i)
scanf("%d%d%d", &point[i].x, &point[i].y, &color[i]);
printf("%d\n", solve());
}
return 0;
}