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

Shanghai Hypercomputers, the world‘s largest computer chip manufacturer, has invented a new class of nanoparticles called Amphiphilic Carbon Molecules (ACMs). ACMs are semiconductors. It means that they can be either conductors or insulators of electrons, and thus possess a property that is very important for the computer chip industry. They are also amphiphilic molecules, which means parts of them are hydrophilic while other parts of them are hydrophobic. Hydrophilic ACMs are soluble in polar solvents (for example, water) but are insoluble in nonpolar solvents (for example, acetone). Hydrophobic ACMs, on the contrary, are soluble in acetone but insoluble in water. Semiconductor ACMs dissolved in either water or acetone can be used in the computer chip manufacturing process.

As a materials engineer at Shanghai Hypercomputers, your job is to prepare ACM solutions from ACM particles. You go to your factory everyday at 8 am and find a batch of ACM particles on your workbench. You prepare the ACM solutions by dripping some water, as well as some acetone, into those particles and watch the ACMs dissolve in the solvents. You always want to prepare unmixed solutions, so you first separate the ACM particles by placing an Insulating Carbon Partition Card (ICPC) perpendicular to your workbench. The ICPC is long enough to completely separate the particles. You then drip water on one side of the ICPC and acetone on the other side. The ICPC helps you obtain hydrophilic ACMs dissolved in water on one side and hydrophobic ACMs dissolved in acetone on the other side. If you happen to put the ICPC on top of some ACM particles, those ACMs will be right at the border between the water solution and the acetone solution, and they will be dissolved. Fig.1 shows your working situation.

Fig.1

Your daily job is very easy and boring, so your supervisor makes it a little bit more challenging by asking you to dissolve as much ACMs into solution as possible. You know you have to be very careful about where to put the ICPC since hydrophilic ACMs on the acetone side, or hydrophobic ACMs on the water side, will not dissolve. As an experienced engineer, you also know that sometimes it can be very difficult to find the best position for the ICPC, so you decide to write a program to help you. You have asked your supervisor to buy a special digital camera and have it installed above your workbench, so that your program can obtain the exact positions and species (hydrophilic or hydrophobic) of each ACM particle in a 2D pictures taken by the camera. The ICPC you put on your workbench will appear as a line in the 2D pictures.

Fig.2

Input

There will be no more than 10 test cases. Each case starts with a line containing an integer N, which is the number of ACM particles in the test case. N lines then follow. Each line contains three integers x, y, r, where (x, y) is the position of the ACM particle in the 2D picture and r can be 0 or 1, standing for the hydrophilic or hydrophobic type ACM respectively. The absolute value of x, y will be no larger than 10000. You may assume that N is no more than 1000. N = 0 signifies the end of the input and need not be processed. Fig.2 shows the positions of ACM particles and the best ICPC position for the last test case in the sample input.

Output

For each test case, output a line containing a single integer, which is the maximum number of dissolved ACM particles.

Sample Input

3
0 0 0
0 1 0
2 2 1
4
0 0 0
0 4 0
4 0 0
1 2 1
7
-1 0 0
1 2 1
2 3 0
2 1 1
0 3 1
1 4 0
-1 2 0
0

【题意】  给定平面上的n个点,分别为黑色和白色,现在需要放置一条隔板,使得隔板一侧的白点数目加上另一侧的黑点总数最大。隔板上的点可以看做是在任意一侧;

【分析】  贪心策略:假设隔板一定经过至少两个点;

      方法:①暴力枚举--先枚举两个点,然后输出两侧黑白点的个数。复杂度O(n^3).90%会TLE...;         ②先找一个基准点,让隔板绕该点旋转。每当隔板扫过一个点就动态修改两侧的点数。在此之前,需对所有点相对于基准点(即将基准点看做(0,0))进行极角排序。复杂度O(n^2*logn);【NOTE】  极角排序:通俗的说,就是根据坐标系内每一个点与x轴所成的角,逆时针比较,。按照角度从小到大的方式排序。用到的函数是atan2(y, x);       扫描线:这个概念也比较好理解。注意动态维护的时候细节问题(代码中会有详解,当然只针对自己这种第一次接触看不懂人家代码的菜)。       小技巧:因题目要求求两侧不同颜色的点数和,可以预处理将黑点做中心对称。这样原本位于两侧的黑白点的点就会分布在同一侧内了(真心巧妙。。。);【代码】
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 const int maxn = 1010;
 8 struct Node
 9 {
10     int x, y;
11     double rad;
12     int col;
13     bool operator < (const Node& a) const
14     {
15         return rad < a.rad;
16     }
17 }node[maxn], tnode[maxn];
18 int Cross(Node a, Node b)
19 {
20     return (a.x*b.y - b.x*a.y) >= 0;
21 }
22 int main()
23 {
24     //freopen("out.txt", "w", stdout) ;
25     int n, ans, cnt = 0;
26     while(scanf("%d", &n) && n)
27     {
28         ans = 0;
29         for(int i = 0; i < n; i++)
30         {
31             scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].col);
32         }
33         if(n <= 3) {printf("%d\n", n); continue;}
34         cnt = 0;
35         for(int basicp = 0; basicp < n; basicp++) //基准点
36         {
37             for(int i = 0; i < n; i++) //各点相对于基准点的坐标
38             {
39                 if(i == basicp) continue;
40                 tnode[cnt].x = node[i].x-node[basicp].x;
41                 tnode[cnt].y = node[i].y-node[basicp].y;
42                 if(node[i].col) {tnode[cnt].x = -tnode[cnt].x; tnode[cnt].y = -tnode[cnt].y;} //对黑点中心对称
43                 tnode[cnt].rad = atan2(tnode[cnt].y, tnode[cnt].x); //求极角
44                 cnt++;
45             }
46             sort(tnode, tnode+cnt); //极角排序。几何意义为由x轴正半轴逆时针旋转;
47             int l = 0, r = 0; //l, r:求tnode[l~r]之间的点
48             int sum = 2; //注意这个sum = 2;
49             while(l < cnt)
50             {
51                 if(r == l) //主要针对初始情况
52                 {
53                     r=(r+1)%cnt;  sum++;
54                 }
55                 while(l != r&&Cross(tnode[l],tnode[r])) //Cross(tnode[l],tnode[r]) 叉积:若小于0则两点的夹角大于180度。跳出循环
56                 {
57                     r=(r+1)%cnt;  sum++;
58                 }
59                 sum--;  //sum-1主要是为了在枚举新的起点时减去老的起点。而初始sum=2也是为了符合这一条件
60                 l++; //枚举新的起点
61                 ans=max(ans,sum);
62             }
63         }
64         cout << ans << endl;
65     }
66     return 0;
67 }

