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:

  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.

Sample Input

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

题意:给你n天的情况
0 代表休息
1 代表只能参加contest或休息
2 代表只能参加gym或休息
3 代表能参加contest或gym或休息
要求 不能连续参加contest 不能连续参加gym
问 如何安排使得休息日最少 输出休息日的数量 题解: dp处理 dp[i][j] 代表 第i天进行j活动的情况下的前i天的最多非休息日的数量
三个状态的转移方程
dp[i][1]=max(dp[i-1][2],dp[i-1][0])+1;
dp[i][2]=max(dp[i-1][1],dp[i-1][0])+1;
dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]); 结果为n-max(max(dp[n][0],dp[n][1]),dp[n][2])
 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n;
int a[];
int dp[][];
int main()
{
scanf("%d",&n);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
if(a[]==)
dp[][]=;
if(a[]==)
dp[][]=;
if(a[]==)
dp[][]=;
if(a[]==)
{
dp[][]=;
dp[][]=;
dp[][]=;
}
for(int i=;i<=n;i++)
{
if(a[i]==)
{
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
if(a[i]==)
{
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
if(a[i]==)
{
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
if(a[i]==)
{
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(dp[i-][],dp[i-][])+;
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
}
}
int ans=;
for(int i=;i<=;i++)
ans=max(ans,dp[n][i]);
cout<<n-ans<<endl;
return ;
}

贪心处理  我gou代码

重点在3状态的处理上  若当前状态为3 则判断前一天状态

若前一天为1状态 则 将当前状态改为2  非休息日++

若前一天为2状态 则 将当前状态改为1  非休息日++

若前一天为0状态 则 将当前状态改为0  非休息日++ 当前状态改为0意味着 无论后一个状态为什么

当前这一天都有相应的状态可以更改 使得当前这一天为非休息日  即当前这一天 对后一天没有影响。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
#define PI acos(-1.0)
const int inf = (<<) - ;
using namespace std;
inline int get_int()
{
int r=;
char c;
while((c=getchar())!=' '&&c!='\n')
r=r*+c-'';
return r;
}
inline void out(int x)
{
if(x>)
{
out(x/);
}
putchar(x % + '');
putchar('\n');
}
/****************************************/
int a[];
int main()
{
int n,m;
cin>>n;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
m=;
if(a[]>){
m++;
}
if(a[]==)
a[]=;
for(int i=;i<n;i++){
if(a[i]==){
if(a[i-]!=)
m++;
else a[i]=;
}
if(a[i]==){
if(a[i-]!=)
m++;
else a[i]=;
}
if(a[i]==){
if(a[i-]==){
a[i]=;
m++;
}
if(a[i-]==){
a[i]=;
m++;
}
if(a[i-]==){
a[i]=;
m++;
}
}
}
printf("%d\n",n-m);
return ;
}

另外附一种奇怪搞法 逻辑运算处理

http://blog.csdn.net/nare123/article/details/51966794

Codeforces Round #363 (Div. 2) C dp或贪心 两种方法的更多相关文章

  1. Codeforces Round 363 Div. 1 (A,B,C,D,E,F)

    Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...

  2. 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 ...

  3. Codeforces Round #363 (Div. 2)

    A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...

  4. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  5. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  6. Codeforces Round #363 Div.2[111110]

    好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...

  7. Codeforces Round #652 (Div. 2) E. DeadLee(贪心)

    题目链接:https://codeforces.com/contest/1369/problem/E 题意 Lee 有 $n$ 种不同种类的食物和 $m$ 个朋友,每种食物有 $w_i$ 个,每个朋友 ...

  8. 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] ...

  9. 严格递增类的dp Codeforces Round #371 (Div. 1) C dp

    http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...

随机推荐

  1. 题解 P4613 【[COCI2017-2018#5] Olivander】

    话说这道题,作为一个哈迷,是不能错过的 我很惊讶本蒟蒻竟然看得懂题面 好了,闲话少说,这道题,虽说是入门难度,但凭着良心说,它还是一道普及 - 的吧 看到标签,“高性能”,大脑的第一反应是快读. 是不 ...

  2. nodejs 爬虫

    参考了各位大大的,然后自己写了个爬虫 用到的modules:utils.js     ---    moment module_url.js var http = require("http ...

  3. 移动端rem匹配

    Rem是相对于根元素font-size大小的单位 记inphone5屏幕宽度是 320px font-size16px 1rem=16px <html>   <head>    ...

  4. servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码

    package com.swift; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOE ...

  5. dom节点获取文本的方式

    1. innerHTML innerHTML可以作为获取文本的方法也可以作为修改文本内容的方法 element.innerHTML 会直接返回element节点下所有的HTML化的文本内容 <b ...

  6. 【C++学习笔记】 链式前向星

    链式前向星是一种常见的储存图的方式(是前向星存图法的优化版本),支持增边和查询,但不支持删边(如果想要删除指定的边建议用邻接矩阵). 储存方式 首先定义数组 head[ i ] 来储存从节点 i 出发 ...

  7. 回数是指从左向右读和从右向左读都是一样的数,例如 12321 , 909 。请利用 filter() 滤掉非回数

    不管在什么地方,什么时候,学习是快速提升自己的能力的一种体现!!!!!!!!!!! 最近一段时间学习了廖雪峰老师学的Python学习资料,给自己的帮助很大,同时也学到的了很多,今天做了一道练习题,对于 ...

  8. 记录一些 FileZillaClient 的基本连接操作

    本地主机:Window 10 FileZilla版本:3.39.0 64位 远程主机:CentOS 6.4 需安装FTP服务 小提示:查看CentOS版本命令 # cat /etc/issue Fil ...

  9. vue 网页文字中带#的话题颜色高亮

    网页中显示文字时,带#开始和结束的文字蓝色高亮,就像微博话题一样效果如下 html <span v-html="parseComments('#吃货节#有什么好吃的')"&g ...

  10. vue 项目中使用mock假数据实现前后端分离

    也是查了很多的资料,整理出来.实现了前后端的分离,用到的技术vue-cli,webpack,node,json-server.首先全局安装json-server cnpm i json-server ...