• dp(电梯与楼梯)


    http://codeforces.com/problemset/problem/1249/E

    E. By Elevator or Stairs?
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are planning to buy an apartment in a n

    -floor building. The floors are numbered from 1 to n

    from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.

    Let:

    • ai

    for all i from 1 to n1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i

    • -th as well) using the stairs;
    • bi
    for all i from 1 to n1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c
    • — time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).

    In one move, you can go from the floor you are staying at x

    to any floor y (xy

    ) in two different ways:

    • If you are using the stairs, just sum up the corresponding values of ai
    . Formally, it will take i=min(x,y)max(x,y)1ai
    • time units.
    • If you are using the elevator, just sum up c
    and the corresponding values of bi. Formally, it will take c+i=min(x,y)max(x,y)1bi
    • time units.

    You can perform as many moves as you want (possibly zero).

    So your task is for each i

    to determine the minimum total time it takes to reach the i-th floor from the 1

    -st (bottom) floor.

    Input

    The first line of the input contains two integers n

    and c (2n2105,1c1000

    ) — the number of floors in the building and the time overhead for the elevator rides.

    The second line of the input contains n1

    integers a1,a2,,an1 (1ai1000), where ai is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i

    -th as well) using the stairs.

    The third line of the input contains n1

    integers b1,b2,,bn1 (1bi1000), where bi is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i

    -th as well) using the elevator.

    Output

    Print n

    integers t1,t2,,tn, where ti is the minimum total time to reach the i

    -th floor from the first floor if you can perform as many moves as you want.

    Examples
    Input
    Copy
    10 2
    7 6 18 6 16 18 1 17 17
    6 9 3 10 9 1 10 1 5
    
    Output
    Copy
    0 7 13 18 24 35 36 37 40 45 
    
    Input
    Copy
    10 1
    3 2 3 1 3 3 1 4 1
    1 2 3 4 4 1 2 1 3
    
    Output
    Copy
    0 2 4 7 8 11 13 14 16 17

    题意:有n楼,给你1到2,2到3....n-1到n楼的爬楼梯时间和坐电梯时间。从楼梯去坐电梯需要等电梯开门的时间c。问每一楼到一楼的最短时间。

    解法:考虑四个转移状态:从楼梯到楼梯,从楼梯到电梯,从电梯到楼梯,从电梯到电梯。

    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 998244353
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    ll a[200009] , b[200009];
    ll dp[200009][3];
    
    int main()
    {
        ll n , c ;
        while(~scanf("%lld%lld" , &n , &c))
        {
            for(int i = 2 ; i <= n ; i++)
            {
                scanf("%lld" , &a[i]);
            }
            for(int i = 2 ; i <= n ; i++)
            {
                scanf("%lld" , &b[i]);
            }
            memset(dp , INF , sizeof(dp));
            dp[1][1] = c ;
            dp[1][0] = 0 ;
            for(int i = 2 ; i <= n ; i++)
            {
                dp[i][0] = min(dp[i][0] , dp[i-1][1]+a[i]);//电梯到楼梯
                dp[i][0] = min(dp[i][0] , dp[i-1][0]+a[i]);//楼梯到楼梯
                dp[i][1] = min(dp[i][1] , dp[i-1][1]+b[i]);//电梯到电梯
                dp[i][1] = min(dp[i][1] , dp[i-1][0]+b[i]+c);//楼梯到电梯
            }
    
            for(int i = 1 ; i < n ; i++)
                cout << min(dp[i][0] , dp[i][1]) <<  " " ;
            cout << min(dp[n][0] , dp[n][1]) << endl ;
        }
    
    
        return 0 ;
    }
  • 相关阅读:
    [备忘]使用Outlook 2010,拖拽大于20M附件发生“附件大小超过了允许的范围”提示的解决方法
    关于Linq to SQL 的“异常详细信息: System.InvalidCastException: 指定的转换无效。”
    [备忘]ie6中href设为javascript:void(0)页面无法提交的解决方法
    [备忘]谷歌员工证实PR值不再更新 呼吁用户关注内容
    今天早上
    C++primer6.20
    Fuzzy KNN
    数值转换
    ArcMap分割图斑
    VS2008 集成openCV过程
  • 原文地址:https://www.cnblogs.com/nonames/p/11748491.html
Copyright © 2020-2023  润新知