• Leetcode983 Minimum Cost For Tickets


    Description

    Problem

    In a country popular for train travel, you have planned some train travelling one year in advance.  The days of the year that you will travel is given as an array days.  Each day is an integer from 1 to 365.

    Train tickets are sold in 3 different ways:

    a 1-day pass is sold for costs[0] dollars;
    a 7-day pass is sold for costs[1] dollars;
    a 30-day pass is sold for costs[2] dollars.
    The passes allow that many days of consecutive travel.  For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.

    Return the minimum number of dollars you need to travel every day in the given list of days.

    Sample

    Sample1

    Input: days = [1,4,6,7,8,20], costs = [2,7,15]

    Output: 11

    Explanation:

    For example, here is one way to buy passes that lets you travel your travel plan:

    On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.

    On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.

    On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.

    In total you spent $11 and covered all the days of your travel.

    Sample2

    Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]

    Output: 17

    Explanation:

    For example, here is one way to buy passes that lets you travel your travel plan:

    On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.

    On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.

    In total you spent $17 and covered all the days of your travel.

    Analysis

    不方便看出线性关系时一定要考虑全部的不变量,即使往外扩展有多种可能,不变的还是天数之间的差值,以此列出状态转移方程

    Code

    以旅游日入手

    考虑upper_bound

    dp(j)=min(dp(j-1)+costs[0],dp(j-7)+costs[1],dp(j-30)+costs[2])

    class Solution {
    public:
        int mincostTickets(vector<int>& days, vector<int>& costs) {
            int n=days.size();
            auto start=days.begin();
            for(int i=0;i<n;++i){
                int b=(int)(upper_bound(start,start+i,days[i]-7)-start);
                int c=(int)(upper_bound(start,start+i,days[i]-30)-start);
                dp[i+1]=min(dp[i]+costs[0],min(dp[b]+costs[1],dp[c]+costs[2]));
            }
            return dp[n];
        }
    private:
            int dp[366];
    };
    

    以365天入手

    dp(i)=dp(i-1)

    dp(i)=min{cost(j)+dp(i-j)},j∈{1,7,30}

    class Solution {
    public:
        int mincostTickets(vector<int>& days, vector<int>& costs) {
            int n=days.size();
            int end=days[n-1];
            int idx=0;
            int x,y;
            for(int i=days[0];i<=end;++i){
                if(i==days[idx]){
                    x=i-7>=0?dp[i-7]:0;
                    y=i-30>=0?dp[i-30]:0;
                    dp[i]=min(dp[i-1]+costs[0],min(x+costs[1],y+costs[2]));
                    idx++;
                }
                else dp[i]=dp[i-1];
            }
            return dp[end];
        }
    private:
            int dp[366];
    };
    
  • 相关阅读:
    F. Maximum White Subtree 树形dp*换根
    D
    E
    两圆相交板子
    lucass定理
    高精度求组合数
    康托展开与康托逆展开
    FFT变换
    Codeforces Round #625 Div. 1 Problem C
    E.Multiply Pollard_rho质因数分解
  • 原文地址:https://www.cnblogs.com/chanceYu/p/12839666.html
Copyright © 2020-2023  润新知