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 ...
随机推荐
- 杨柳目-杨柳科-Info-新闻:杨柳絮造成的危害相关视频资讯
ylbtech-杨柳目-杨柳科-Info-新闻:杨柳絮造成的危害相关视频资讯 1. 杨絮返回顶部 1. 2. https://www.baidu.com/sf/vsearch?pd=video&a ...
- 【JZOJ4861】【NOIP2016提高A组集训第7场11.4】推冰块
题目描述 Dpstr最近迷上了推冰块.冰地是一个n行m列的网格区域,第i行第j列的格子记为(i,j),也就是左上角为(1,1),右下角为(n,m).每个格子可能是冰面.障碍物.减速带三者之一.其中,冰 ...
- QT_OPENGL-------- 5.model
在qt中实现opengl obj模型导入: main.cpp #include<GL/glew.h> #include <GLFW/glfw3.h> #include<s ...
- HashSet的运用
TestHashSet.java package com.sxt.set1; /* * Set接口 唯一:元素唯一(不重复) * 无序:不是按照添加的顺序显示数据 * 采用哈希表的方式存储 * 根据哈 ...
- Libev源码分析02:Libev中的IO监视器
一:代码流程 在Libev中,启动一个IO监视器,等待该监视器上的事件触发,然后调用该监视器的回调函数.整个的流程是这样的: 首先调用ev_default_loop初始化struct ev_loop ...
- Libev源码分析01:Libev中的监视器结构(C结构体实现继承)
在Libev的源码中,用到了一种用C实现类似C++中继承的技巧,主要是用宏和结构体实现. 在Libev中,最关键的数据结构就是各种监视器,比如IO监视器,信号监视器等等.这些监视器的多数成员都是一样的 ...
- 根据User Agent参数的各个字段Mozilla/5.0/4.0-AppleWebKit/Chrome/Safari/Firefox/Opera/MSIE来确定/判断客户端使用什么浏览器
下面给你一一解答以及给你介绍: //Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/533.21.1 (KHTML, like ...
- Porject Euler Problem 6-Sum square difference
我的做法就是暴力,1+...+n 用前n项和公式就行 1^2+2^2+....+n^2就暴力了 做完后在讨论版发现两个有趣的东西. 一个是 (1+2+3+...+n)^2=(1^3)+(2^3)+(3 ...
- SuperSocket AppServer 的两个事件: NewSessionConnected 和 SessionClosed
订阅事件: appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConn ...
- 运行项目npm run dev时报错: ~Error: Cannot find module 'webpack-cli/bin/config-yargs', 原因是
webpack@3.X运行项目npm run dev时报错: ~Error: Cannot find module 'webpack-cli/bin/config-yargs' 我的原因是: web ...