任意线可以贪心移动到两点上。直接枚举O(n^3),会TLE。

所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了。旋转的时候在O(1)时间推出下一种情况,总复杂度为O(n^2logN)就可以过了。

另外,本题有个很巧妙的技巧,就是一点等效与相反坐标的相反颜色的点。

第一次写,细节还是蛮多的,花了好久才搞清所有细节。。。

极角排序版,比较容易理解,932ms。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ;
  4. struct Point
  5. {
  6. int x,y;
  7. double rad;
  8. bool operator<(const Point &rhs) const {
  9. return rad < rhs.rad;
  10. }
  11. }P[maxn];
  12. int col[maxn];
  13.  
  14. Point tmp[maxn];
  15.  
  16. bool cmp(const Point& a,const Point& b) //anticlockwise sort
  17. {
  18. return a.x*b.y >= b.x*a.y;
  19. }
  20.  
  21. int solve(int n)
  22. {
  23. if(n<=) return n;
  24. int ans = -;
  25. for(int pivot = ; pivot < n; pivot++){
  26. int k = ;
  27. for(int i = ; i < n; i++) if(i!=pivot){
  28. tmp[k].x = P[i].x - P[pivot].x;
  29. tmp[k].y = P[i].y - P[pivot].y;
  30. if(col[i]) { tmp[k].x = - tmp[k].x; tmp[k].y = -tmp[k].y; }
  31. tmp[k].rad = atan2(tmp[k].y, tmp[k].x);
  32. k++;
  33. }
  34. sort(tmp,tmp+k);
  35. int L = , R = , sum = ;
  36. while(L<k){
  37. if(L == R) { R = (R+)%k; sum++; }
  38. while(R != L && cmp(tmp[L],tmp[R])) {
  39. R = (R+)%k; sum++;
  40. }
  41. ans = max(ans,sum);
  42. sum--; L++;
  43. }
  44. }
  45. return ans;
  46. }
  47.  
  48. int main()
  49. {
  50. int n;
  51. while(~scanf("%d",&n)&&n){
  52. for(int i = ; i < n; i++)
  53. scanf("%d%d%d",&P[i].x,&P[i].y,col+i);
  54. printf("%d\n",solve(n));
  55. }
  56. return ;
  57. }

极角排序

如果卡精度,那么就用叉积两两比较,算极角常数大一些,叉积跑的快一点577ms。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = ;
  4. struct Point
  5. {
  6. int x,y,col;
  7. }P[maxn],tmp[maxn];;
  8.  
  9. inline int cross(const Point& a,const Point& b)
  10. {
  11. return a.x*b.y - b.x*a.y;
  12. }
  13.  
  14. bool cmp(const Point& a,const Point& b) //anticlockwise sort
  15. {
  16. return a.x*b.y > b.x*a.y;
  17. }
  18.  
  19. int solve(int n)
  20. {
  21. if(n<=) return n;
  22. int ans = -;
  23. for(int pivot = ; pivot < n; pivot++){
  24. int k = ;
  25. for(int i = ; i < n; i++) if(i!=pivot){
  26. tmp[k].x = P[i].x - P[pivot].x;
  27. tmp[k].y = P[i].y - P[pivot].y;
  28. if(tmp[k].y < || (tmp[k].y == && tmp[k].x < ) ) {
  29. tmp[k].x = - tmp[k].x; tmp[k].y = -tmp[k].y;
  30. tmp[k].col = P[i].col^;
  31. }else tmp[k].col = P[i].col;
  32. k++;
  33. }
  34. sort(tmp,tmp+k,cmp);
  35. int w = ,b = ;
  36. int i,j;
  37. for(i = ; i < k; i++) if(tmp[i].col == )w++;
  38. for( i = ; i < k; i = j) {
  39. int lw = ;
  40. for(j = i; j < k; j++) {
  41. if(cross(tmp[i],tmp[j])) break;
  42. if(tmp[j].col) b++;
  43. else lw++;
  44. }
  45. ans = max(ans,w+b+);
  46. ans = max(ans,k-w-b+j-i+);
  47. w -= lw;
  48. }
  49. }
  50. return ans;
  51. }
  52.  
  53. int main()
  54. {
  55. int n;
  56. while(~scanf("%d",&n)&&n){
  57. for(int i = ; i < n; i++)
  58. scanf("%d%d%d",&P[i].x,&P[i].y,&P[i].col);
  59. printf("%d\n",solve(n));
  60. }
  61. return ;
  62. }

