10. 贪心与动态规划专题(一)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
14/31
贪心算法的基本步骤
1、从问题的某个初始解出发。 、从问题的某个初始解出发。 2、采用循环语句,当可以向求解目标前进一 、采用循环语句, 步时,就根据局部最优策略, 步时,就根据局部最优策略,得到一个部 分解,缩小问题的范围或规模。 分解,缩小问题的范围或规模。 3、将所有部分解综合起来,得到问题的最终 、将所有部分解综合起来, 解。
• Sa来自百度文库ple Output
10 20 30
11/31
算法分析:
1、如果没有交叉,总时间应该是多少? 如果没有交叉,总时间应该是多少? 影响搬运时间的因素是什么? 2、影响搬运时间的因素是什么? 3、如果每趟处理都包含最大重叠,处理后的 如果每趟处理都包含最大重叠, 效果是什么? 效果是什么? 4、得出什么结论? 得出什么结论?
4/31
所谓“贪心算法”是指:
在对问题求解时,总是作出在当前看来是最好的选择。 在对问题求解时,总是作出在当前看来是最好的选择。 当前看来是最好的选择 也就是说,不从整体上加以考虑, 也就是说,不从整体上加以考虑,它所作出的仅仅是 在某种意义上的局部最优解(是否是全局最优, 在某种意义上的局部最优解(是否是全局最优,需要 证明)。 证明)。
ACM算法与程序设计 ACM算法与程序设计
第十讲
贪心与动态规划专题 (一)
导引问题:FatMouse' Trade
2/31
• Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
13/31
if(s>d) { temp=s; s=d; d=temp; } for(k=s;k<=d;k++) P[k]++; } min=-1; for(j=0;j<200;j++) if(P[j]>min) min=P[j]; printf("%d\n", min*10); } return 0; }
9/31
• Input
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above.
12/31
#include <iostream> using namespace std; int main() { int t,i,j,N,P[200]; int s,d,temp,k,min; scanf("%d",&t); for(i=0;i<t;i++) { for(j=0;j<200;j++) P[j]=0; scanf("%d",&N); for(j=0;j<N;j++) { scanf("%d %d",&s, &t); s=(s-1)/2; d=(d-1)/2;
• Description
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.
8/31
The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.
17/31
• Problem Description
Here is a famous story in Chinese history."That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others." "Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser.“ "Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian." "Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match." "It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"
6/31
特别说明: 特别说明:
若要用贪心算法求解某问题的整体最优解, 若要用贪心算法求解某问题的整体最优解,必须首先证 明贪心思想在该问题的应用结果就是最优解!! 明贪心思想在该问题的应用结果就是最优解!!
7/31
Moving Tables
http://acm.hdu.edu.cn/showproblem.php?pid=1050
5/31
• 当一个问题的最优解包含着它的子问题的最优 解时,称此问题具有最优子结构性质。 解时,称此问题具有最优子结构性质。 • 当一个问题具有最优子结构性质时,我们会想 当一个问题具有最优子结构性质时, 到用动态规划法去解它。 到用动态规划法去解它。但有时会有更简单有 效的算法。顾名思义, 效的算法。顾名思义,贪心算法总是作出在当 前看来是最好的选择。 前看来是最好的选择。也就是说贪心算法并不 从整体最优上加以考虑, 从整体最优上加以考虑,它所作出的选择只是 在某种意义上的局部最优选择。 在某种意义上的局部最优选择。 • 用贪心算法更简单,更直接且解题效率更高。 用贪心算法更简单,更直接且解题效率更高。 虽然贪心算法不是对所有问题都能得到整体最 优解, 优解,但对范围相当广的许多问题它能产生整 体最优解。如图的单源最短路径问题, 体最优解。如图的单源最短路径问题,最小生 成树问题等。在一些情况下, 成树问题等。在一些情况下,即使贪心算法不 能得到整体最优解, 能得到整体最优解,但其最终结果却是最优解 的很好的近似解。 的很好的近似解。
• Output
The output should contain the minimum time in minutes to complete the moving, one per line.
10/31
• Sample Input
3 4 10 20 30 40 50 60 70 80 2 13 2 200 3 10 100 20 80 30 50
3/31
Input The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. Output For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain. Sample Input 53 72 43 52 -1 -1 Sample Output 13.333
15/31
贪心算法都很简单吗?
看一道难一些的。 看一道难一些的。 2004年上海赛区 正式赛是简单题) 年上海赛区: (2004年上海赛区:正式赛是简单题)
16/31
ACMACM-ICPC Asia Regional, 2004, Shanghai
Ji— Tian Ji—The Horse Racing
相关文档
最新文档