Cake slicing

给你一个矩形大小,和每个樱桃的坐标,现在让你去切使得切之后的小矩形包含一个樱桃,每次切的代价是切痕的长度,问你最小代价是多少

思路:

首先要明白一点,不能切除一个不含樱桃的矩形,因为那样完全没必要切,只是在浪费代价,那么就需要递归求解这个问题了,把这个问题扔给子局面(分),并统计并返回最优解(合),这运用到分治思想,也可以说是记忆化搜索+状态转移

具体如何分?就是枚举所有合法的切的方式,比如从中间竖着切,那么他的代价就是两个子局面的代价和加上这次切的代价

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 23;
const int INF = 0x3f3f3f3f;
int dp[maxn][maxn][maxn][maxn],flag[maxn][maxn],sum[maxn][maxn],m,n,k;
void getsum(){
memset(sum,0,sizeof sum);
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
sum[i][j] = sum[i][j-1] +flag[i][j];
}
}
}
int findnum(int l,int r,int u,int d){
int ans=0;
for(int i=u;i<=d;i++){
ans += sum[i][r] - sum[i][l-1];
}
return ans;
}
int dfs(int l,int r,int u,int d){
int &ans = dp[l][r][u][d];
if(ans>=0) return ans;
int num = findnum(l,r,u,d);
if(!num) return ans = INF;
if(num==1) return ans = 0;
ans = INF;
for (int i=l;i<r;i++){
ans = min(dfs(l,i,u,d)+dfs(i+1,r,u,d)+(d-u)+1,ans);
}
for (int i=u;i<d;i++){
ans = min(dfs(l,r,u,i)+dfs(l,r,i+1,d)+(r-l)+1,ans);
}
return ans;
}
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
int kase=0;
int a,b;
while(cin>>m>>n>>k){
memset(dp,-1,sizeof dp);
memset(flag,0,sizeof flag);
for(int i=1;i<=k;i++){
cin>>a>>b;flag[a][b]=1;
}
getsum();
printf("Case %d: %d\n",++kase,dfs(1,n,1,m));
}
return 0;
}











UVA1629_Cake slicing的更多相关文章

  1. 从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数

    一.多态 多态性是面向对象程序设计的重要特征之一. 多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为. 多态的实现: 函数重载 运算符重载 模板 虚函数 (1).静态绑定与动态绑 ...

  2. BZOJ 2196: [Usaco2011 Mar]Brownie Slicing( 二分答案 )

    二分答案就可以了.... ----------------------------------------------------------------------- #include<cst ...

  3. Numpy 笔记: 多维数组的切片(slicing)和索引(indexing)【转】

    目录 切片(slicing)操作 索引(indexing) 操作 最简单的情况 获取多个元素 切片和索引的同异 切片(slicing)操作 Numpy 中多维数组的切片操作与 Python 中 lis ...

  4. Python学习--字符串slicing

    Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages Python indexe ...

  5. ZOJ 3427 Array Slicing (scanf使用)

    题意  Watashi发明了一种蛋疼(eggache) 语言  你要为这个语言实现一个 array slicing 函数  这个函数的功能是 有一个数组初始为空  每次给你一个区间[ l, r)  和 ...

  6. Object Slicing in C++

    In C++, a derived class object can be assigned to base class, but the other way is not possible. cla ...

  7. [Python Cookbook] Numpy Array Slicing and Indexing

    1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...

  8. [Python] Slicing Lists

    In addition to accessing individual elements from a list we can use Python's slicing notation to acc ...

  9. 《流畅的Python》 Sequence Hacking, Hashing and Slicing(没完成)

    序列修改,散列和切片 基本序列协议:Basic sequence protocol: __len__ and __getitem__ 本章通过代码讨论一个概念: 把protocol当成一个正式接口.协 ...

随机推荐

  1. 基于Idea从零搭建一个最简单的vue项目

    一.需要了解的基本知识 node.js Node.js是一个Javascript运行环境(runtime),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.N ...

  2. Gradle 入门--只此一篇

    是什么? 在语法上是基于Groovy语言的(Groovy 是一种基于JVM的敏捷开发语言,可以简单的理解为强类型语言java的弱类型版本),在项目管理上是基于Ant和Maven概念的项目自动化建构工具 ...

  3. Zookeeper之启动常见错误及解决方法

    Zookeeper启动后,有时候没有真正的启动,那我们如何查找错误呢,就可以查看zookeeper目录下面的zookeeper.out文件,就可以查看到错误了.zookeeper.out文件比较的重要 ...

  4. Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法. 重启服务

    今天用Oracle突然出现Oracle ORA-01033: ORACLE initialization or shutdown in progress. 想起前两天删掉了几个DBF文件,幸好还没有清 ...

  5. Python之读写文本数据

    知识点不多 一:普通操作  # rt 模式的 open() 函数读取文本文件 # wt 模式的 open() 函数清除覆盖掉原文件,write新文件 # at 模式的 open() 函数添加write ...

  6. 微信小程序(16)-- bindtap,catchtap事件绑定的区别

    bindtap,catchtap事件绑定的区别,这里就涉及冒泡事件了.bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡. logs.wxml <view cl ...

  7. 初学Java 使用输入对话框

    import javax.swing.JOptionPane; public class ComputeLoanUsingInputDialog { public static void main(S ...

  8. Linux ct6.5安装rabbitmq

    yum install gcc glibc-devel make ncurses-devel openssl-devel xmlto 1.Erlang安装配置 下载安装包,地址http://www.e ...

  9. bzoj4455 & loj2091 [Zjoi2016]小星星 容斥原理+树形DP(+状压DP?)

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4455 https://loj.ac/problem/2091 题解 很不错的一道题.(不过在当 ...

  10. ubuntu 自带截图工具快捷键盘

    PrtSc – 获取整个屏幕的截图并保存到 Pictures 目录. Shift + PrtSc – 获取屏幕的某个区域截图并保存到 Pictures 目录. Alt + PrtSc –获取当前窗口的 ...