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. Debug与Release版本的区别详解

    原文链接 Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug和Release的行为完全颠 ...

  2. C# 常用函数和方法集汇总

    1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.D ...

  3. detection in video and image

    video中的detection,背景更加复杂,目标更加不聚焦,同时由于图片分辨率低于图像,因此更加难做. image中的Detection,背景相对简单些,目标更加聚焦,同时图片分辨率高,因此更加容 ...

  4. 快速下载jar包

    1, http://www.mvnrepository.com 2,可以从spring官网上下载,如果是mvn的话可以通过上面的网址下载

  5. Spring中注解大全和应用

    @Controller@RestController:@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable@C ...

  6. JS大小转化B KB MB GB的转化方法

    function conver(limit){ var size = ""; ){ //如果小于0.1KB转化成B size = limit.toFixed() + "B ...

  7. ZendFramework-2.4 源代码 - 关于MVC - Model层类图

  8. JZOJ 5777. 【NOIP2008模拟】小x玩游戏

    5777. [NOIP2008模拟]小x玩游戏 (File IO): input:game.in output:game.out Time Limits: 1000 ms  Memory Limits ...

  9. Java Web 基础(一) 基于TCP的Socket网络编程

    一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...

  10. Jconsole连接Tomcat JVM

    修改java虚拟机启动参数 在%TOMCAT_HOME%\bin\catalina.sh文件的最顶端 JAVA_OPTS=”-Dcom.sun.management.jmxremote.port=10 ...