Codeforces Round #363 (Div. 2) C DP
C. Vacations
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputVasya 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:
- 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.
题意:
有n天,每天有不同状态,一共四个状态(0:不能健身,不能打比赛。1:不能健身,能打比赛。2:能健身,不能打比赛。3:能健身,能打比赛。),Vasya能1天做2个不同的工作(状态3),但不能连续2天做1个相同的工作,问他能休息(不健身和不打比赛)的最小天数。
思路:
一眼DP系列……然而比赛时智障没想清楚状态是怎么转移的,赛后讨论才知道。分三个状态:0(休息),1(打比赛),2(健身),第i天的0=i-1天中的三个状态的min+1,1=i-1天中的1和3下的0和2状态的min,2=i-1天中的2和3下的0和1状态的min……最后输出第n天三个状态的最小值就可以了。
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn=1010; int n; int a[maxn]; int dp[maxn][3];//三个状态:0(休息),1(参加比赛),2(参加健身) int main(){ scanf("%d",&n); memset(dp,1,sizeof(dp)); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } dp[0][0]=0; for(int i=1;i<=n;i++){ dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1; if(a[i]==1||a[i]==3){ dp[i][1]=min(dp[i-1][0],dp[i-1][2]); } if(a[i]==2||a[i]==3){ dp[i][2]=min(dp[i-1][0],dp[i-1][1]); } } printf("%d\n",min(min(dp[n][0],dp[n][1]),dp[n][2])); return 0; }