Codeforces 699C. Vacations

C. Vacations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

题目大意:爱学习的Vasya ,在接下来的n天中根据运动场和是否有比赛的状态来决定自己休息或者是做事。

      0:运动场没有开,没有比赛

      1:运动场打开,没有比赛

      2:运动场关闭,有比赛

      3: 运动场打开,有比赛

     不能连续两天都打比赛或者都运动否则就必须休息一天

     请你求出怎样才能让Vasya 休息的时间最少。

/*
动态规划
思路:如果我们要获得第n天做事的最小休息时间,就要从第n-1天的最小休息时间获得。
dp[i][j]:在第i天如果我选择做j这件事所能休息的最小天数
当我们在第j天选择工作或者比赛,就从j-1天时的工作或者比赛所休息的最小天数来更新状态 状态转移方程如代码
*/ #include<bits/stdc++.h> using namespace std; int day[200]; int dp[200][3]; const int maxn = 200; int main() { int n; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&day[i]); } for(int i=1; i<=n; i++) dp[i][0]=dp[i][1]=dp[i][2]=maxn; for(int i=1; i<=n; i++) { if(day[i]==0) { dp[i][0] = min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2])) + 1;  //当我这天选择休息时,从前一天休息 运动 比赛中选择最小的+1 } else if(day[i]==1) { dp[i][0] = min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2])) + 1;  //当我这天选择休息时,从前一天休息 比赛选最小的+1 dp[i][1] = min(dp[i-1][0],dp[i-1][2]);              //当我这天选择运动时,从前一天运动 休息选最小的 } else if(day[i]==2) { dp[i][0] = min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2])) + 1;  //当我这天选择休息时, 从前一天休息 运动 比赛中选择最小的+1 dp[i][2] = min(dp[i-1][0],dp[i-1][1]);              //当我这天选择比赛时, 从前一天休息 运动中选择最小的 } else     { dp[i][0] = min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2])) + 1;    //同理 dp[i][1] = min(dp[i-1][0],dp[i-1][2]); dp[i][2] = min(dp[i-1][0],dp[i-1][1]); } } int ans = maxn; for(int i=0; i<3; i++) ans = min(ans,dp[n][i]);  //找出第n天最小的天数 printf("%d",ans); return 0; }
原文地址:https://www.cnblogs.com/MyCodeLife-/p/7711482.html