(贪心) Vacations 求休息的最少天数
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:
- 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
用k记录每天的状态,b[i]记录从第一天到第i天休息的天数,每一天的状态只与前一天有关,尽量保证能工作就不休息。
// 0 健身房关门 没比赛
// 1 健身房关门 有比赛
// 2 健身房开门 没比赛
// 3 健身房开门 有比赛
#include <cstdio>
int n,a[],k,i,b[];
int main()
{ while(scanf("%d",&n)!=EOF)
{
b[]=;
k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i = ; i <= n ; i++)
{
if(a[i] == )
{
b[i]=b[i-]+;
k=;
}
else
{
if(a[i] == )
{
b[i]=b[i-];
k=-k;
}
else
{
if(a[i] == k)
{
b[i]=b[i-]+;
k=;
}
else
{
b[i]=b[i-];
k=a[i];
}
}
}
}
printf("%d\n",b[n]);
}
}
(贪心) Vacations 求休息的最少天数的更多相关文章
- 算法笔试题整理——升级蓄水池 && 字符串数字表达式计算值 && 求旅游完所有景点需要的最少天数 && 宝箱怪
1. 小米笔试题——升级蓄水池 题目描述: 在米兔生活的二维世界中,建造蓄水池非常简单. 一个蓄水池可以用n个坐标轴上的非负整数表示,代表区间为[0-n]范围内宽度为1的墙壁的高度. 如下图1,黑色部 ...
- 【js】Leetcode每日一题-制作m束花所需的最少天数
[js]Leetcode每日一题-制作m束花所需的最少天数 [题目描述] 给你一个整数数组 bloomDay,以及两个整数 m 和 k . 现需要制作 m 束花.制作花束时,需要使用花园中 相邻的 k ...
- LeetCode 1482. 制作 m 束花所需的最少天数
LeetCode 1482. 制作 m 束花所需的最少天数 题目 给你一个整数数组 bloomDay,以及两个整数 m 和 k . 现需要制作 m 束花.制作花束时,需要使用花园中 相邻的 k 朵花 ...
- 1482. 制作 m 束花所需的最少天数
2021-05-09 LeetCode每日一题 链接:https://leetcode-cn.com/problems/minimum-number-of-days-to-make-m-bouquet ...
- Vijos P1023Victoria的舞会3【贪心+DFS求强联通分量】
链接:Click Me! P1023Victoria的舞会3 Accepted 标签:Victoria的舞会[显示标签] 描写叙述 Victoria是一位颇有成就的艺术家,他因油画作品<我爱北京 ...
- UVALive 6911 Double Swords (Set,贪心,求区间交集)
补:华中VJ这个题目很多标程都不能AC了,包括我下面原本AC了的代码,再交就WA掉了,感觉是样例有问题呢-- 首先左边的是必须要选的,然后右边的需要注意,有些区间是可以舍掉的.1.区间里有两个不同的A ...
- 通过日期在js中求出判断间隔天数,周期等实现分享
在我们在项目的时候,可能出现这样的一种情况,有一个开始时间和一个结束时间,而这两个时间用$('#StartTime').val(); 取出来的时候又是datetime 类型,我们需要求这个时间中的间隔 ...
- POJ 4020 NEERC John's inversion 贪心+归并求逆序对
题意:给你n张卡,每张卡上有蓝色和红色的两种数字,求一种排列使得对应颜色数字之间形成的逆序对总数最小 题解:贪心,先按蓝色排序,数字相同再按红色排,那么蓝色数字的逆序总数为0,考虑交换红色的数字消除逆 ...
- HDU - 2037 今年暑假不AC 贪心(求序列中不重叠子序列的最大值问题)
HDU2037 今年暑假不AC 贪心算法 大意: 每次测试数据输入一个n,然后输入n对的电视节目播放时间:开始时间及结束时间, 求这个人能看的最多的完整的节目数. 解题思路: 对于这道解题,是对每个 ...
随机推荐
- (DP ST表 线段树)51NOD 1174 区间中最大的数
给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数为7. ...
- 浅谈扩展欧几里得[exgcd] By cellur925
关于扩展欧几里得从寒假时就很迷,抄题解过了同余方程,但是原理并不理解. 今天终于把坑填上了qwq. 由于本人太菜,不会用markdown,所以这篇总结是手写的(什么).(字丑不要嫌弃嘛) ****** ...
- matlab学习路线
知乎:matlab学习路线 MATLAB数学教学视频
- 洛谷 P1288 取数游戏II
奇奇怪怪的游戏,不多写了 #include<cstdio> ]; int main() { int i; scanf("%d",&n); ;i<=n;i+ ...
- static属性
static 属于全局,也就是类的属性 和方法,换句话说 一个类,不管有多少个实例,却只有一个全局变量 用static修饰的属性和方法称为静态属性和方法 需要注意的是 静态属性和方法属于类方法,加载类 ...
- [转]Entity Framework and SQL Azure
本文转自:https://msdn.microsoft.com/zh-cn/library/gg190738 Julie Lerman http://thedatafarm.com April 201 ...
- JavaScript实现JQuery的功能
- Android 使用Bitmap将自身保存为文件,BitmapFactory从File中解析图片并防止OOM
1.使用Bitmap将自身保存为文件 public boolean saveBitmapAsFile(String name, Bitmap bitmap) { File saveFile = new ...
- git---安装及配置
git官网:https://git-scm.com 安装: 官网下载->一路Next->安装完成 配置git: 1.win+r进入windows命令行 2.注册: git config - ...
- OpenGl之旅-—如何使用code blocks创建一个opengl项目
开始学习opengl啦,练习用的编辑器是code blocks. 首先当然是要清楚如何使用code blocks创建一个opengl项目了. 首先必须先引用opengl的库glut,网上百度下载一个完 ...