TIOJ 1423 . 直線線段相交問題 (Intersection) ( Easy Geometry )
1423 - 直線線段相交問題 (Intersection) | TIOJ INFOR Online Judge
枚舉所有點對,算出其直線方程式 ay + bx + c = 0 之後,判斷其他的點中有多少在這個直線方程上( 座標帶入後右式正 ),和下( 右式下 ),相乘後取總和。
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef pair< int, int > pii; typedef vector< pii > vp; int N; vp A; void init(){ cin >> N; A = vp( N ); for( int i = 0; i < N; ++i ) cin >> A[ i ].first >> A[ i ].second; } void solve(){ int ans = 0; for( int i = 0; i < N; ++i ){ // ay + bx + c = f for( int j = i + 1; j < N; ++j ){ int a = A[ j ].first - A[ i ].first; int b = -( A[ j ].second - A[ i ].second ); int c = - ( a * A[ i ].second + b * A[ i ].first ); int pcnt = 0, ncnt = 0; for( int u = 0; u < N; ++u ) if( u != i and u != j ){ int f = a * A[ u ].second + b * A[ u ].first + c; if( f > 0 ) ++pcnt; if( f < 0 ) ++ncnt; } ans += pcnt * ncnt; } } cout << ans << endl; } signed main(){ ios::sync_with_stdio( false ); init(); solve(); return 0; }