poj 1271 && uva 10117 Nice Milk (半平面交)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1058
半平面交求面积最值。直接枚举C(20,8)的所有情况即可。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int N = ;
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
} pt[N], poly[N];
template<class T> T sqr(T x) { return x * x;} typedef Point Vec;
Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);}
const double EPS = 1e-;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);} inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
inline double vecLen(Vec x) { return sqrt(dotDet(x, x));}
inline Vec normal(Vec x) { return Vec(-x.y, x.x) / vecLen(x);} struct DLine {
Point p;
Vec v;
double ang;
DLine() {}
DLine(Point p, Vec v) : p(p), v(v) { ang = atan2(v.y, v.x);}
bool operator < (const DLine &L) const { return ang < L.ang;}
DLine move(double x) { return DLine(p + normal(v) * x, v);}
} dl[N]; inline bool onLeft(DLine L, Point p) { return crossDet(L.v, p - L.p) > ;}
inline Point dLineIntersect(DLine a, DLine b) { return a.p + a.v * (crossDet(b.v, a.p - b.p) / crossDet(a.v, b.v));} struct Poly {
vector<Point> pt;
Poly() { pt.clear();}
~Poly() {}
Poly(vector<Point> &pt) : pt(pt) {}
Point operator [] (int x) const { return pt[x];}
int size() { return pt.size();}
double area() {
if (pt.size() < ) return 0.0;
double ret = 0.0;
for (int i = , sz = pt.size(); i < sz; i++)
ret += crossDet(pt[i], pt[(i + ) % sz]);
return fabs(ret / 2.0);
}
} ; Point p[N];
DLine q[N];
int halfPlane(DLine *L, int n) {
sort(L, L + n);
int fi, la;
q[fi = la = ] = L[];
for (int i = ; i < n; i++) {
while (fi < la && !onLeft(L[i], p[la - ])) la--;
while (fi < la && !onLeft(L[i], p[fi])) fi++;
q[++la] = L[i];
if (fabs(crossDet(q[la].v, q[la - ].v)) < EPS) {
la--;
if (onLeft(q[la], L[i].p)) q[la] = L[i];
}
if (fi < la) p[la - ] = dLineIntersect(q[la - ], q[la]);
}
while (fi < la && !onLeft(q[fi], p[la - ])) la--;
if (la < fi) return ;
p[la] = dLineIntersect(q[la], q[fi]);
int ret = ;
for (int i = fi; i <= la; i++) poly[ret++] = p[i];
return ret;
} //Poly halfPlane(DLine *L, int n) {
// Poly ret = Poly();
// sort(L, L + n);
// int fi, la;
// Point *p = new Point[n];
// DLine *q = new DLine[n];
// q[fi = la = 0] = L[0];
// for (int i = 1; i < n; i++) {
// while (fi < la && !onLeft(L[i], p[la - 1])) la--;
// while (fi < la && !onLeft(L[i], p[fi])) fi++;
// q[++la] = L[i];
// if (fabs(crossDet(q[la].v, q[la - 1].v)) < EPS) {
// la--;
// if (onLeft(q[la], L[i].p)) q[la] = L[i];
// }
// if (fi < la) p[la - 1] = dLineIntersect(q[la - 1], q[la]);
// }
// while (fi < la && !onLeft(q[fi], p[la - 1])) la--;
// if (la < fi) return ret;
// p[la] = dLineIntersect(q[la], q[fi]);
// for (int i = fi; i <= la; i++) ret.pt.push_back(p[i]);
// return ret;
//} double polyArea(Point *p, int n) {
if (n < ) return 0.0;
double sum = 0.0;
p[n] = p[];
Point O = Point(0.0, 0.0);
for (int i = ; i < n; i++) sum += crossDet(O, p[i], p[i + ]);
return fabs(sum / 2.0);
} double work(int st, int n, double d) {
// cout << st << endl;
for (int i = ; i < n; i++) dl[i] = DLine(pt[i], pt[i + ] - pt[i]).move((st & ( << i)) == ? 0.0 : d);
int tmp = halfPlane(dl, n);
return polyArea(poly, tmp);
// Poly tmp = halfPlane(dl, n);
// return tmp.area();
} int cntBit(int x) {
int cnt = ;
while (x > ) {
if (x & ) cnt++;
x >>= ;
}
return cnt;
} int main() {
// freopen("in", "r", stdin);
int n, k;
double d;
while (~scanf("%d%d%lf", &n, &k, &d) && (n + k + d > EPS)) {
for (int i = ; i < n; i++) scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[n] = pt[];
double ans = 0.0, area = polyArea(pt, n);
for (int i = , end = << n; i < end; i++) {
if (cntBit(i) <= k) {
// cout << i << endl;
ans = max(ans, area - work(i, n, d));
}
if (sgn(ans - area) == ) break;
}
printf("%.2f\n", ans);
}
return ;
}
能通过POJ的代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int N = ;
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
} pt[N], poly[N];
template<class T> T sqr(T x) { return x * x;} typedef Point Vec;
Vec operator + (Vec a, Vec b) { return Vec(a.x + b.x, a.y + b.y);}
Vec operator - (Vec a, Vec b) { return Vec(a.x - b.x, a.y - b.y);}
Vec operator * (Vec a, double p) { return Vec(a.x * p, a.y * p);}
Vec operator / (Vec a, double p) { return Vec(a.x / p, a.y / p);}
const double EPS = 1e-;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);} inline double dotDet(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double crossDet(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double dotDet(Point o, Point a, Point b) { return dotDet(a - o, b - o);}
inline double crossDet(Point o, Point a, Point b) { return crossDet(a - o, b - o);}
inline double vecLen(Vec x) { return sqrt(dotDet(x, x));}
inline Vec normal(Vec x) { return Vec(-x.y, x.x) / vecLen(x);} struct DLine {
Point p;
Vec v;
double ang;
DLine() {}
DLine(Point p, Vec v) : p(p), v(v) { ang = atan2(v.y, v.x);}
bool operator < (const DLine &L) const { return ang < L.ang;}
DLine move(double x) { return DLine(p + normal(v) * x, v);}
} dl[N]; inline bool onLeft(DLine L, Point p) { return crossDet(L.v, p - L.p) > ;}
inline Point dLineIntersect(DLine a, DLine b) { return a.p + a.v * (crossDet(b.v, a.p - b.p) / crossDet(a.v, b.v));} struct Poly {
vector<Point> pt;
Poly() { pt.clear();}
~Poly() {}
Poly(vector<Point> &pt) : pt(pt) {}
Point operator [] (int x) const { return pt[x];}
int size() { return pt.size();}
double area() {
if (pt.size() < ) return 0.0;
double ret = 0.0;
for (int i = , sz = pt.size(); i < sz; i++)
ret += crossDet(pt[i], pt[(i + ) % sz]);
return fabs(ret / 2.0);
}
} ; Point p[N];
DLine q[N], tmpDL[N];
int halfPlane(DLine *L, int n) {
for (int i = ; i < n; i++) tmpDL[i] = L[i];
sort(L, L + n);
int fi, la;
q[fi = la = ] = L[];
for (int i = ; i < n; i++) {
while (fi < la && !onLeft(L[i], p[la - ])) la--;
while (fi < la && !onLeft(L[i], p[fi])) fi++;
q[++la] = L[i];
if (fabs(crossDet(q[la].v, q[la - ].v)) < EPS) {
la--;
if (onLeft(q[la], L[i].p)) q[la] = L[i];
}
if (fi < la) p[la - ] = dLineIntersect(q[la - ], q[la]);
}
for (int i = ; i < n; i++) L[i] = tmpDL[i];
while (fi < la && !onLeft(q[fi], p[la - ])) la--;
if (la < fi) return ;
p[la] = dLineIntersect(q[la], q[fi]);
int ret = ;
for (int i = fi; i <= la; i++) poly[ret++] = p[i];
return ret;
} double polyArea(Point *p, int n) {
if (n < ) return 0.0;
double sum = 0.0;
p[n] = p[];
Point O = Point(0.0, 0.0);
for (int i = ; i < n; i++) sum += crossDet(O, p[i], p[i + ]);
return fabs(sum / 2.0);
} double work(int st, int n, double d) {
// cout << st << endl;
for (int i = ; i < n; i++) dl[i] = DLine(pt[i], pt[i + ] - pt[i]).move((st & ( << i)) == ? 0.0 : d);
int tmp = halfPlane(dl, n);
return polyArea(poly, tmp);
// Poly tmp = halfPlane(dl, n);
// return tmp.area();
} int cntBit(int x) {
int cnt = ;
while (x > ) {
if (x & ) cnt++;
x >>= ;
}
return cnt;
} const double FINF = 1e100;
double ans, d;
void dfs(int id, int tt, int used, int k) {
if (id >= tt) {
int tmp = halfPlane(dl, tt);
ans = min(ans, polyArea(poly, tmp));
return ;
}
dl[id] = DLine(pt[id], pt[id + ] - pt[id]);
dfs(id + , tt, used, k);
if (used >= k) return ;
dl[id] = DLine(pt[id], pt[id + ] - pt[id]).move(d);
dfs(id + , tt, used + , k);
} int main() {
// freopen("in", "r", stdin);
int n, k;
while (~scanf("%d%d%lf", &n, &k, &d) && (n + k + d > EPS)) {
for (int i = ; i < n; i++) scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[n] = pt[];
double area = polyArea(pt, n);
ans = FINF;
dfs(, n, , k);
printf("%.2f\n", area - ans);
}
return ;
}
——written by Lyon
poj 1271 && uva 10117 Nice Milk (半平面交)的更多相关文章
- poj 2451 Uyuw's Concert (半平面交)
2451 -- Uyuw's Concert 继续半平面交,这还是简单的半平面交求面积,不过输入用cin超时了一次. 代码如下: #include <cstdio> #include &l ...
- poj 2451 Uyuw's Concert(半平面交)
Uyuw's Concert Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8580 Accepted: 3227 De ...
- POJ - 1474 :Video Surveillance (半平面交-求核)
pro:顺时针给定多边形,问是否可以放一个监控,可以监控到所有地方,即问是否存在多边形的核. 此题如果两点在同一边界上(且没有被隔段),也可以相互看到. sol:求多边形是否有核.先给直线按角度排序, ...
- POJ 3384 Feng Shui(计算几何の半平面交+最远点对)
Description Feng shui is the ancient Chinese practice of placement and arrangement of space to achie ...
- POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...
- poj 3335(半平面交)
链接:http://poj.org/problem?id=3335 //大牛们常说的测模板题 ------------------------------------------------- ...
- poj 1755 半平面交+不等式
Triathlon Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6461 Accepted: 1643 Descrip ...
- poj 1279 半平面交核面积
Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6668 Accepted: 2725 Descr ...
- poj 3335 Rotating Scoreboard(半平面交)
Rotating Scoreboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6420 Accepted: 25 ...
随机推荐
- Unity 同一Text文本修改不同的字体大小和字体颜色
类似Html,在color和size对应的<>str</>中,就能修改str的相关属性, 下面的代码就是把time改为字体颜色为红色,大小为40: 而前面的"Time ...
- 【logo】设计书籍的logo
一个女孩发光般的看着,眼前发现的书籍 含义:这本书就是女孩想要得到的书籍 平台的宗旨:就是让学生能够得到想要的书籍 书籍来源:其他学生的书籍 目的:实现书籍回收再利用,同时让接受者低于市场价得到
- 【GDOI2017 day1】取石子游戏 线段树+区间合并
题面 如果给你一棵有根树,树根为 1,并且树的每个结点上有一个权值,现在我想知道每个点,除它所在子树以外的结点权值集合的 mex,怎么做呢? 在这里,mex 是定义在集合上的函数,mex(S) 表示 ...
- java jsp j2ee
1. JavaScript用于编写嵌入在网页文档中的程序,它由浏览器负责解释和执行,可以在网页上产生动态的显示效果和实现与用户交互的功能,譬如,让一串文字跟着鼠标移动,让一个图标在网页漂浮移动,验证用 ...
- js实现动态计数效果
下面附有数字图片和数字边框图 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 《2019上半年DDoS攻击态势报告》发布:应用层攻击形势依然严峻,海量移动设备成新一代肉鸡
2019年上半年,阿里云安全团队平均每天帮助用户防御2500余次DDoS攻击,与2018年持平.目前阿里云承载着中国40%网站流量,为全球上百万客户提供基础安全防御.可以说,阿里云上的DDoS攻防态势 ...
- MSSQL→ 03:数据库操作
一.数据库的操作 1.1.新增 使用SSMS图形界面创建数据库 在SQL Server 2008 中,通过SQL Server Management Studio 创建数据库 使用Transact-S ...
- Data Lake Analytics中OSS LOCATION的使用说明
前言 Data Lake Analytic(后文简称 DLA)可以帮助用户通过标准的SQL语句直接对存储在OSS.TableStore上的数据进行查询分析. 在查询前,用户需要根据数据文件的格式和内容 ...
- 【JZOJ4884】【NOIP2016提高A组集训第12场11.10】图的半径
题目描述 mhy12345学习了树的直径,于是开始研究图的半径,具体来说,我们需要在图中选定一个地方作为中心,其中这个中心有可能在路径上. 而这个中心的选址需要能够使得所有节点达到这个中心的最短路里面 ...
- zabbix3.4.x添加短信报警
一.修改zabbix_server.conf #vi /etc/zabbix/zabbix_server.conf 去掉注释: ### Option: AlertScriptsPath # Full ...