UVA - 1606 Amphiphilic Carbon Molecules
生活随笔
收集整理的這篇文章主要介紹了
UVA - 1606 Amphiphilic Carbon Molecules
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:平面上有n個點n<=1000,分為黑白兩個點,用一條線分開,使得兩邊兩個點(只能單獨取黑或白)最大。
分析:這題首先肯定是用枚舉,涉及到角度精確的問題,和巨人與鬼很相似。參考了作者的方法,還是orz。
int L = 0, R = 0, cnt = 2;while(L < k) {if(R == L) { R = (R+1)%k; cnt++; } // empty intervalwhile(R != L && Left(p[L], p[R])) { R = (R+1)%k; cnt++; } // stop when [L,R] spans across > 180 degreescnt--;L++;ans = max(ans, cnt);}這是代碼中最精華的一部分。巧妙地將黑白兩點的區分和分割問題用叉積解決了。想到應該不難,但是還是有些細節方面的處理。
作者完整代碼:
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000 + 5;struct Point {int x, y;double rad; // with respect to current pointbool operator<(const Point &rhs) const {return rad < rhs.rad;}}op[maxn], p[maxn];int n, color[maxn];// from O-A to O-B, is it a left turn?bool Left(Point A, Point B) {return A.x * B.y - A.y * B.x >= 0;}int solve() {if(n <= 2) return 2;int ans = 0;// pivot pointfor(int i = 0; i < n; i++) {int k = 0;// the list of other point, sorted in increasing order of radfor(int j = 0; j < n; j++)if(j != i) {p[k].x = op[j].x - op[i].x;p[k].y = op[j].y - op[i].y;if(color[j]) { p[k].x = -p[k].x; p[k].y = -p[k].y; }p[k].rad = atan2(p[k].y, p[k].x);k++;}sort(p, p+k);// sweeping. cnt is the number of points whose rad is between p[L] and p[R]int L = 0, R = 0, cnt = 2;while(L < k) {if(R == L) { R = (R+1)%k; cnt++; } // empty intervalwhile(R != L && Left(p[L], p[R])) { R = (R+1)%k; cnt++; } // stop when [L,R] spans across > 180 degreescnt--;L++;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", &op[i].x, &op[i].y, &color[i]);printf("%d\n", solve());}return 0;}?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的UVA - 1606 Amphiphilic Carbon Molecules的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uva-11134
- 下一篇: UVA - 11572