博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1991 Turning in Homework(区间DP,大区间推出小区间的思想)好题,想法很独特
阅读量:4035 次
发布时间:2019-05-24

本文共 4148 字,大约阅读时间需要 13 分钟。

1、

2、题目大意:

Bessie要交作业,现在知道每个教师的位置,假设都位于X轴上,每份作业都有一个最早交的时间,知道公交车站位于坐标B的对面,假设他每走一个单位耗费时间是1,求他交完作业到达公交车站那最少的时间是什么?

3、题目分析

这道题目乍一看好像有思路,这跟吃草坪的拿到题目很像,但仔细已考虑就发现不是那么回事,现在我们不仅规定了起始点,而且规定了结束的位置,假如我们还是用dp[i][j][0],dp[i][j][1]来表示状态,那么我们能够求出1-n都交完作业的最少时间,但是不能够确定当前是在哪个位置,因为最后我们还要加上从当前位置到公交车站的距离,想了好久发现都不能实现,搜了一下别人的解题报告,发现了一种非常巧妙的解题思路

我们将dp[i][j][0]表示为i-j区间尚未完成交作业的最少时间,其中0代表只交了i作业,dp[i][j][1]同理就是只交了j作业,那么我们将很容易想出来,这是由大区间逐渐推出小区间的一个过程,最终的dp[i][i][0]和dp[i][i][1]就代表停留在i位置的最少时间,最后再加上i-B的距离就是到达公交车站的时间

dp[1][n][0]=max(a[1].x,a[1].t),这个很好理解,在1-n区间内只交了1作业

dp[1][n][1]=max(a[n].x,a[n].t),这个很好理解,在1-n区间内只交了n作业

4、题目:

Turning in Homework
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1010   Accepted: 444

Description

Bessie must turn in her homework for her C classes (1 <= C <= 1,000) at Moo U so that she still has time to chew the cud with her fellow classmates as they wait for the bus to go home.
Teachers accept homework submissions only after they have finished their classes and also cleaned the chalkboard, put away lab supplies,and so on. The input data tells the earliest time a teacher will accept homework.
Bessie starts at one end (distance 0) of a hallway H (1 <= H <= 1,000) meters long and walks at the rate of one meter per second to various classrooms (in any order she chooses) to turn in her homework. Each classroom is located along this hallway, as well as the door to the waiting area for the buses.
Given the location of both the exit and the classrooms and also the teachers' schedules, determine the earliest time that Bessie can exit the door to the waiting area for the buses. Bessie must turn in all her homework before exiting. The act of turning in the homework takes no time, by the way.

Input

* Line 1: Three integers: C, H, and B. B (0 <= B <= H) is the distance from the hall entrance to the bus waiting area.
* Lines 2..C+1: Each line contains two integers that describe a classroom where homework is to be submitted. The first integer (0..H) is the number of meters to the classroom from the hallway entrance. The second integer (0..10,000) is the first time (in seconds) that the teacher for that course will accept homework.

Output

* Line 1: A single integer: the earliest second that Bessie can exit the door to the waiting area for the buses.

Sample Input

4 10 38 94 213 168 12

Sample Output

22

Hint

Time   Action  0   Bessie walks to the classrooms 8 meters down the hall (at 8m)  8   Bessie waits 1 second  9   Bessie turns in the first set of homework   9   Bessie waits 3 seconds, thinking about cool hay in the summertime 12   Bessie turns in the other homework for this location 12   Bessie walks back to the classroom 4 meters down the hall (at 4m) 16   Bessie waits 5 seconds, thinking of a handsome bull she once met 21   Bessie turns in her homework 21   Bessie walks back to the classroom 1 meters down the hall (at 3m) 22   Bessie turns in her homework 22   Bessie exits, since this also the location of the bus exitThus, Bessie can leave at time 22.  No better schedule exists.

Source

5、AC代码:

 

#include
#include
#include
using namespace std;#define N 1005#define INF 10000005int dp[N][N][2];struct node{ int x; int t;} a[N];int cmp(node a,node b){ if(a.x==b.x) return a.t
=0; d--) { for(int i=1; i+d<=n; i++) { int s=i; int e=i+d; dp[s][e][0]=INF;// dp[s][e][0]=min(dp[s][e][0],max(dp[s-1][e][0]+a[s].x-a[s-1].x,a[s].t));// dp[s][e][0]=min(dp[s][e][0],max(dp[s][e+1][1]+a[e+1].x-a[e].x,a[e].t));// dp[s][e][1]=min(dp[s][e][1],max(dp[s][e+1][0]+a[e].x-a[s].x,a[e].t));// dp[s][e][1]=min(dp[s][e][1],max(dp[s-1][e][1]+a[e].x-a[s].x,a[s].t)); if(s-1>0) { dp[s][e][0]=min(dp[s][e][0],dp[s-1][e][0]+a[s].x-a[s-1].x); } if(e+1<=n) { dp[s][e][0]=min(dp[s][e][0],dp[s][e+1][1]+a[e+1].x-a[s].x); } if(dp[s][e][0]
0) { dp[s][e][1]=min(dp[s][e][1],dp[s-1][e][0]+a[e].x-a[s-1].x); } if(dp[s][e][1]

 

 

转载地址:http://twcdi.baihongyu.com/

你可能感兴趣的文章
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Word Break(python)
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java swing最简单实例(2) 往JFrame里面放一个容器或组件
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
从头开始学习JSP(3)——一些配置
查看>>
html常用标签快速检索
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>