【刷题】【dp】help jimmy!

看着复杂,其实理一理之后还好

重要的是

伪代码技巧

 

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
int n,mxh;
const int N=1e3+3,inf=1<<30;
struct node
{
    int l,r,h;
    bool operator < (const node & o) const
    { return h>o.h; } //我先处理上面的,然后递归深入
}d[N];
int f[N][2];

int find(int st,int x ,int low)
{
    for(int i=st;i<=n;i++)
        if(d[i].h <low)//超过mxh,就算找到了也没用,不如直接清掉
            return n+1;
        else if(d[i].l <=x && d[i].r >=x )//恰好落在平台边缘,视作落在平台上
            return i;
    return n+1;
}
bool vis[N];
void work(int nw)
{    //我已经memset成inf了,所以直接找能走的更新就行
    if(vis[nw]) return ;
    vis[nw]=true;
    //往左走
    int x=d[nw].l ;
    int nx=find(nw+1 ,x ,d[nw].h -mxh );
    if(nx<=n)
    {
        work(nx);
        f[nw][0]=min( f[nx][0]+x-d[nx].l ,f[nx][1]+d[nx].r -x) + d[nw].h -d[nx].h ;
    }
    else
        if(d[nw].h <=mxh) f[nw][0]=d[nw].h ;
    //往右走
    x=d[nw].r ;
    nx=find(nw+1 ,x ,d[nw].h -mxh );
    if(nx<=n)
    {
        work(nx);
        f[nw][1]=min( f[nx][0]+x-d[nx].l ,f[nx][1]+d[nx].r -x) + d[nw].h -d[nx].h ;
    }
    else
        if(d[nw].h <=mxh) f[nw][1]=d[nw].h ;
}

int main()
{
    while(~scanf("%d%d%d%d",&n,&d[0].l ,&d[0].h ,&mxh))
    {
        memset(f,0x7f,sizeof(f)); 

        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&d[i].l ,&d[i].r ,&d[i].h );
        d[0].r =d[0].l ;
        sort(d+1,d+n+1);

        work(0);
        printf("%d\n",min(f[0][0],f[0][1]));
    }

    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
int n,mxh;
const int N=1e3+3,inf=1<<30;
struct node
{
    int l,r,h;
    bool operator < (const node & o) const
    { return h>o.h; } //我先处理上面的,然后递归深入
}d[N];
int f[N][2];

int find(int st,int x ,int low)
{
    for(int i=st;i<=n;i++)
        if(d[i].h <low)//超过mxh,就算找到了也没用,不如直接清掉
            return n+1;
        else if(d[i].l <=x && d[i].r >=x )//恰好落在平台边缘,视作落在平台上
            return i;
    return n+1;
}
bool vis[N];
void work(int nw)
{    //我已经memset成inf了,所以直接找能走的更新就行
    if(vis[nw]) return ;
    vis[nw]=true;
    //往左走
    int x=d[nw].l ;
    int nx=find(nw+1 ,x ,d[nw].h -mxh );
    if(nx<=n)
    {
        work(nx);
        f[nw][0]=min( f[nx][0]+x-d[nx].l ,f[nx][1]+d[nx].r -x) + d[nw].h -d[nx].h ;
    }
    else
        if(d[nw].h <=mxh) f[nw][0]=d[nw].h ;
    //往右走
    x=d[nw].r ;
    nx=find(nw+1 ,x ,d[nw].h -mxh );
    if(nx<=n)
    {
        work(nx);
        f[nw][1]=min( f[nx][0]+x-d[nx].l ,f[nx][1]+d[nx].r -x) + d[nw].h -d[nx].h ;
    }
    else
        if(d[nw].h <=mxh) f[nw][1]=d[nw].h ;
}

