"Polar Sort, scan line" UVa 1606-amphiphilic Carbon molecules (two pro molecule)

Source: Internet
Author: User

Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of nanoparticles called Amphiphilic Carbon Molecules (ACMS). ACMS is 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 is also amphiphilic molecules, which means parts of them is hydrophilic while other parts of them is hydrophobic. Hydrophilic ACMs is soluble in polar solvents (for example, water) but is insoluble in nonpolar solvents (for example, a Cetone). Hydrophobic ACMs, on the contrary, is soluble in acetone and insoluble in water. Semiconductor ACMs dissolved in either water or acetone can is used in the computer chip manufacturing process.

As a materials engineer at Shanghai Hypercomputers, your job was to prepare ACM solutions from ACM Particles. You go to your factory everyday @ 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 dis Solve in the solvents. You always want to prepare unmixed solutions, so you first separate the ACM particles by placing a insulating Carbon part Ition Card (ICPC) perpendicular to your workbench. The ICPC is a long enough to completely separate the particles. 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'll be is right at the border between the water Solut Ion and the acetone solution, and they'll 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 e as much acms into solution as possible. You know are very careful about where to put the ICPC since hydrophilic ACMs on the acetone side, or Hydrophobi C ACMs on the water side, would 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 the write a program. You have asked your supervisor to buy a special digital camera and has it installed above your workbench, so that your PR Ogram 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 would appear as a line in the 2D pictures.

Fig.2

Input

There'll be is no more than test cases. Each case starts with a line containing an integer N , which is the number of the 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 would be no larger than 10000. Assume that N is no more than 1000. N = 0 signifies the end of the input and need not being 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

"Test Instructions" on the given plane of n points, respectively, black and white, now need to place a baffle, so that the number of white dots on the side of the partition and the number of black spots on the other side maximum. The points on the bulkhead can be seen on either side;

"Analysis" greedy strategy: Assume that the partition must pass at least two points;

Method: ① Brute Force enumeration--enumerates two points first, then outputs the number of black and white dots on both sides. Complexity O (n^3). 90% will tle ... ;
② first find a datum point, let the clapboard rotate around that point. Each time the partition is swept over a point, it dynamically modifies the points on both sides. Prior to this, you would have to sort all the points relative to the Datum points (0,0). Complexity O (N^2*LOGN);
"NOTE" Polar sort : In layman's terms, the counter-clockwise comparison is based on the angle of each point in the coordinate system and the x-axis. Sort by angle from small to large. The function used is atan2 (y, x);
Scan Line : This concept is also better understood. Pay attention to the details of the dynamic maintenance (the code will be explained in detail, of course, only for their first contact to see people do not understand the code of the dish).
Tip: Because the topic requires different colors on both sides of the points and can be pre-treatment to the Black Point center symmetry. So the points on both sides of the black and white point will be distributed on the same side (really clever ...) );
Code
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cmath>5#include <algorithm>6 using namespacestd;7 Const intMAXN =1010;8 structNode9 {Ten     intx, y; One     DoubleRad; A     intCol; -     BOOL operator< (Constnode& a)Const -     { the         returnrad <A.rad; -     } - }NODE[MAXN], TNODE[MAXN]; - intCross (Node A, Node B) + { -     return(A.X*B.Y-B.X*A.Y) >=0; + } A intMain () at { -     //freopen ("OUT.txt", "w", stdout); -     intN, ans, cnt =0; -      while(SCANF ("%d", &n) &&N) -     { -Ans =0; in          for(inti =0; I < n; i++) -         { toscanf"%d%d%d", &node[i].x, &AMP;NODE[I].Y, &node[i].col); +         } -         if(N <=3) {printf ("%d\n", n);Continue;} theCNT =0; *          for(intBASICP =0; BASICP < n; basicp++)//Datum points $         {Panax Notoginseng              for(inti =0; I < n; i++)//coordinates of each point relative to the Datum point -             { the                 if(i = = BASICP)Continue; +tnode[cnt].x = node[i].x-node[basicp].x; ATNODE[CNT].Y = node[i].y-node[basicp].y; the                 if(Node[i].col) {tnode[cnt].x =-tnode[cnt].x; tnode[cnt].y =-tnode[cnt].y;}//symmetry to Black Point center +Tnode[cnt].rad = atan2 (tnode[cnt].y, tnode[cnt].x);//Seeking Polar angle -cnt++; $             } $Sort (tnode, tnode+cnt);//Polar Sort. The geometrical meaning is rotated counterclockwise by the positive half axis of the x-axis; -             intL =0, r =0;//L, R: Find the point between Tnode[l~r] -             intsum =2;//Note this sum = 2; the              while(L <CNT) -             {Wuyi                 if(r = = L)//mainly for the initial situation the                 { -R= (r+1)%cnt; sum++; Wu                 } -                  while(L! = R&&cross (Tnode[l],tnode[r]))//Cross (Tnode[l],tnode[r]) fork product: If less than 0, the angle of two points is greater than 180 degrees. Jump out of the loop About                 { $R= (r+1)%cnt; sum++; -                 } -sum--;//The main purpose of sum-1 is to subtract the old starting point when enumerating a new starting point. And the initial sum=2 to meet this condition . -l++;//Enumerate the New beginnings Aans=Max (ans,sum); +             } the         } -cout << ans <<Endl; $     } the     return 0; the}

"Summary" Look at the code is obviously also looked at others. Hope can long a lesson, later encountered a similar problem can be written independently,



Polar Sort, scan line UVa 1606-amphiphilic Carbon molecules (two pro molecule)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.