题目链接: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\n",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\n",min(dp[n][1],min(dp[n][2],dp[n][0])));
}
return 0;
}

CodeForces 699C - Vacations的更多相关文章

  1. 【动态规划】Codeforces 698A & 699C Vacations

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

  2. CodeForces 698A Vacations

    题目链接 : http://codeforces.com/problemset/problem/698/A 题目大意: 阿Q有n天假期,假期中有三种安排 休息.健身.比赛.每天有三种选择条件: 0 健 ...

  3. Codeforces 698A - Vacations - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/698/A 题意: 有 $n$ 天假期,每天有四种情况:0.体育馆不开门,没有比赛:1.体育馆不开门,有比赛 ...

  4. CodeForces 698A - Vacations (Codeforces Round #363 (Div. 2))

    要么去体育馆,要么去比赛,要么闲在家里 给出每一天体育馆和比赛的有无情况,要求连续两天不能去同一个地方 问最少闲几天 DP方程很容易看出 dp(第i天能去的地方) = min(dp(第i-1天的三种情 ...

  5. CodeForces #363 div2 Vacations DP

    题目链接:C. Vacations 题意:现在有n天的假期,对于第i天有四种情况: 0  gym没开,contest没开 1  gym没开,contest开了 2 gym开了,contest没开 3 ...

  6. Codeforces Round #363 (Div. 2)->C. Vacations

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

  7. Codeforces Round #363 (Div. 2) C. Vacations(DP)

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

  8. Codeforces 698A:Vacations(DP)

    题目链接:http://codeforces.com/problemset/problem/698/A 题意 Vasya在n天中,有三件事情可以做,健身.比赛或者休息,但是不能连续两天都是比赛或都是但 ...

  9. Codeforces Round #363 (Div. 2) C. Vacations —— DP

    题目链接:http://codeforces.com/contest/699/problem/C 题解: 1.可知每天有三个状态:1.contest ,2.gym,3.rest. 2.所以设dp[i] ...

随机推荐

  1. Qt 进程和线程之三:线程同步、可重入与线程安全

    一.同步线程方法 使用线程的目的是允许代码并行运行,但是有时线程必须停止并等待其他线程.例如,如果两个线程试图同时写入相同的变量,结果是不确定的,所以需要同步线程.同步线程是一种保护共享资源等数据的常 ...

  2. Gym - 101810D ACM International Collegiate Programming Contest (2018)

    bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...

  3. SSE练习:单精度浮点数组求和

    SSE(Streaming SIMD Extensions)指令是一种SIMD 指令, Intrinsics函数则是对SSE指令的函数封装,利用C语言形式来调用SIMD指令集,大大提高了易读性和可维护 ...

  4. python学习之列表元组,字典

    list:元素性质可以不一致,元素还可以是list,可类似数组方法进行索引(也可以用负数索引,-1表示最后一个),可用.append('')进行动态增加,可用pop()删除最后一个或者pop(i)删除 ...

  5. JavaScript alert()函数

    avaScript alert()函数 alert--弹出消息对话框(对话框中有一个OK按钮) alert,中文"提醒"的意思 alert函数语法 alert(str); aler ...

  6. DDX和DDV——控件与变量之间值的传递

    DoDataExchange由框架调用,作用是交互并且验证对话框数据,主要由(DDX) 和 (DDV)宏实现. 永远不要直接调用这个函数,而是通过UpdateData(TRUE/FALSE)实现控件与 ...

  7. JAVA设计模式之策略模式 - Strategy

    在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...

  8. ubuntu中wine下安装QQ

    原文:http://jingyan.baidu.com/article/359911f55da27057fe0306d8.html 可以把win改成最新版

  9. Django中对单表的增删改查

    之前的简单预习,重点在后面 方式一: # create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象   book_obj=Book.objects.creat ...

  10. ES-Mac OS环境搭建-ik中文分词器

    下载 从github下载ik中文分词器,点击地址,需要注意的是,ik分词器和elasticsearch版本必须一致. 安装 下载到本地并解压到elasticsearch中的plugins目录内即可. ...