【总结】  看代码很明显又是看了别人的。希望能长个教训吧,以后遇到类似的题自己能独立写出来、

				
时间: 2024-10-16 16:26:00

【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)的相关文章

UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)

任意线可以贪心移动到两点上.直接枚举O(n^3),会TLE. 所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了.旋转的时候在O(1)时间推出下一种情况,总复杂度为O(n^2logN)就可以过了. 另外,本题有个很巧妙的技巧,就是一点等效与相反坐标的相反颜色的点. 第一次写,细节还是蛮多的,花了好久才搞清所有细节... 极角排序版,比较容易理解,932ms. #include<bits/stdc++.h> using namespace std; const

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

Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof nanoparticles called Amphiphilic Carbon Molecules (ACMs). ACMs are semiconductors. It meansthat they can be either conductors or insulators of electro

UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法

题目:点击查看题目 思路:这道题的解决思路是极角扫描法.极角扫描法的思想主要是先选择一个点作为基准点,然后求出各点对于该点的相对坐标,同时求出该坐标系下的极角,按照极角对点进行排序.然后选取点与基准点形成的线对点进行扫描,基准线为遍历选取,扫描线扫过的点,减去基准线扫过的点即为所要求的点的数量.同时注意到我们要求的是线两边的两种点的数量,于是一种点比如黑点可以旋转180度,然后之考察这180度内的百点数量即可.本题的基准点选取复杂度为O(n),极角排序复杂度O(nlogn),扫描复杂度O(n),

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

平面上给你一些具有黑或白颜色的点,让你设置一个隔板,使得隔板一侧的黑点加上另一侧的白点数最多.隔板上的点可视作任意一侧. 易知一定存在一个隔板穿过两个点且最优,因此可以先固定以一个点为原点,将其他点中的黑点移到对称的位置,并将所有点按极角排序,然后双指针遍历其他点,利用尺取法维护一个角度不超过180°的区间(算角度是否大于180°可以用叉积).对每个点都算一遍,取最大区间长度即为最终答案. 区间尺取部分网上的代码基本都的lr表示法,比较冗长,我这里给出了lw表示法(w代表区间长度),代码简洁了许

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

题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也可以通过平移使它经过,然后每次枚举两个点,当作隔板,枚举量是n*n, 然后计算是 n,那么时间复杂度就是 n3 ,一定会超时的,我产可以这样想,先枚举一个点,然后绕这个点旋转,每扫过一个点,就动态修改两侧的点数, 在转一周过程中,每个点至多扫到两次,这个过程复杂是 n,扫描前进行极角,时间复杂度是

UVa 1606 Amphiphilic Carbon Molecules 题解

难度:β 建议用时:40 min 实际用时:1 h 题目:?? 代码:?? 这题我看了刘汝佳大神的代码,在上面改了几个变量的名称,方便理解. 这是一道简单的几何题(不是真正的集合题,更可以说是一个坐标题),所以长话短说了. 题目告诉我们有一些随机分布的点,要我们用一个隔板把平面分开,然后往两边分别倒入水和丙酮来溶解掉这些点. 然而这些点有的只能溶解在水里,有的只能溶解到丙酮里. 任务是找到最大的溶解的点的数量. 这题建模就是按照刘汝佳大神的方法,一个隔板分开平面,要使左侧黑点数加右侧白点数的和最

1606 - Amphiphilic Carbon Molecules(极角排序)

这道题的关键是用到了极角排序的方法,枚举一个固定点,其他点以此点为原心求出角度,然后排序,将数点的多少转化为数角度的多少.因为角度是有序的,便可以用一次扫描求出最大值.另外,还用到了一个小技巧,那就是利用对称性,将一侧的黑点转化成另一侧的白点,这样只需要数白点的个数就好了. 值得注意的是,为了形成那条分界线,我们枚举两个角度(也就是由基准点为原心的新坐标系中的点)   ,使他们之间的夹角不超过180°,为了使分界线旋转360°,我们将l变量枚举到最后一个点再停,所以r变量的自加变成了(r+1)%

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

题目 题目 分析 跟着lrj学的,理解了,然而不是很熟,还是发上来供以后复习 代码 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=1005; 5 6 struct Point 7 { 8 int x,y; 9 double rad; 10 bool operator < (const Point &rhs) const{ 11 return rad<rhs.rad; 12 } 13

Amphiphilic Carbon Molecules

Description: Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of nanoparticles called Amphiphilic Carbon Molecules (ACMs). ACMs are semiconductors. It means that they can be either conductors or insula