UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
任意线可以贪心移动到两点上。直接枚举O(n^3),会TLE。
所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了。旋转的时候在O(1)时间推出下一种情况,总复杂度为O(n^2logN)就可以过了。
另外,本题有个很巧妙的技巧,就是一点等效与相反坐标的相反颜色的点。
第一次写,细节还是蛮多的,花了好久才搞清所有细节。。。
极角排序版,比较容易理解,932ms。
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
struct Point
{
int x,y;
double rad;
bool operator<(const Point &rhs) const {
return rad < rhs.rad;
}
}P[maxn];
int col[maxn]; Point tmp[maxn]; bool cmp(const Point& a,const Point& b) //anticlockwise sort
{
return a.x*b.y >= b.x*a.y;
} int solve(int n)
{
if(n<=) return n;
int ans = -;
for(int pivot = ; pivot < n; pivot++){
int k = ;
for(int i = ; i < n; i++) if(i!=pivot){
tmp[k].x = P[i].x - P[pivot].x;
tmp[k].y = P[i].y - P[pivot].y;
if(col[i]) { tmp[k].x = - tmp[k].x; tmp[k].y = -tmp[k].y; }
tmp[k].rad = atan2(tmp[k].y, tmp[k].x);
k++;
}
sort(tmp,tmp+k);
int L = , R = , sum = ;
while(L<k){
if(L == R) { R = (R+)%k; sum++; }
while(R != L && cmp(tmp[L],tmp[R])) {
R = (R+)%k; sum++;
}
ans = max(ans,sum);
sum--; L++;
}
}
return ans;
} int main()
{
int n;
while(~scanf("%d",&n)&&n){
for(int i = ; i < n; i++)
scanf("%d%d%d",&P[i].x,&P[i].y,col+i);
printf("%d\n",solve(n));
}
return ;
}
极角排序
如果卡精度,那么就用叉积两两比较,算极角常数大一些,叉积跑的快一点577ms。
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
struct Point
{
int x,y,col;
}P[maxn],tmp[maxn];; inline int cross(const Point& a,const Point& b)
{
return a.x*b.y - b.x*a.y;
} bool cmp(const Point& a,const Point& b) //anticlockwise sort
{
return a.x*b.y > b.x*a.y;
} int solve(int n)
{
if(n<=) return n;
int ans = -;
for(int pivot = ; pivot < n; pivot++){
int k = ;
for(int i = ; i < n; i++) if(i!=pivot){
tmp[k].x = P[i].x - P[pivot].x;
tmp[k].y = P[i].y - P[pivot].y;
if(tmp[k].y < || (tmp[k].y == && tmp[k].x < ) ) {
tmp[k].x = - tmp[k].x; tmp[k].y = -tmp[k].y;
tmp[k].col = P[i].col^;
}else tmp[k].col = P[i].col;
k++;
}
sort(tmp,tmp+k,cmp);
int w = ,b = ;
int i,j;
for(i = ; i < k; i++) if(tmp[i].col == )w++;
for( i = ; i < k; i = j) {
int lw = ;
for(j = i; j < k; j++) {
if(cross(tmp[i],tmp[j])) break;
if(tmp[j].col) b++;
else lw++;
}
ans = max(ans,w+b+);
ans = max(ans,k-w-b+j-i+);
w -= lw;
}
}
return ans;
} int main()
{
int n;
while(~scanf("%d",&n)&&n){
for(int i = ; i < n; i++)
scanf("%d%d%d",&P[i].x,&P[i].y,&P[i].col);
printf("%d\n",solve(n));
}
return ;
}
叉积
UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)的更多相关文章
- 【极角排序、扫描线】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【把缩写写出来,有惊喜】(滑动窗口)——yhx
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof ...
- UVA - 1606 Amphiphilic Carbon Molecules 极角扫描法
题目:点击查看题目 思路:这道题的解决思路是极角扫描法.极角扫描法的思想主要是先选择一个点作为基准点,然后求出各点对于该点的相对坐标,同时求出该坐标系下的极角,按照极角对点进行排序.然后选取点与基准点 ...
- UVA - 1606 Amphiphilic Carbon Molecules (计算几何,扫描法)
平面上给你一些具有黑或白颜色的点,让你设置一个隔板,使得隔板一侧的黑点加上另一侧的白点数最多.隔板上的点可视作任意一侧. 易知一定存在一个隔板穿过两个点且最优,因此可以先固定以一个点为原点,将其他点中 ...
- UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)
题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点.现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大.隔板上的点可以看做是在任意一侧. 分析:枚举每个基准点i,将 ...
- UVa 1606 Amphiphilic Carbon Molecules (扫描法+极角排序)
题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也 ...
- UVa 1606 - Amphiphilic Carbon Molecules
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【UVa】1606 Amphiphilic Carbon Molecules(计算几何)
题目 题目 分析 跟着lrj学的,理解了,然而不是很熟,还是发上来供以后复习 代码 #include <bits/stdc++.h> using namespace std; ; stru ...
- poj2280--Amphiphilic Carbon Molecules(扫描线+极角排序+转换坐标)
题目链接:id=2280">点击打开链接 题目大意:给出n个点的坐标.每一个点有一个值0或者1,如今有一个隔板(无限长)去分开着n个点,一側统计0的个数,一側统计1的个数,假设点在板上 ...
随机推荐
- JS控制GridView行选择
ASP.NET里的GridView控件使用非常广泛,虽然其功能强大,但总有一些不尽如人意的地方.比如在选择行的时候,它就没有UltraWebGrid做的友好:UltraWebGrid允许用户设置是否显 ...
- 数据库路由中间件MyCat - 源代码篇(12)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. NodeList ruleNodes = e.getElementsByTagName("rule ...
- mock测试方法及实践改进
此文已由作者翟曜授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. mock测试常见的定义为:在测试过程中,对于某些不易构造或不易获取的对象,通过创建虚拟对象的方式来模拟测试的测 ...
- girdspec实现画布
对于简单的画布可以很轻松的使用subplot解决,可是对于一些复杂的画布比如下面的这个 就不易使用subplot解决,这是就需要subplot的扩展版gridspec 构造函数GridSpec(nro ...
- 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片
P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...
- 洛谷P2285 [HNOI2004]打鼹鼠
P2285 [HNOI2004]打鼹鼠 题目描述 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某 ...
- Fiddler设置断点修改Request和Response【转】
Fiddler设置断点修改Request和Response 设置断点的两种方式:工具栏和命令 1.工具栏:Rules -> Automatic Breakpoints(automatic [ɔː ...
- 基于SSM搭建网站实现增删改查
网站源码地址:https://github.com/MyCreazy/BasicOperateWebSite.git 使用maven搭建网站的时候,记得选用war包格式,有时候maven包没有引用进来 ...
- php高并发之opcache
今天工作的时候接触到客户的一台服务器,业务逻辑比较简单 .估算pv在120w左右吧,用的是阿里云2c4g的服务器.一大早就开始卡顿了,登陆服务器后查看负载到了八九十. 之后就想办法调整一下吧.突然想起 ...
- mysql--浅谈多表查询1
这是对自己学习燕十八老师mysql教程的总结,非常感谢燕十八老师. 依赖软件:mysql5.6 系统环境:win 连接查询 在谈连接查询之前我们需要对数学上的笛卡尔积有一定的了解 现在有两个集合m和n ...