int main()
{
    while(~scanf("%d%d%d%d",&n,&d[0].l ,&d[0].h ,&mxh))
    {
        memset(f,0x7f,sizeof(f)); 

        for(int i=1;i<=n;i++)
            scanf("%d%d%d",&d[i].l ,&d[i].r ,&d[i].h );
        d[0].r =d[0].l ;
        sort(d+1,d+n+1);

        work(0);
        printf("%d\n",min(f[0][0],f[0][1]));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/xwww666666/p/11765385.html

时间: 2024-08-09 23:12:40

【刷题】【dp】help jimmy!的相关文章

洛谷 P1926 小书童——刷题大军

题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的人. 文言是火,点亮历史宫灯:历史是灯,照亮社会之路:社会是路,通向哲学大坑:哲学是坑,埋葬文科生.——小A 题目描述 小A“刷题”十分猖狂,明目张胆地“刷题”.他现在在小书童里发现了n样他喜欢的“题目”,每“题”都有他的需要时间,而老师布置了m项作业,每项作业都有它的需要时间及分值,老师规定k分以上算及格.小A只剩r个单位时间,他想在及格的基础上更多地“刷题”. 输入输出格式 输入格式: 第一

XDOJ 1020 ACMer去刷题吧(基础dp)

题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输入一个正整数t,(t<=20),表示有t组测试样例. 第二行输入正整数n,k,(1<=n,k<=1000) 第三行输入n个小数,分别为Pi(1<=i<=n,0<=Pi<=1),表示小X做对第i个题目的概率. 输出 输出小X至少做对k道题的概率,并换行(保留4位小数)

DP刷题记录

目录 dp刷题记录 codeforces 706C codeforces 940E BZOJ3997 POJ2279 GYM102082B GYM102082D codeforces132C L3-020 至多删三个字符 牛客 553C Chino with Queue POJ3260 The Fewest Coins Codeforces 372C dp刷题记录 codeforces 706C 题意:给出n个字符串,可以对每个字符串进行翻转操作, 每个操作对应一个消耗c[i],问经过操作后是否

半个暑假的刷题有感

这半个多月一来,主要是在刷DP.开始是一些简单的DP(可是我没有感觉有多简单=_=!!),然后是最大连续子序列,最大公共子序列,最大子矩阵等等,这些题目还好,有的题目甚至可以模板化. 还有一些没有解决的难题: 1024 Max Sum Plus Plus 最大m段不重叠子段和](可不连续)1244 Max Sum Plus Plus Plus [最大m段不重叠子段和](连续) 1074 Doing Homework [压缩dp](这个题整个程序都在用位运算,让我这个小白情何以堪啊) 还有记忆化搜

BZOJ 刷题记录 PART 4

[BZOJ1143]CTSC的题目...先用floyed传递闭包,然后直接上匈牙利算法. [BZOJ1452]从未写过的二维树状数组.好像很简单.. struct two_bit { int f[305][305]; inline void add(int x,int z,int A) { for (;x<=n;x+=L(x)) for (int y=z;y<=m;y+=L(y)) f[x][y]+=A; } inline int ask(int x,int z) { int ans=0; f

刷题计划

我很后悔这一年来被我浪费过的每分每秒,我已经不想再浪费时间了. 平时不好好刷题还想着打比赛? 今年的目标就是刷完紫书第七章的搜索,第八章的贪心,第九章的dp,然后每次的cf补题尽量补到div2的DE题,如果时间有剩余,就学AC自动机等一些数据结构吧. 第一阶段:紫书ch7-ch9  时间11月末-12月31号 第二阶段  训练指南选择性的补充知识点,同时打一些5个小时比赛+补题  时间1月-2月 第三阶段  区域赛真题组队训练

7、8月刷题总结

准备开学了囧,7.8月刷题记录,以后好来复习,并且还要好好总结! 数据结构: splay: [BZOJ]1503: [NOI2004]郁闷的出纳员(Splay) [BZOJ]1269: [AHOI2006]文本编辑器editor(Splay) [BZOJ]1507: [NOI2003]Editor(Splay) treap: [BZOJ]1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心) [BZOJ]3224: Tyvj

[转]POJ的刷题指南(加了超链接的)

网上看到的转过来一下,顺便把题目都加了个超链接,方便刷起~ POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法:       (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.       (4)递推.       (5)构造法.(po

[2015.6.28] OI刷题记录

FZSZOJ刷题记录: 1051 砝码称重: DP 多重背包 1058 liqeuer: 序列DP 1061 挖地雷:DP,注意需要倒过来做DP,同时记录路径. 1059 Number:DP 1054 数塔问题:同数字三角形,普通DP 1390 等式问题:爆搜,枚举每个+号或-号的位置 1006 中位数:维护大根堆+小根堆,每次插入调整 1005 Cube Stacking:并查集维护当前在第几个和当前集合的高度,并查集变种. 1073 DNA分子的最佳比对:序列DP 1110 奖学金:傻逼题,

【leetcode刷题笔记】Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,