Smart Thief 问题
问题:
实验室的老师发在群里的问题,不知道出处,下面给出一些自己的解答:
方法一:整数规划(只是想求一个结果)
建模:
将各房间看成一个变量Xi:X1,X2,…X12
权重为每个房间的profit:W[i]
目标:最大化所偷房间的权重和
限制条件:
X1+X2<=1,
X2+X3<=1,
…
X12+X1<=1
Xi属于{0,1}
(0,1)整数规划问题可以借助excel或其他的一些软件来解,这里采用excel:
方法2:动态规划
如图所示,从上至下,设共有n个房间,形成一个n个数的序列,i表示第i个房间,每个房间的profit为w[i],Fi表示前i个数的最优收获,则对于第i个房间有两种可能,最优收获方案包括i或不包括i,当i<n时,前i个数中的第一个数和最后一个数不相邻,则有Fi=max(w[i]+F(i-2),F(i-1)),当i=n时,情况有点特殊,即Fi=max(w[i]+F’(i-2),F[i-1]),因为这时第n个数与第一个数实际上是相邻的,所以F’(i-2)实际上表示从第二个数开始到第i-2个数的最优方案,所以可以实现一个F函数从第一个数开始递推得到第i个数的最优方案,主函数调用两次该函数求最优,一次是求从第一个数到第n-1个数的最优解,第二次调用时第二个数到第n-2个数的最优解,然后将前者的值与后者加上w[n]的值进行对比。
F函数总是由前两个状态的值得到第三个状态的解,如果只需要求得最大利润,相当于O(N)的时间复杂度,如果需要求最优方案,则需要维护每次求得的子问题的最优方案的值。
下面给出了一个简单的C++代码,但是在维护最优方案值上不够高效,时间复杂度为O(N^2)
(后面想到一种方法,不需要像下面代码那样每次对前两个最优方案的数组进行拷贝,可以实现一个二维数组a[n][n],a[i][j]=0或1,表示第i个数在前j个数的最优方案中是否被包含)
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>using namespace std;//input:profit array,number of rooms;
//output:step[],maxprofit;//step[n] step[i]=0 0r 1, means choose to steal or not respectivelyint Max_profit_range(vector<int> profit,vector<int> &step) //attain the maximum profit and the stepof the input profit array{int n = profit.size();int k1 = profit[0]; int k2,k3;if (n == 1) //n为1的特殊情况{step[0] = 0;return profit[0];}vector<int> s1(n);vector<int> s2(n);vector<int> s3(n);int maxprofit = 0;s1[0] = 1;if (profit[0] >= profit[1]){ s2[0] = 1; s2[1] = 0;k2 = profit[0];}else {s2[0] = 0;s2[1] = 1;k2 = profit[1];}for (int i = 2; i < n; i++) {if (profit[i] + k1 >= k2) //逐渐向后递推,由前两个状态的值得到第三个状态的值{s1[i] = 1;k3 = profit[i] + k1;s3 = s1;}else{k3 = k2;s3 = s2;}k1 = k2;k2 = k3;s1 = s2;s2 = s3;}step =s2;return k3;}void Max_profit(vector<int> profit){int n = profit.size();int max_profit=0,step;if (n <= 3){for (int i = 0; i < n; i++){if (max_profit < profit[i]){max_profit = profit[i];step = i;}}cout << "the maximum profit is: " << max_profit << endl;cout << "the step is:" << step+1 << ":" << profit[step] << endl;return;}auto it1 = profit.begin();auto it2 = profit.end();vector<int> profit1(it1 + 1, it2 - 2);vector<int> profit2(it1, it2 - 1);vector<int> step1(n,0),step2(n,0),step3(n,0);int k1, k2; k1=profit[n-1]+Max_profit_range(profit1, step1);k2=Max_profit_range(profit2, step2); //调用两次函数,将结果进行对比if (k1>=k2){cout << "the maximum profit is " << k1 << endl;cout << "the step is:" << endl;step3.insert(step3.begin()+1,step1.begin(),step1.end());step3[0] = 1;}else{cout << "the maximum profit is " << k2 << endl;cout << "the step is:" << endl;step3=step2;step3.insert(step3.end(), 0);}for (int i = 0; i < n; i++){if (step3[i] == 1)cout << i+1 << ":" << profit[i] << endl;}}int main()
{vector<int> profit{ 89, 55, 37, 6, 64, 99, 11, 23, 76, 4, 65, 9};Max_profit(profit);system("PAUSE");return 0;
}
结果输出
Smart Thief 问题相关推荐
- LeetCode-337 House Robber III
题目描述 The thief has found himself a new place for his thievery again. There is only one entrance to t ...
- 2018 ICPC Asia Jakarta Regional Contest
2018 ICPC Asia Jakarta Regional Contest 题号 题目 知识点 难度 A Edit Distance B Rotating Gear C Smart Thief D ...
- 【算法】【c++】打家劫舍问题
dp问题 House Robber II You are a professional robber planning to rob houses along a street. Each house ...
- Leetcode337. 盗贼问题
Leetcode337. House Robber III 题目 The thief has found himself a new place for his thievery again. The ...
- 如何安装和设置Kwikset Kevo Smart Lock
Traditional door locks work just fine, but if you want to add some smarts to the security of your ho ...
- 下一版本Windowsreg; CE 开发工具Smart Device Extensions for Microsoft Visual Studioreg; .NET...
初识 Smart Device Extensions Larry Roof Tonked.com 2001年10月23日 上个月我曾说过我会前往 Microsoft 学院,了解下一版本的小型工具的情况 ...
- Smart template的控件能否当成普通控件来用
我的同事问过我这个问题: 只要弄清楚Smart control的原理,就能回答这个问题. 答案是: smart control可以像普通的控件一样在xml view中被定义和使用,但是必须结合ODat ...
- Your Podfile has had smart quotes sanitised
2019独角兽企业重金招聘Python工程师标准>>> 1.无论是执行pod install还是pod update都卡在了Analyzing dependencies 或者 Upd ...
- input框选中时如何不出灰框_如何建立学习目标:这个SMART原则,你要了解,家长看懂教给孩子...
作者 | 好看的 原创 来川学习方法,学出幸福感 点击标题下方"来川学习方法"蓝字关注 我经常跟学生们说一句话:学霸都是赢在起跑线上的. 这个起跑线指的是什么?早点启蒙.多上补习班 ...
- 【翻译】A Next-Generation Smart Contract and Decentralized Application Platform
原文链接:https://github.com/ethereum/wiki/wiki/White-Paper 当中本聪在2009年1月启动比特币区块链时,他同时向世界引入了两种未经测试的革命性的新概念 ...
最新文章
- 靠Python数据分析已赚10w的本科生,附赠学习资料
- modelsim 各版本 网盘_阿里云盘官方下载-阿里云盘最新版本 v1.0.1
- 功能之前,感叹号有什么作用?
- c++ mysql 存储图片路径_3.用ADO实现图片在数据库中存储
- anaconda镜像
- 各种排序算法及其实现总结
- 《Python Cookbook 3rd》笔记(1.1):拆分序列后赋值给多个变量
- 实用常识 | 一篇文章记录如何轻松清理C盘,C盘又满了,继续盘它
- Oracle11g新特性:在线操作功能增强-Oracle11g的只读表功能
- 只需三种手段,将传统的网站的性能提高 24%!
- 无线服务器软件,无线局域网AAA服务器的软件设计与实现
- HTML5+CSS3从入门到精通
- 前端JS项目实战——瀑布流
- web浏览器和web服务器的协议是,浏览器是如何与Web服务器进行通信的
- Outlook html 图片白色空白,outlook签名设置_解决Outlook中的签名和邮件图片都显示空白的办法_outlook邮件空白...
- 制作仅在xy方向有周期性的夹层结构,且不使用约束平板
- 软件工程师典藏 C# 程序开发范例宝典 第3版
- 痛苦的事, 伤心的事, 一生只需要一次, 别折腾, 别忘痛- 人生, 软件, 所有人皆如是!...
- 时域同步平均TSA-学习笔记
- 7-4 银行排队问题之单窗口“夹塞”版 (30 分) C语言版