UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)
题意:平面上有n(n <= 1000)个点,每个点为白点或者黑点。现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大。隔板上的点可以看做是在任意一侧。
分析:枚举每个基准点i,将一条直线绕这个点旋转,每当扫过一个点,就可以动态修改两侧的点数。
对于--sum,
1、如果while循环是因为s==j退出的,则其减的是if(s == j)时暂且多加上去的一个sum,这种情况是,所有的点都在基准线的左边。
2、如果while循环是因为!left(vec[j], vec[s])退出的,则其减的是因为基准线移动,上一个与基准点相连的点已不在基准线左边的情况,这种情况同样适用于旋转基准线但是没有新增点,所有sum自然会减少。
3、本题技巧是将黑点旋转180°,这样就可以化统计一侧白点和一侧黑点为统计一侧点的数量的问题。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
using namespace std;
int n;
struct Point{
int x, y, color;
double angle;
void read(){
scanf("%d%d%d", &x, &y, &color);
}
void Set(int xx, int yy, int cc){
x = xx;
y = yy;
if(cc){//如果为黑点,则相对于基准点旋转180°
x = -x;
y = -y;
}
angle = atan2(y, x);
}
bool operator<(const Point& rhs)const{
return dcmp(angle, rhs.angle) == -1;
}
}p[MAXN], vec[MAXN];
bool left(Point A, Point B){
return A.x * B.y - B.x * A.y >= 0;//叉乘
}
int solve(){
if(n <= 3) return n;
int ans = 0;
for(int i = 0; i < n; ++i){//基准点i
int cnt = 0;
for(int j = 0; j < n; ++j){
if(i != j){
vec[cnt++].Set(p[j].x - p[i].x, p[j].y - p[i].y, p[j].color);//点j相对于基准点i的相对坐标
}
}
sort(vec, vec + cnt);
int sum = 2;
int s = 0;//扫描射线
for(int j = 0; j < cnt; ++j){//以基准点为原点,枚举所有经过除基准点之外的点的射线
if(s == j){
(s += 1) %= cnt;
++sum;
}
while(s != j && left(vec[j], vec[s])){
(s += 1) %= cnt;
++sum;
}
--sum;
ans = Max(ans, sum);
}
}
return ans;
}
int main(){
while(scanf("%d", &n) == 1){
if(!n) return 0;
for(int i = 0; i < n; ++i){
p[i].read();
}
printf("%d\n", solve());
}
return 0;
}
UVA - 1606 Amphiphilic Carbon Molecules(两亲性分子)(扫描法)的更多相关文章
- UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
任意线可以贪心移动到两点上.直接枚举O(n^3),会TLE. 所以采取扫描法,选基准点,然后根据极角或者两两做叉积比较进行排排序,然后扫一遍就好了.旋转的时候在O(1)时间推出下一种情况,总复杂度为O ...
- 【极角排序、扫描线】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 (扫描法+极角排序)
题意:平面上有 n 个点,每个点不是黑的就是白的,现在要放一个隔板,把它们分成两部分,使得一侧的白点数加上另一侧的黑点数最多. 析:这个题很容易想到的就是暴力,不妨假设隔板至少经过两个点,即使不经过也 ...
- UVA - 1606 Amphiphilic Carbon Molecules (计算几何,扫描法)
平面上给你一些具有黑或白颜色的点,让你设置一个隔板,使得隔板一侧的黑点加上另一侧的白点数最多.隔板上的点可视作任意一侧. 易知一定存在一个隔板穿过两个点且最优,因此可以先固定以一个点为原点,将其他点中 ...
- 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 ...
- UVa 1606 (极角排序) Amphiphilic Carbon Molecules
如果,没有紫书上的翻译的话,我觉得我可能读不懂这道题.=_=|| 题意: 平面上有n个点,不是白点就是黑点.现在要放一条直线,使得直线一侧的白点与另一侧的黑点加起来数目最多.直线上的点可以看作位于直线 ...
随机推荐
- 在CentOS中配置java jdk环境
方法一. 1.查看yum库中都有哪些jdk版本(暂时只发现了openjdk) [root@localhost ~]# yum search java|grep jdkldapjdk-javadoc.x ...
- 解决dotnet-Angular的跨域(cors)问题
解决dotnet-Angular的跨域(cors)问题 前言 之前学了点 Angular ,打算用 dotnet core 做后端,之前没接触过这方面的东西,理所当然的遇到了跨域问题,之后也解决了,所 ...
- Windows7 wampServer3.0.6 Mutillidae2.7.12
在Mac上访问虚拟机中的mutillidae,报403: By default, Mutillidae only allow access from localhost ***: Parallels ...
- Centos7下 Oracle11G自动备份
1.创建备份目录: [root@Centos ~]# mkdir -p /home/oracle/backup 2.设置目录权限: [root@Centos ~]# chown -R oracle:o ...
- Linux ---- 查看当前TCP连接数
一.查看哪些IP连接本机 netstat -an 二.查看TCP连接数 1)统计80端口连接数netstat -nat|grep -i "80"|wc -l 2)统计httpd协议 ...
- 第2节 网站点击流项目(下):6、访客visit分析
0: jdbc:hive2://node03:10000> select * from ods_click_stream_visit limit 2;+--------------------- ...
- 如何修改 app.config 的配置信息
如何修改 app.config 的配置信息 收藏 最问这个问题的人有点多,其实 .Net 提供了这样的功能我们可以在 app.config 中 userSettings 节点中保存我们的应用程序设置信 ...
- 剑指offer 链表中环的入口位置
题目描述 一个链表中包含环,请找出该链表的环的入口结点. 思路:这题需要知道a = c,然后head和slow每次走一步,相遇的时候就是第一个入口交点, 注意:for循环或者while循环之后,一 ...
- maven加载ojdbc14报错
问题复现步骤: 1.在pom.xml里面添加ojdbc14的依赖,代码如下: <dependency> <groupId>com.oracle</groupId> ...
- POJ 3468 区间更新(求任意区间和)A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 163977 ...