Help Jimmy (POJ 1661)
"Help Jimmy" 是在下图所示的场景上完成的游戏。
场景中包括多个长度和高度各不相同的平台。地面是最低的平台,高度为零,长度无限。
Jimmy老鼠在时刻0从高于所有平台的某处开始下落,它的下落速度始终为1米/秒。当Jimmy落到某个平台上时,游戏者选择让它向左还是向右跑,它跑动的速度也是1米/秒。当Jimmy跑到平台的边缘时,开始继续下落。Jimmy每次下落的高度不能超过MAX米,不然就会摔死,游戏也会结束。
设计一个程序,计算Jimmy到底地面时可能的最早时间。
Input
第一行是测试数据的组数t(0 <= t <= 20)。每组测试数据的第一行是四个整数N,X,Y,MAX,用空格分隔。N是平台的数目(不包括地面),X和Y是Jimmy开始下落的位置的横竖坐标,MAX是一次下落的最大高度。接下来的N行每行描述一个平台,包括三个整数,X1[i],X2[i]和H[i]。H[i]表示平台的高度,X1[i]和X2[i]表示平台左右端点的横坐标。1 <= N <= 1000,-20000 <= X, X1[i], X2[i] <= 20000,0 < H[i] < Y <= 20000(i = 1..N)。所有坐标的单位都是米。
Jimmy的大小和平台的厚度均忽略不计。如果Jimmy恰好落在某个平台的边缘,被视为落在平台上。所有的平台均不重叠或相连。测试数据保证问题一定有解。
Output
对输入的每组测试数据,输出一个整数,Jimmy到底地面时可能的最早时间。
Sample Input
1
3 8 17 20
0 10 8
0 10 13
4 14 3
Sample Output
23
#include<bits/stdc++.h> const int N = 1003; const int M = 20004; const int INF = 0x3f3f3f; using namespace std; struct Time { int x1, x2, h; }a[N]; int dp[N][2];//0表示第i的木板左边到底的最短时间,1表示右边的时间 //这个dp其实就是问题中所需要求的时间 int max_h, n; int cmp(Time c, Time b) { return c.h> b.h;//从大到小排列 } void LeftMinTime(int i) { //往左 int k = i + 1;//k是i木板的下一个,整体是从下往上推的 while (k <= n && a[i].h - a[k].h <= max_h) { if (a[i].x1 >= a[k].x1 && a[i].x1 <= a[k].x2) { //上面这个是上面的木板可以往左跳到下面木板的条件 dp[i][0] = a[i].h - a[k].h + min(dp[k][0] + a[i].x1 - a[k].x1, dp[k][1] + a[k].x2 - a[i].x1); //往左走是的时间是由两块板之间的距离,加上下一块板往左走或者往右走的最小值 return; } k++; } if (a[i].h - a[k].h > max_h) { //不可以到达下一个平台 dp[i][0] = INF; } else { //直接落地 dp[i][0] = a[i].h; } return; } void RightMinTime(int i) { //往右 int k = i + 1;//k是i木板的下一个,整体是从下往上推的 while (k <= n && a[i].h - a[k].h <= max_h) { if (a[i].x2 >= a[k].x1 && a[i].x2 <= a[k].x2) { //上面这个是上面的木板可以往左跳到下面木板的条件 dp[i][1] = a[i].h - a[k].h + min(dp[k][0] + a[i].x2 - a[k].x1, dp[k][1] + a[k].x2 - a[i].x2); //往左走是的时间是由两块板之间的距离,加上下一块板往左走或者往右走的最小值 return; } k++; } if (a[i].h - a[k].h > max_h) { //不可以到达下一个平台 dp[i][1] = INF; } else { //直接落地 dp[i][1] = a[i].h; } return; } int main() { int t, x, y, i; cin >> t; while (t--) { cin >> n >> x >> y >> max_h; //后面是边界的设置,注意这个边界特征就是地面。 a[0].x1 = -20000; a[0].x2 = 20000; //地面的边界 a[0].h = 0; //上面是地面,地面在第一层 a[1].x1 = a[1].x2 = x; a[1].h = y; //但是我有一个问题, for (i = 2; i <= n + 1; i++) { cin >> a[i].x1 >> a[i].x2 >> a[i].h; } sort(a, a + n + 2, cmp); memset(dp, 0, sizeof(dp)); //memset这个函数还是要学会用的的确很方便 for (i = n; i >= 0; i--) { //还有这个是从头开始的 LeftMinTime(i); RightMinTime(i); } int time = min(dp[0][0], dp[0][1]); //很明显是从下到上递归 cout << time << endl; return 0; } }
推荐这些文章:
题目大意:
有 \(n\) 块木板从左到右排成一行,有 \(m\) 个工匠对这些木板进行粉刷,每块木板至多被粉刷一次。
第 \(i\) 个木匠要么不粉刷,要么粉刷包含木板 \(S_i\) 且长度不超过 \(L_i\) 的连续的一段木板,每粉刷一块可以得到 \(P_i\) 的报酬。不同工匠的 \(S_i\) 不同。 请问如何安排能使工匠们获得的总报酬最多。
思路:
设 \(f_{i,j}\) 表示前 \(i\) 个人涂完前 \(j\) 段木板的最大报酬。则有:
\[f_{i,j}=\max(f_{i-1,j},f_{i,j-1},\max_{j-L_i\leq k \leq S_i-1}\{f...
第十三届蓝桥杯大赛软件赛省赛C/C++大学B组真题(先占个位子有时间补)
A
B
C
D
E
F
G
H
(虽然贴代码也挺好但就是想试试新装的插件)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
const int N = 5e4 + 10;
int h[N], e[N], ne[N], idx;
int n, m, r;
bool st[N]; // 避免一个炸弹被多次引爆
struct Node
{
int x,...
一维前缀和
例题
#include<iostream>
using namespace std;
const int N = 1e5+10;
int n,m;
int s[N],num[N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
cin >> num[i];
s[i] = s[i - 1] + num[i];
}
while(m -- )
{
int l,r...
code
#include<iostream>
#include<algorithm>
#include<complex>
#include<cstring>
using namespace std;
const int N=10000;
double a[N],b[N],c[N],d[N],e[N],f[N],t[N];//array t mean fly time
int main(){
ios::sync_with_stdio(false);
int n=0;
cin>>n;
for(int i=0;i<n;i+...
...
使用类做为Dictionary<T,K>的key需什么要求?
问题
<P> </P>
最佳回答
没有要求
...
题目链接:http://poj.org/problem?id=3230
1 #include<iostream>
2 #include<algorithm>
3 #include<cmath>
4 #include<cstring>
5 #include<cstdio>
6 #define inf 0x3f3f3f3f
7 using namespace std;
8 const int N = 105;
9
10 int dp[N][N], in[N][N], to[N][N];
11
12 int mai...
知识点
二级指针
int **myMalloc(int r, int c, int* returnSize, int** returnColumnSizes) {
int i;
int **ret = (int **)malloc( sizeof(int *) * r ); // (1)
*returnColumnSizes = (int *)malloc( sizeof(int) * r ); // (2)
*returnSize = r; // (3)
f...
正则表达式(?<=<(\w+)>).*(?=<\/\1>) 分组
问题
正则表达式(?<=<(\w+)>).*(?=<\/\1>)里面\1代表的是 (\w+) 还是 (?<=<(\w+)>) ?请解释一下,谢谢!
最佳回答
测试了下 是指 (\w+)
...
1. <build>
1.1 <resources>
编译保留 *.propertie s,*.xml
1.2 <plugins>
拷贝config目录
Java 1.8 编译
一般打包(无Spring)
Spring 打包
2. <properties>
编码 编译版本
1. <build>
1.1 <resources>
编译保留 *.propertie s,*.xml
<resource>
<directory>src/main/java</dire...
文章链接:https://www.dianjilingqu.com/51536.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。