首页 > 其他分享 >POJ - 1661 Help Jimmy(DP)

POJ - 1661 Help Jimmy(DP)

时间:2023-04-07 11:03:44浏览次数:42  
标签:Jimmy return int Max leftTime POJ const find DP


题目大意:中文题

解题思路:因为是垂直下降,所以就可以将问题转变成从降落的平台到地面的最小时间
因为只能从平台的左边或者右边才可以离开平台,所以可以分别计算出从左边和从右边到达地面的最短时间,并记录

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010;
const int INF = 0x3f3f3f3f;

struct Board{
    int x, y, h;
}B[N];

int n, Max;
int L[N], R[N];

int cmp(const Board &a, const Board &b) {
    return a.h > b.h;
}

void init() {
    scanf("%d%d%d%d", &n, &B[0].x, &B[0].h, &Max)   ;
    B[0].y = B[0].x;

    for (int i = 1; i <= n; i++)
        scanf("%d%d%d", &B[i].x, &B[i].y, &B[i].h);
    sort(B, B + n + 1, cmp);
    memset(L, -1, sizeof(L));
    memset(R, -1, sizeof(R));
}

int dfs(int cur, int dir) {
    int x, h;
    h = B[cur].h;
    if (dir == 0)  x = B[cur].x;
    else x = B[cur].y;

    int find = -1;
    for (int i = cur + 1; i <= n; i++) {
        if (B[i].x <= x && B[i].y >= x ) {
            find = i;
            break;
        }
    }

    if (find == -1) {
        if (h > Max) return INF;
        else return h;
    }
    else if(h - B[find].h > Max) return INF;

    int leftTime = h - B[find].h + x - B[find].x;
    int rightTime = h - B[find].h + B[find].y - x;
    if (L[find] == -1)
        L[find] = dfs(find, 0);
    if (R[find] == -1)
        R[find] = dfs(find, 1);

    leftTime += L[find];
    rightTime += R[find];
    return leftTime < rightTime ? leftTime: rightTime;
}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        init();
        printf("%d\n", dfs(0, 0));
    }
    return 0;
}


标签:Jimmy,return,int,Max,leftTime,POJ,const,find,DP
From: https://blog.51cto.com/u_10970600/6175573

相关文章

  • POJ - 3616 Milking Time(DAG)
    题目大意:给出N头牛的产奶时间段和产奶量,每接完一头牛的奶后,需要休息R分钟问如何选择牛,才能使接到的产奶量达到最大解题思路:DAG,按产奶的结束时间大小排序#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintN=1010;structInterval{......
  • HDU 5479 Scaena Felix(DP)
    题目大意:给出一系列的由’(‘和’)’组成的字符串,现有一种操作,可以将括号的方向变反,但需要花费1现在问,需要花费多少的代价才能使该字符串的任意一个连续子串都不存在”()”的配对解题思路:用dp[i][0]表示第i个位置变成’(‘的代价,dp[i][1]表示第i个位置变成’)’的代价如果当前......
  • 两种保存状态的方法getSharedPreferences和onSaveInstanceState
    虽然这些东西很简单有时候还真的让你搞混@OverrideprotectedvoidonPause(){super.onPause();SharedPreferencesprefs=getSharedPreferences("X",MODE_PRIVATE);Editoreditor=prefs.edit();editor.putString("lastAct......
  • SQLiteOpenHelper&SharedPreferences练习
    目录结构:packagecom.dc.app;importjava.text.DecimalFormat;importjava.util.Locale;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.app.Dialog;importandroid.app.Notification;importandroid.app.Notificati......
  • udp协议的时间服务器
    #include<stdio.h>#include<stdlib.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<errno.h>#include<string.h>#include<time.h>constintmaxline=4096;char*sock_nt......
  • udp协议的获取时间的客户端
    #include<stdio.h>#include<stdlib.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<errno.h>#include<string.h>constintmaxline=4096;voiddg_cli(FILE*fp,intsockfd,stru......
  • 【wordpress】wordpress插件之自动采集发布工具
    前言安装好wordpress后,就要开始发布文章,由于之前的文章分散在各个平台,想要一个个拷贝过去,的确费时费力,所以想要一劳永逸的解决这个问题,就要用到今天介绍的这个采集工具插件安装搜索:FatRatCoolect然后点击现在安装如果因为网速慢下载不下来,可以直接到官网下载然后上传:cd/wp-con......
  • 【Linux】wordpress后台设置
    文章目录一.个人资料1.点击右上角的个人名称:选中编辑我的个人资料2.按下图进行修改二.设置--常规选项三.文章分类一.个人资料1.点击右上角的个人名称:选中编辑我的个人资料2.按下图进行修改二.设置–常规选项三.文章分类登陆后台-文章-分类目录Linux基础,web应用,中间件,数......
  • 树形dp
    https://atcoder.jp/contests/abc259/tasks/abc259_f树形dp(最简单的那种类型,但是题目的方法还是很巧妙的)易知:负权边可以忽略思路定义定义f[i][0]表示以i为根的子树尽量用到d[i]-1条边的最大可能(留一条边给父节点联通用)f[i][1]表示以i为根的子树尽量用到d[i]条边的最大可......
  • 设置Sweet Home 3D 支持高DPI显示
    SweetHome3D没有默认检测当前屏幕的DPI大小,而进行相应的缩放,需要添加下面的环境变量手动设置一下。JAVA_OPTS=-Dcom.eteks.sweethome3d.resolutionScale=1.5......