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的更多相关文章

  1. [总结-动态规划]经典DP状态设定和转移方程

    马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...

  2. Windows 7上执行Cake 报错原因是Powershell 版本问题

    在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...

  3. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  4. Scalaz(15)- Monad:依赖注入-Reader besides Cake

    我们可以用Monad Reader来实现依赖注入(dependency injection DI or IOC)功能.Scala界中比较常用的不附加任何Framework的依赖注入方式可以说是Cake ...

  5. uva10167 Birthday Cake

    Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them. Now we put t ...

  6. HDU 4762 Cut the Cake(公式)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. Brute Force --- UVA 10167: Birthday Cake

     Problem G. Birthday Cake  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudg ...

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

  9. hdu acmsteps 2.1.3 Cake

    Cake Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

随机推荐

  1. hdu 2412 Party at Hali-Bula【树形dp】

    HDU 2412 和poj 2342(hdu 1520)差不多,多了一个判断最优解是(Yes)否(No)唯一.关键问题也在这个判断最优解是否唯一上. 先定义dp[u][2],表示选(dp[][1])或 ...

  2. oracle函数 MAX([distinct|all]x)

    [功能]统计数据表选中行x列的最大值. [参数]all表示对所有的值求最大值,distinct只对不同的值求最大值,默认为all 如果有参数distinct或all,需有空格与x(列)隔开. [参数] ...

  3. Python中进制转换函数的使用

    Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...

  4. php 对接java短信接口带有英文逗号就无法通过

    在对接短息接口时,对方是java接口,要求content两次编码 短信内容(Content)发起请求前必须进行URL转码.例如对于短信内容为“中文短信abc”,转码过程如下(java语言): Stri ...

  5. oracle函数 NLS_INITCAP(x[,y])

    [功能]返回字符串并将字符串的第一个字母变为大写,其它字母小写; [参数]x字符型表达式 [参数]Nls_param可选, 查询数据级的NLS设置:select * from nls_database ...

  6. @atcoder - Japanese Student Championship 2019 Qualification - E@ Card Collector

    目录 @description@ @solution@ @accepted code@ @details@ @description@ N 个卡片放在 H*W 的方格图上,第 i 张卡片的权值为 Ai ...

  7. LRJ-Example-06-13-Uva1103

    pic[][]数组存储每个点的值,0或1,输入时在原图的周围加了一圈0. color[][]数组存储每个点的color值,从1开始,dfs(row, col, c) 负责对每个点着色,连通在一起的连通 ...

  8. hdu 1050 Moving Tables (Greedy)

    Problem - 1050 过两天要给12的讲贪心,于是就做一下水贪心练习练习. 代码如下: #include <cstdio> #include <iostream> #i ...

  9. js实现方块的碰撞检测

    文章地址:https://www.cnblogs.com/sandraryan/ 个人感觉.方块的碰撞检测比圆形麻烦~~ <!DOCTYPE html> <html lang=&qu ...

  10. idea actiBPM插件生成png文件 (解决没有Diagrams或Designer选项问题)

    版权声明:随便转, 记得给个链接过来哦 https://blog.csdn.net/wk52525/article/details/79362904 idea对activiti工作流的支持没有ecli ...