zoukankan      html  css  js  c++  java
  • uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx

    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.
    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.
    You always 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 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 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 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.
    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 nd 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.
    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 signi es the end of the input and
    need not be 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 namespace std;
     6 #define M(a) memset(a,0,sizeof(a))
     7 struct pnt
     8 {
     9     int x,y;
    10     bool b;
    11     double k;
    12 }a[1010],t[1010];
    13 bool cmp(const pnt &a,const pnt &b)
    14 {
    15     return a.k<b.k;
    16 }
    17 int max(int a,int b)
    18 {
    19     return a>b?a:b;
    20 }
    21 int main()
    22 {
    23     int i,j,k,m,n,p,q,x,y,z,ans,l,r,cnt;
    24     while (scanf("%d",&n)&&n)
    25     {
    26         M(a);
    27         M(t);
    28         for (i=1;i<=n;i++)
    29           scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].b);
    30         if (n<=3)
    31         {
    32             printf("%d
    ",n);
    33             continue;
    34         }
    35         ans=-1;
    36         for (i=1;i<=n;i++)
    37         {
    38             k=0;
    39             for (j=1;j<=n;j++)
    40               if (i!=j)
    41               {
    42                   k++;
    43                   t[k].x=a[j].x-a[i].x;
    44                   t[k].y=a[j].y-a[i].y;
    45                   if (a[j].b)
    46                   {
    47                       t[k].x=-t[k].x;
    48                       t[k].y=-t[k].y;
    49                 }
    50                   t[k].k=atan2(t[k].y,t[k].x);
    51               }
    52             sort(t+1,t+k+1,cmp);
    53             for (l=1,r=1,cnt=2;l<=k;l++)
    54             {
    55                 if (r==l)
    56                 {
    57                     r=r%k+1;
    58                     cnt++;
    59                 }
    60                 while (r!=l&&t[l].y*t[r].x<=t[l].x*t[r].y)
    61                 {
    62                     r=r%k+1;
    63                     cnt++;
    64                 }
    65                 cnt--;
    66                 ans=max(ans,cnt);
    67             }
    68         }
    69         printf("%d
    ",ans);
    70     }
    71 }

    题目里先后出现了ACM和ICPC,这一定只是巧合。

    没有什么特殊的算法,但不好想,也有很多细节容易写错。

    一定存在某种最优情况,分界线至少经过两个点。否则可以平移它使它经过两个点而不减少答案数。

    对每个黑点,作关于原点(即枚举的那个点)的对称点,问题就转化为计算一边的点的个数。

    枚举每个点,以该点为原点计算每个点的位置,按角度从小到大排序(不能直接用斜率k=tan,否则y轴上的点会出错。)于是用滑动窗口一边进一边出,枚举分界线的另一个点,每滑过左边的一个点就向右边滑动,由于每个点都会作为起点和终点滑过一次,算上排序,复杂度为O(nlogn)。避免O(n^2)的枚举+判断。

    总复杂度为O(n^2logn)。

    计数和滑动的地方容易写错,而且要考虑绕圈。

  • 相关阅读:
    Vue多页面 按钮级别权限控制 directive指令控制
    Vue下载页面显示内容
    golang中数组指针和指针数组的区别
    mqtt mosquitto 源码安装
    【后台管理系统】—— Ant Design Pro结合插件(一)
    【后台管理系统】—— Ant Design Pro页面相关(二)
    【后台管理系统】—— Ant Design Pro页面相关(一)
    【后台管理系统】—— Ant Design Pro组件使用(二)
    【后台管理系统】—— Ant Design Pro组件使用(一)
    【千纸诗书】—— PHP/MySQL二手书网站后台开发之知识点记录
  • 原文地址:https://www.cnblogs.com/AwesomeOrion/p/5402336.html
Copyright © 2011-2022 走看看