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 (半平面交)的更多相关文章

  1. poj 2451 Uyuw's Concert (半平面交)

    2451 -- Uyuw's Concert 继续半平面交,这还是简单的半平面交求面积,不过输入用cin超时了一次. 代码如下: #include <cstdio> #include &l ...

  2. poj 2451 Uyuw's Concert(半平面交)

    Uyuw's Concert Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8580   Accepted: 3227 De ...

  3. POJ - 1474 :Video Surveillance (半平面交-求核)

    pro:顺时针给定多边形,问是否可以放一个监控,可以监控到所有地方,即问是否存在多边形的核. 此题如果两点在同一边界上(且没有被隔段),也可以相互看到. sol:求多边形是否有核.先给直线按角度排序, ...

  4. POJ 3384 Feng Shui(计算几何の半平面交+最远点对)

    Description Feng shui is the ancient Chinese practice of placement and arrangement of space to achie ...

  5. 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 ...

  6. poj 3335(半平面交)

    链接:http://poj.org/problem?id=3335     //大牛们常说的测模板题 ------------------------------------------------- ...

  7. poj 1755 半平面交+不等式

    Triathlon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6461   Accepted: 1643 Descrip ...

  8. poj 1279 半平面交核面积

    Art Gallery Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6668   Accepted: 2725 Descr ...

  9. poj 3335 Rotating Scoreboard(半平面交)

    Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 25 ...

随机推荐

  1. 一款你不容错过的Laravel后台管理扩展包 —— Voyager – Laravel学院

    1.简介 Voyager是一个你不容错过的Laravel后台管理扩展包,提供了CRUD操作.媒体管理.菜单构建.数据管理等操作. 官网:https://the-control-group.github ...

  2. 在VMware安装Windows server 2003操作系统帮助文档

    在VMware上安装Windows server 2003操作系统,及VMware上三种网络连接模式(以VMware 10为例) 一.在Windows上安装VMware 10虚拟机软件 1.首先在Wi ...

  3. 学习JDK1.8集合源码之--LinkedHashSet

    1. LinkedHashSet简介 LinkedHashSet继承自HashSet,故拥有HashSet的全部API,LinkedHashSet内部实现简单,核心参数和方法都继承自HashSet,只 ...

  4. 7 种 Javascript 常用设计模式学习笔记

    7 种 Javascript 常用设计模式学习笔记 由于 JS 或者前端的场景限制,并不是 23 种设计模式都常用. 有的是没有使用场景,有的模式使用场景非常少,所以只是列举 7 个常见的模式 本文的 ...

  5. MaxCompute推出面向开发者的专属版本,普惠大数据开发者

    3月20号,阿里云正式对外宣布推出MaxCompute产品的新规格-开发者版.MaxCompute开发者版是阿里云大数据计算服务发布的开发者专属版本.区别于原有的按量付费.按CU预付费规格,开发者版是 ...

  6. Codeforces 439C

    题目链接 比赛时间没能通过==, 只能说明自己代码写的太不严谨咯! 解题思想就是贪心 先判断无解的情况: 1. 奇数不够,因为偶数是无法凑成奇数的 2. 偶数不够,2个奇数可以凑成一个偶数 3. 在奇 ...

  7. yum源配置及详解

      红帽系列中,进行软件安装可以有三种方法,编译安装,rpm包安装,和yum源安装.其中yum方法安装最简单,因为它可以自动解决软件包之间的依赖关系... 一.常用yum源 yum源可以来源于多种文件 ...

  8. 简单利用XSS获取Cookie信息实例演示

    简单利用XSS获取Cookie信息实例演示   首先要找到一个有XXS的站,这里就不整什么大站了,谷歌一下inurl:'Product.asp?BigClassName',搜出来的命中率也比较高.随便 ...

  9. pycharm 永久注册

    pycharm 使用又到期了,找到了破解版亲测(到期日期2099/12/31),绝对简单好用,直接使用步骤: 一,下载pycharm(windows版):  https://www.jetbrains ...

  10. docker search

    命令:docker search [root@iZ943kh74qgZ ~]# docker search --help Usage: docker search [OPTIONS] TERM Sea ...