ZOJ3537 Cake
ZOJ3537 Cake
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3537
题意:
给你几何形状的蛋糕,你需要按照顶点来切蛋糕,每次切蛋糕会产生一个贡献,\(val=cost_{i, j} *|x_i + x_j| * |y_i + y_j| % p\) 问你把蛋糕切成几个三角形所需要的最小花费
题解:
区间dp好题
首先我们维护 一个凸包,如果这个几何形状只有三个点,那么就不需要切,花费为0
然后维护出一个凸包出来,如果满足凸包的条件的话,我们就开始切三角形
很显然我们需要维护一个花费最小的三角形剖分
设置dp[i][j]为从顶点i到顶点j所围成凸多边形的最优解。
枚举切点k (i < k < j)
\(dp[i][j] = min(dp[i][k] + dp[k][j] + cost[i][k] + cost[k][j])\)
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct Point {
int x, y;
Point() {}
Point(int xx, int yy): x(xx), y(yy) {}
void read() {
scanf("%d%d", &x, &y);
}
bool operator < (const Point &other)const {
if(x == other.x)
return y < other.y;
return x < other.x;
}
Point operator - (const Point &other)const {
return Point(x - other.x, y - other.y);
}
};
Point P[400], ch[400];
int n, m;
double Cross(Point A, Point B) {
return A.x * B.y - A.y * B.x;
}
int ConvexHull() {
sort(P, P + n);
int cnt = 0;
for(int i = 0; i < n; i++) {
while(cnt > 1 && Cross(ch[cnt - 1] - ch[cnt - 2], P[i] - ch[cnt - 2]) <= 0) cnt--;
ch[cnt++] = P[i];
}
int k = cnt;
for(int i = n - 2; i >= 0; i--) {
while(cnt > k && Cross(ch[cnt - 1] - ch[cnt - 2], P[i] - ch[cnt - 2]) <= 0) cnt--;
ch[cnt++] = P[i];
}
if(n > 1) cnt--;
return cnt;
}
int calc(Point a, Point b) {
return (abs(a.x + b.x) * abs(a.y + b.y)) % m;
}
int f[505][505];
int dp[505][505];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
while(~scanf("%d%d", &n, &m)) {
for(int i = 0; i < n; i++) {
scanf("%d%d", &P[i].x, &P[i].y);
}
if(n == 3) {
puts("0");
continue;
}
if(ConvexHull() < n) {
printf("I can't cut.\n");
} else {
for(int i = 0; i < n; i++) {
for(int j = i + 2; j < n; j++) {
f[i][j] = f[j][i] = calc(ch[i], ch[j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
dp[i][j] = INF;
}
dp[i][(i + 1) % n] = 0;
}
for(int i = n - 3; i >= 0; i--) {
for(int j = i + 2; j < n; j++) {
for(int k = i + 1; k < j; k++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + f[i][k] + f[k][j]);
}
}
}
printf("%d\n", dp[0][n - 1]);
}
}
return 0;
}
ZOJ3537 Cake的更多相关文章
- [总结-动态规划]经典DP状态设定和转移方程
马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...
- Windows 7上执行Cake 报错原因是Powershell 版本问题
在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- Scalaz(15)- Monad:依赖注入-Reader besides Cake
我们可以用Monad Reader来实现依赖注入(dependency injection DI or IOC)功能.Scala界中比较常用的不附加任何Framework的依赖注入方式可以说是Cake ...
- uva10167 Birthday Cake
Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them. Now we put t ...
- HDU 4762 Cut the Cake(公式)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Brute Force --- UVA 10167: Birthday Cake
Problem G. Birthday Cake Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudg ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...
- hdu acmsteps 2.1.3 Cake
Cake Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
随机推荐
- Apache CarbonData1.3简介
CarbonData是一种高性能大数据存储方案,支持快速过滤查找和即席OLAP分析,已在20+企业生产环境上部署应用,其中最大的单一集群数据规模达到几万亿.针对当前大数据领域分析场景需求各异而导致的存 ...
- NEFU 118 n!后面有多少个0【数论】
http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=118 求n!后面有多少个0(1<=n<=1000000000) ...
- php配置文件php.ini中文详解
转自:http://www.cnblogs.com/hbl/archive/2008/02/15/1069367.html [PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而p ...
- 开发者说:Sentinel 流控功能在 SpringMVC/SpringBoot 上的实践
从用户的视角来感受一个开源项目的成长,是我们推出「开发者说」专栏的初衷,即在开发者进行开源项目选型时,提供更为立体的项目信息.专栏所有内容均来自作者原创/投稿,本文是「开发者说」的第6篇,作者 Jas ...
- Java之Socket与HTTP区别
我们都知道TCP/IP协议共分四层: ① 链路层,有时也称作数据链路层或网络接口层,通常包括操作系统中的设备驱动程序和计算机中对应的网络接口卡.它们一起处理与电缆(或其他任何传输媒介)的物理接口细节. ...
- oracle 通过内部函数提高SQL效率.
SELECT H.EMPNO,E.ENAME,H.HIST_TYPE,T.TYPE_DESC,COUNT(*) FROM HISTORY_TYPE T,EMP E,EMP_HISTORY H WHER ...
- @bzoj - 4379@ [POI2015] Modernizacja autostrady
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵无根树,边权都是1,请去掉一条边并加上一条新边,定义直径 ...
- hdu 5744 Keep On Movin (2016多校第二场)
Keep On Movin Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- iptables在我们的网络机房实现NAT共享上网
工作环境:上层代理192.168.60.6(4480),只授予教师机(192.168.62.111)使用该代理的权限 目标:不使用squid代理上网,而是使用NAT的方式上网 方法: 1) 确保停止教 ...
- [转]爬虫 selenium + phantomjs / chrome
目录 selenium 模块 安装 phantomjs 浏览器 安装 chromedriver 接口 安装 对比两个接口 整合使用 基本实例 常用属性方法 定位节点 节点操作 其他操作 实例解析 - ...