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;
    }
  • 相关阅读:
    【区间覆盖问题】uva 10020
    【Fibonacci】BestCoder #28B Fibonacci
    Struts2 用过滤器代替了 servlet ,???? 且不需要tomcat就可以直接做功能测试
    血的教训 password写成passward,教训应该从首页赋值 参数名
    为什么这个地方用重定向会报错.只能用 服务器跳转?? 为什么我加了过滤器,还是能直接登陆 servlet
    //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面
    request.setAttribute("username", username);//一定要保存,OGNL才能获取${username}
    form表单的提交地址一定要是完整的绝对地址
    登录页面jsp跳转到另一个jsp 与jsp-Servlet-jsp
    在Windows下MyEclipse运行JAVA程序连接HBASE读取数据出错
  • 原文地址:https://www.cnblogs.com/immortal-worm/p/5703114.html
Copyright © 2011-2022 走看看