HDU 5473 There was a kingdom 凸包 DP
题意:
给出平面上n个点的坐标,选k个点,使得这k个点围起来的面积最大.
分析:
参考了 叉姐的分析 和 不慌不忙菊苣的代码
思路我都懂,但是DP的部分还是不太会写.
我体会了一下其中含义,也许这样可能会好理解一点:
因为求出来的凸包的点数是固定的,所能选的点数也是固定的,那么不选的点的数量也是固定的.
可以反过来考虑:少选一个点,就要损失凸包上的一块面积.
假设\(d(i,j)\)表示考虑了前\(i\)个点,选了\(j\)个点,所损失的最少面积.
第\(i\)个点的前一个点是\(i'\),损失的面积为\(S_{cut}\),那么\(d(i,j)=min(d(i,j),d(i',j-1)+S_{cut})\)
最后答案就是凸包总面积减去最后损失的最小面积.
损失的面积是一小块一小块三角形累加起来的.
上个图片仅供参考:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 100 + 10;
struct Point
{
LL x, y;
Point(LL x = 0, LL y = 0) : x(x), y(y) {}
void read() { scanf("%lld%lld", &x, &y); }
};
Point operator - (const Point& A, const Point& B) {
return Point(A.x - B.x, A.y - B.y);
}
bool operator < (const Point& A, const Point& B) {
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
LL Cross(const Point& A, const Point& B) {
return A.x * B.y - A.y * B.x;
}
vector<Point> p, con;
vector<Point> ConvexHull() {
sort(p.begin(), p.end());
int n = p.size();
vector<Point> ch(n);
int m = 0;
for(int i = 0; i < n; i++) {
while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) < 0) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n - 2; i >= 0; i--) {
while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) < 0) m--;
ch[m++] = p[i];
}
if(n > 1) m--;
ch.resize(m);
return ch;
}
int n, k;
LL d[maxn][maxn];
bool vis[maxn];
int main()
{
//freopen("in.txt", "r", stdin);
int _; scanf("%d", &_);
for(int __ = 1; __ <= _; __++) {
scanf("%d%d", &n, &k);
p.resize(n);
for(int i = 0; i < n; i++) p[i].read();
con = ConvexHull();
int sz = con.size();
if(sz <= 2 || k <= 2) { printf("0\n"); continue; }
LL totarea = 0;
for(int i = 2; i < sz; i++) totarea += Cross(con[i-1]-con[0], con[i] - con[0]);
if(k >= sz) {
printf("%lld\n", totarea);
continue;
}
LL ans = 0x3f3f3f3f3f3f3f3f;
memset(vis, false, sizeof(vis));
int times = min(10 * n / k, sz);
while(times--) {
int s = rand() % sz;
while(vis[s]) s = rand() % sz;
vis[s] = true;
memset(d, 0x3f, sizeof(d));
d[0][0] = 0;
for(int i = 1; i <= sz; i++) {
int p0 = (s + i) % sz;
LL cut = 0;
for(int j = i - 1; j >= 0; j--) {
int p2 = (s + j) % sz;
int p1 = (p2 + i) % sz;
cut += Cross(con[p1] - con[p0], con[p2] - con[p0]);
for(int l = k; l > 0; l--)
d[i][l] = min(d[i][l], d[j][l-1] + cut);
}
}
ans = min(ans, d[sz][k]);
}
printf("Case #%d: %lld\n", __, totarea - ans);
}
return 0;
}
HDU 5473 There was a kingdom 凸包 DP的更多相关文章
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- Codeforces 835 F Roads in the Kingdom(树形dp)
F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...
- HDU 1028 Ignatius and the Princess III dp整数划分
http://acm.hdu.edu.cn/showproblem.php?pid=1028 dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和. 1 = 1 2 = 1 + 1 . ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom (DP)
Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...
随机推荐
- @Primary 使用
造轮子的一个小小的发现 当一个接口被两个service实现时,controller调用接口实现功能,会报错,提示开发者指定service,官方是建议你使用@Qualifier来区分的,但是,总有另一种 ...
- MyBatis配置文件之概述
MyBatis配置文件所有元素 <?xml version="1.0" encoding="UTF-8"?> <configuration&g ...
- 适配器模式和php实现
1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...
- cf519D. A and B and Interesting Substrings(前缀和)
题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...
- ios 利用runtime任性跳转
在开发项目中,会有这样变态的需求: 推送:根据服务端推送过来的数据规则,跳转到对应的控制器 feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦 ...
- js读取excel数据后的时间格式转换
使用xlsx.full.min.js 获取excel的日期数据为:37858: 拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换: function formatDate(num ...
- javaSe-字符型和布尔型
其实java数据类型一节就可以全部写完了,为什么还需要字符型和布尔型呢,原因是这俩个都很重要: 字符型用char表示,字符分三种: 普通字符:char a = 'a',普通字符表示一个普通的字符,没有 ...
- 在Linux系统里安装Virtual Box的详细步骤
今天我试图在Linux 服务器上安装Kyma时,遇到如下错误消息: E1009 23:51:37.685891 358 start.go:174] Error starting host: Error ...
- Beta冲刺(周四)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...
- 设置DataGridView单元格的文本对齐方式
实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性 //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...