UVA 1606 amphiphilic carbon molecules "write abbreviations, surprise" (sliding window)--yhx

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 being 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, acetone).
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.
Fig.1
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
nd a batch of ACM particles on your
Workbench. You prepare the ACM so-
Lutions by dripping some water, as well
As some acetone, into those particles and
Watch the ACMs dissolve in the solvents.
Want to prepare unmixed solu-
tions, so you rst separate the ACM parti-
Cles by placing an insulating Carbon par-
Tition 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 hydropho-
BIC 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 right at the border between the water solution and the acetone solution,
And they'll be dissolved. Fig.1 shows your working situation.
Your Daily job is very easy and boring, so Your supervisor makes it a little bit more challenging by
Asking dissolve 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 hydrophobic acms on
The water side, would not dissolve. As an experienced engineer, you also know that sometimes it can be
Very difficult to nd the best position for the ICPC, so you decide to write a program to help you. You
There are asked your supervisor to buy a special digital camera and has it installed above your workbench,
So, 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'll appear
As a line in the 2D pictures.
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'll be no larger
than 10000. You are assume that N are no more than 1000. N = 0 Signies The end of the input and
need not being processed.
Output
For each test case, output a line containing a single integer, which is the maximum number of dissolved
ACM particles.
Note:fig.2 shows the positions of ACM particles and the best ICPC position for the last Test case in
The sample input.

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <algorithm>5 using namespacestd;6 #defineM (a) memset (A,0,sizeof (a))7 structPNT8 {9     intx, y;Ten     BOOLb; One     DoubleK; A}a[1010],t[1010]; - BOOLcmpConstPNT &a,ConstPNT &b) - { the     returna.k<B.K; - } - intMaxintAintb) - { +     returnA>b?a:b; - } + intMain () A { at     inti,j,k,m,n,p,q,x,y,z,ans,l,r,cnt; -      while(SCANF ("%d", &n) &&N) -     { - M (a); - M (t); -          for(i=1; i<=n;i++) inscanf"%d%d%d",&a[i].x,&a[i].y,&a[i].b); -         if(n<=3) to         { +printf"%d\n", n); -             Continue; the         } *ans=-1; $          for(i=1; i<=n;i++)Panax Notoginseng         { -k=0; the              for(j=1; j<=n;j++) +               if(i!=j) A               { thek++; +t[k].x=a[j].x-a[i].x; -t[k].y=a[j].y-a[i].y; $                   if(a[j].b) $                   { -t[k].x=-t[k].x; -t[k].y=-t[k].y; the                 } -t[k].k=atan2 (t[k].y,t[k].x);Wuyi               } theSort (t+1, t+k+1, CMP); -              for(l=1, r=1, cnt=2; l<=k;l++) Wu             { -                 if(r==l) About                 { $r=r%k+1; -cnt++; -                 } -                  while(r!=l&&t[l].y*t[r].x<=t[l].x*t[r].y) A                 { +r=r%k+1; thecnt++; -                 } $cnt--; theans=Max (ans,cnt); the             } the         } theprintf"%d\n", ans); -     } in}

The problem has appeared in the ACM and ICPC, this must be just a coincidence.

There is no special algorithm, but it is difficult to think, there are many details easy to write wrong.

There must be some kind of optimal situation where the dividing line passes at least two points. Otherwise you can pan it so that it passes through two points without reducing the number of answers.

For each black point, the problem is converted to the number of points on one side of the point of origin (that is, the point of the enumeration).

Enumerates each point, calculates the position of each point as the origin, and sorts from small to large (not directly with the slope K=tan, otherwise the points on the Y axis are faulted. Then, using the sliding window side out, enumerate the other points of the dividing line, each slide across the left side of the slide to the right, because each point will be as the beginning and end of the slide once, count the order, the complexity of O (Nlogn). Avoid O (N^2) enumeration + judgment.

The total complexity is O (N^2LOGN).

The place to count and slide is easy to write wrong, and you have to think about circles.

UVA 1606 amphiphilic carbon molecules "write abbreviations, surprise" (sliding window)--yhx

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.