叉积

UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)的更多相关文章

  1. 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)

    Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...

  2. uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx

    Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof ...

  3. UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法

    题目:点击查看题目 思路:这道题的解决思路是极角扫描法.极角扫描法的思想主要是先选择一个点作为基准点,然后求出各点对于该点的相对坐标,同时求出该坐标系下的极角,按照极角对点进行排序.然后选取点与基准点 ...

  4. UVA - 1606 Amphiphilic Carbon Molecules (计算几何,扫描法)

    平面上给你一些具有黑或白颜色的点,让你设置一个隔板,使得隔板一侧的黑点加上另一侧的白点数最多.隔板上的点可视作任意一侧. 易知一定存在一个隔板穿过两个点且最优,因此可以先固定以一个点为原点,将其他点中 ...

  5. UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)

    题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点.现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大.隔板上的点可以看做是在任意一侧. 分析:枚举每个基准点i,将 ...

  6. UVa 1606 Amphiphilic Carbon Molecules (扫描法+极角排序)

    题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也 ...

  7. UVa 1606 - Amphiphilic Carbon Molecules

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. 【UVa】1606 Amphiphilic Carbon Molecules(计算几何)

    题目 题目 分析 跟着lrj学的,理解了,然而不是很熟,还是发上来供以后复习 代码 #include <bits/stdc++.h> using namespace std; ; stru ...

  9. poj2280--Amphiphilic Carbon Molecules(扫描线+极角排序+转换坐标)

    题目链接:id=2280">点击打开链接 题目大意:给出n个点的坐标.每一个点有一个值0或者1,如今有一个隔板(无限长)去分开着n个点,一側统计0的个数,一側统计1的个数,假设点在板上 ...

随机推荐

  1. Hibernate的auto-import属性详解

    auto-import是什么意思呢? 我们经常会写这样一个HQL语句: from User u where u.name='罗灿锋'; 绝大多数时候,这样写是不会发生问题的. hibernate在处理 ...

  2. [51nod] 1432 独木桥 贪心

    n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...

  3. <!--[if !IE]> 的用法

    除IE外都可识别

  4. ZOJ3469 Food Delivery

    Food Delivery ZOJ - 3469 题意:外卖送饭给N个顾客,要求他们不满度和最小,没人不满度=等待时间*耐心值 #include<cstring> #include< ...

  5. js中的原型以及原型链

    在js中原型是每个构造函数的属性: 这个算 js 核心概念的一部分 var f1 = new Foo(); 对象 f1 的构造函数就是 Foo , f1的原型 __proto__ 就指向构造函数 Fo ...

  6. SpringBoot2.0 基础案例(03):配置系统全局异常映射处理

    一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性. 常见的业务 ...

  7. CODING 告诉你硅谷的研发项目管理之道(5)

    CODING 已经通过前四期文章,让大家逐步了解了一些硅谷优秀的项目管理者是如何工作.如何维持团队高效运作的.在过去的十几年中,中国的互联网行业发展过于迅猛,导致很多管理人员都是赶鸭子上架,商场如战场 ...

  8. Struts2拦截器再认识

    拦截器(Interceptor)是 Struts 2 的核心组成部分. Struts2 很多功能都是构建在拦截器基础之上的,例如文件的上传和下载.国际化.数据类型转换和数据校验等等. Struts2 ...

  9. Unity 打包PC和安卓的路径注意事项

    if UNITY_STANDALONE_WIN || UNITY_EDITOR return Application.persistentDataPath + "/LocalData&quo ...

  10. Exception sending context destroyed event to listener instance of class

    五月 29, 2019 6:29:39 下午 org.apache.catalina.core.StandardContext listenerStop严重: Exception sending co ...