1. 程式人生 > 實用技巧 >Benelux Algorithm Programming Contest 2016 Preliminary Target Practice

Benelux Algorithm Programming Contest 2016 Preliminary Target Practice

題意:

二維平面上若干個點,能否用兩條直線,穿過所有的點

前3個點要麼在同一直線上,要麼在2條直線上

所以用前3個點裡的任意兩個點定好第一條直線

然後判斷剩餘所有的點是否在這條直線或另一條直線上

#include<cstdio>

using namespace std;

#define N 100001

int n,x[N],y[N];

bool solve(int a,int b,int c)
{
    long long x0=x[b]-x[a],y0=y[b]-y[a],x1,y1,x2,y2;
    bool tag=false;
    for(int i=1;i<=n;++i)
    {
        
if(i==c || i==b || i==a) continue; x1=x[i]-x[a]; y1=y[i]-y[a]; if(x0*y1-x1*y0) if(!tag) { x2=x[c]-x[i]; y2=y[c]-y[i]; tag=true; } else { x1=x[c]-x[i]; y1
=y[c]-y[i]; if(x1*y2-x2*y1) return false; } } return true; } int main() { // freopen("bigsuccess3.in","r",stdin); scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d%d",&x[i],&y[i]); if(n<=4) { printf("success"); return
0; } int a,b,c=3; long long x0=x[1]-x[2],y0=y[1]-y[2],xx,yy; for(int i=3;i<=n;++i) { xx=x[i]-x[1]; yy=y[i]-y[1]; if(x0*yy-xx*y0) { c=i; break; } } a=1; b=2; if(solve(a,b,c) || solve(b,c,a) || solve(c,a,b)) printf("success"); else printf("failure"); return 0; }