zoukankan      html  css  js  c++  java
  • CodeForces 699C

    题目链接:http://codeforces.com/problemset/problem/699/C


    C. Vacations

    time limit per test1 second
    memory limit per test256 megabytes

    Problem Description

    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:

    on this day the gym is closed and the contest is not carried out;
    on this day the gym is closed and the contest is carried out;
    on this day the gym is open and the contest is not carried out;
    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:

    1. ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out;
    2. ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out;
    3. ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out;
    4. 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:
    6. to do sport on any two consecutive days,
    7. to write the contest on any two consecutive days.
    这里写图片描述

    Note

    In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.

    In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.

    In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.


    这是一个简单的dp,然而完全没想到,模拟也可以做。
    dp[i][j] (i:第几天,j:昨天干了啥) = 从第1天到第n天最少休息了多少天。


    简单模拟:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 110;
    int num[maxn];
    bool z[maxn];//用于标记前一天是否是休假
    int main()
    {
        int n;
        while(scanf("%d",&n)!= EOF)
        {
            memset(z,0,sizeof(z));
            for(int i=1;i<=n;i++)
                scanf("%d",&num[i]);
            int ans = 0;
            bool flag = false;
            bool flag2 = false;
            for(int i=1;i<=n;i++)
            {
                int now = ans;
                if(num[i] == 0)
                    ans++;
                else if(num[i] == 1)
                {
                    if(num[i-1] == 3 && num[i-2] == 2 && i-2 > 0 && !z[i-1] && !z[i-2])
                        ans++;
                    else if(num[i-1] == 1 && !z[i-1] && i-1 >0)
                        ans++;
                }
                else if(num[i] == 2)
                {
                    if(num[i-1] == 3 && num[i-2] == 1 && i-2 > 0 && !z[i-1] && !z[i-2])
                        ans++;
                    else if(num[i-1] == 2 && !z[i-1] && i-1 > 0)
                        ans++;
                }
                if(num[i] == 3 && num[i-1] == 1 && !z[i-1])
                    num[i] = 2;
                else if(num[i] == 3 && num[i-1] == 2 && !z[i-1])
                    num[i] = 1;
                if(ans > now)
                   z[i] = true;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }

    用dp做:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 110;
    int dp[maxn][10];
    int num[maxn];
    int main()
    {
        int n;
        while(scanf("%d",&n) != EOF)
        {
            memset(dp,0x7f,sizeof(dp));
            for(int i=0;i<=4;i++)
                dp[0][i] = 0;
            for(int i=1;i<=n;i++)
            {
                int temp;
                scanf("%d",&temp);
                dp[i][0] = min(dp[i-1][1],min(dp[i-1][2],dp[i-1][0])) + 1;//前一天是休假,今天的dp++;
                if(temp == 1)
                    dp[i][2] = min(dp[i-1][0],dp[i-1][1]);//今天做1,昨天做2或者休假
                if(temp == 2)
                    dp[i][1] = min(dp[i-1][0],dp[i-1][2]);//今天做2,昨天做1或者休假
                if(temp == 3)
                {
                    dp[i][2] = min(dp[i-1][0],dp[i-1][1]);
                    dp[i][1] = min(dp[i-1][0],dp[i-1][2]);
                }
            }
            printf("%d
    ",min(dp[n][1],min(dp[n][2],dp[n][0])));
        }
        return 0;
    }
  • 相关阅读:
    android init进程分析 ueventd
    SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required
    No such file or directory 8356:error:02001003:system library:fopen:No such process:cryptoioss_file.c:7 4:fopen
    mybatis-generator的maven插件使用异常(mybatis-generator-maven-plugin):generate failed: Exception getting JDBC Driver
    maven中import scope依赖方式解决单继承问题的理解
    复式记账中"借"与"贷"的理解
    Spring初始化Bean或销毁Bean前执行操作的方式
    NoClassDefFoundError: com/ibatis/sqlmap/engine/transaction/external/ExternalTransactionConfig处理
    Spring Bean依赖但注入(autowired或resource)时NullPointerException(xml和annotation混用的场景下)
    解决IntelliJ IDEA 创建Maven项目速度慢问题
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107249.html
Copyright © 2011-2022 走看看