zoukankan      html  css  js  c++  java
  • 动态规划日常练习1

    问题:

    Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:

    1. on this day the gym is closed and the contest is not carried out;
    2. on this day the gym is closed and the contest is carried out;
    3. on this day the gym is open and the contest is not carried out;
    4. on this day the gym is open and the contest is carried out.

    On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).

    Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.

    Input

    The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations.

    The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where:

    • ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out;
    • ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out;
    • ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out;
    • ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
    Output

    Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:

    • to do sport on any two consecutive days,
    • to write the contest on any two consecutive days.
    Examples
    Input
    4
    1 3 2 0
    Output
    2
    Input
    7
    1 3 3 2 1 2 3
    Output
    0
    Input
    2
    2 2
    Output
    1

    思路:
    动态规划的核心是状态和状态转移方程,这题解决的关键点在于状态转移方程的寻找
    即我们根据每天的能做的事情(获取的数字)来确定当天所处的每个状态的“最多累计工作天数”
    难点不是具体思考这个状态转移究竟是什么,而是能不能想到这一步

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std; 
    
    int a[107];
    int dp[107][3];//第i天做j项工作的最大的工作天数 
    
    int main()
    {
        int n;
        while(cin>>n)
        {
            memset(dp,0,sizeof(dp));//1 3 2 0
            for(int i = 1;i <= n;i++)
                cin>>a[i];
            if(a[1] == 0) {
                dp[1][0] = dp[1][1] = dp[1][2] = 0;
            }
            if(a[1] == 1) {
                dp[1][1] = 1;
                dp[1][2] = dp[1][0] = 0;
            }
            if(a[1] == 2) {
                dp[1][2] = 1;
                dp[1][1] = dp[1][0] = 0;
            }
            if(a[1] == 3) {
                dp[1][0] = 0;
                dp[1][1] = dp[1][2] = 1;
            }
    //        cout<<dp[1][0]<<' '<<dp[1][1]<<' '<<dp[1][2]<<endl;
            for(int i = 2;i <= n;i++) {
                dp[i][0] = max(dp[i-1][0],max(dp[i-1][1],dp[i-1][2]));
                if(a[i]==1 || a[i]==3)
                    dp[i][1] = max(dp[i-1][1],max(dp[i-1][2],dp[i-1][0])+1);
                if(a[i]==2 || a[i]==3)
                    dp[i][2] = max(dp[i-1][2],max(dp[i-1][1],dp[i-1][0])+1);
    //            cout<<dp[i][0]<<endl;
    //            cout<<dp[i][1]<<endl;
    //            cout<<dp[i][2]<<endl;
            }
            int ans = max(dp[n][0],max(dp[n][1],dp[n][2]));
            cout<<n-ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    C++ 把输出结果写入文件/从文件中读取数据
    转载:C++之高精度算法
    借助bool判断使冒泡排序效率提高
    启程
    2017总结,2018的路
    mysql 分组排序
    2021年VS2019最新有效的调试ASP.NET Core源码
    神级Java程序员 开车教你基础开发,最简单 微型Java Web框架
    手把手教你 基础 整合最优雅SSM框架:SpringMVC + Spring
    学习Java绝对要懂的,Java编程中最常用的几种排序算法!
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/5703114.html
Copyright © 2011-2022 走看看