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 signies 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.
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define M(a) memset(a,0,sizeof(a))
struct pnt
{
int x,y;
bool b;
double k;
}a[],t[];
bool cmp(const pnt &a,const pnt &b)
{
return a.k<b.k;
}
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k,m,n,p,q,x,y,z,ans,l,r,cnt;
while (scanf("%d",&n)&&n)
{
M(a);
M(t);
for (i=;i<=n;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].b);
if (n<=)
{
printf("%d\n",n);
continue;
}
ans=-;
for (i=;i<=n;i++)
{
k=;
for (j=;j<=n;j++)
if (i!=j)
{
k++;
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;
}
t[k].k=atan2(t[k].y,t[k].x);
}
sort(t+,t+k+,cmp);
for (l=,r=,cnt=;l<=k;l++)
{
if (r==l)
{
r=r%k+;
cnt++;
}
while (r!=l&&t[l].y*t[r].x<=t[l].x*t[r].y)
{
r=r%k+;
cnt++;
}
cnt--;
ans=max(ans,cnt);
}
}
printf("%d\n",ans);
}
}
题目里先后出现了ACM和ICPC,这一定只是巧合。
没有什么特殊的算法,但不好想,也有很多细节容易写错。
一定存在某种最优情况,分界线至少经过两个点。否则可以平移它使它经过两个点而不减少答案数。
对每个黑点,作关于原点(即枚举的那个点)的对称点,问题就转化为计算一边的点的个数。
枚举每个点,以该点为原点计算每个点的位置,按角度从小到大排序(不能直接用斜率k=tan,否则y轴上的点会出错。)于是用滑动窗口一边进一边出,枚举分界线的另一个点,每滑过左边的一个点就向右边滑动,由于每个点都会作为起点和终点滑过一次,算上排序,复杂度为O(nlogn)。避免O(n^2)的枚举+判断。
总复杂度为O(n^2logn)。
计数和滑动的地方容易写错,而且要考虑绕圈。
uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx的更多相关文章
- 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...
- UVa 1606 - Amphiphilic Carbon Molecules
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
任意线可以贪心移动到两点上.直接枚举O(n^3),会TLE. 所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了.旋转的时候在O(1)时间推出下一种情况,总复杂度为O ...
- UVa 1606 Amphiphilic Carbon Molecules (扫描法+极角排序)
题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也 ...
- UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法
题目:点击查看题目 思路:这道题的解决思路是极角扫描法.极角扫描法的思想主要是先选择一个点作为基准点,然后求出各点对于该点的相对坐标,同时求出该坐标系下的极角,按照极角对点进行排序.然后选取点与基准点 ...
- UVA - 1606 Amphiphilic Carbon Molecules (计算几何,扫描法)
平面上给你一些具有黑或白颜色的点,让你设置一个隔板,使得隔板一侧的黑点加上另一侧的白点数最多.隔板上的点可视作任意一侧. 易知一定存在一个隔板穿过两个点且最优,因此可以先固定以一个点为原点,将其他点中 ...
- UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)
题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点.现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大.隔板上的点可以看做是在任意一侧. 分析:枚举每个基准点i,将 ...
- 【UVa】1606 Amphiphilic Carbon Molecules(计算几何)
题目 题目 分析 跟着lrj学的,理解了,然而不是很熟,还是发上来供以后复习 代码 #include <bits/stdc++.h> using namespace std; ; stru ...
- UVa 1606 (极角排序) Amphiphilic Carbon Molecules
如果,没有紫书上的翻译的话,我觉得我可能读不懂这道题.=_=|| 题意: 平面上有n个点,不是白点就是黑点.现在要放一条直线,使得直线一侧的白点与另一侧的黑点加起来数目最多.直线上的点可以看作位于直线 ...
随机推荐
- ViewPager的使用
在上培训课的时候,老师一直在将ViewPager是现在的主流,一直想去好好的了解一下,今天去网上学习了一下 ,做一个总结: ViewPager其实就是后来谷歌提供给我们的一个组件,就像TextVi ...
- gene框架文档 - 概述
欢迎使用Gene框架 最新版本:V1.2.2 开源地址:https://github.com/sasou/php-gene 作者:sasou 文档地址:http://php-gene.com/doc ...
- [moka同学摘录]Yii2 csv数据导出扩展
yii2-thecsv(Yii2框架csv数据导出扩展) github: https://github.com/13552277443/yii2-thecsv 1.安装 运行 php composer ...
- Verilog学习笔记设计和验证篇(三)...............同步有限状态机的指导原则
因为大多数的FPGA内部的触发器数目相当多,又加上独热码状态机(one hot code machine)的译码逻辑最为简单,所以在FPGA实现状态机时,往往采用独热码状态机(即每个状态只有一个寄存器 ...
- 方法----MessageDigest和DigestUtils加密算法
总结:使用DigestUtils的方法加密的结果与messageDigest的方法加密结果一致,可使用DigestUtils替换MessageDigest 可省掉部分代码 package com.ac ...
- 什么是目标框架别名(What are the Target Framework Monikers (TFMs))?
我们现在的.NET Core 1.0应用(ASP.NET Core 1.0应用或控制台应用)有了新的被运行在不同框架上的可能性:①运行在.NET Core平台上 ②运行在传统的.NET Framewo ...
- 写给java程序员的c++与java实现的一些重要细微差别
0.其实常规的逻辑判断结构.工具类.文件读写.控制台读写这些的关系都不大,熟悉之后,这些都是灵活运用的问题. 学习c/c++需要预先知道的一个前提就是,虽然有ANSI C标准,但是每个c/c++编译器 ...
- C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...
- MySql中时间类型总结
最近建表要用到时间类型的数据,但对时间类型的数据一向不了解,就总结了一下.. 一.日期DATE 一个日期.支持的范围是“1000-01-01”以“9999-12-31”.MySQL显示日期用 “YYY ...
- Python基础(1)--Python编程习惯与特点
1.代码风格 在Python中,每行程序以换行符代表结束,如果一行程序太长的话,可以用“\”符号扩展到下一行.在python中以三引号(""")括起来的字符串,列表,元组 ...