(贪心) 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对的电视节目播放时间:开始时间及结束时间, 求这个人能看的最多的完整的节目数. 解题思路: 对于这道解题,是对每个 ...
随机推荐
- Oracle异机恢复
RMAN异机恢复注意事项:1.RMAN 异机恢复的时候,db_name必须相同. 如果说要想改成其他的实例名,可以在恢复成功后,用nid 命令修改. 实例名的信息会记录到控制文件里,所以如果在恢复的时 ...
- windows 下使用命令行操作ftp
open 192.168.10.6 (连接到FTP主机) User allan\ftp (用户连接验证,注意这里的用户用到的是FTP服务器端创建的用户名) 123 ...
- Java中的流(5)大数据流的分段读取
来自文件 或 网络的InputStream数据量可能很大,如果用流的大小申请byte[],可能内存不足报错. 解决方案:分段读取 InputStream的方法int available()返回本次可读 ...
- hibernate 中createQuery与createSQLQuery(转载)
息: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.miracle.dm.doc.catalog.mo ...
- WebForm 开发方式,简单使用
ASP开发方式 格式 <% %> C#代码可以写在里面 <%= %> 往外输出一个值,可以放一个变量,一个方法(这个方法是有返回值的直接打印到界面上去) <%@ %& ...
- .net主站和二级域名下实现session共享
public class CrossDomainCookie : IHttpModule { private string m_RootDomain = string.Empty; #region I ...
- oracle DBA笔试题
Unix/Linux题目: 1.如何查看主机CPU.内存.IP和磁盘空间? cat /proc/cpuinfo cat /proc/meminfo ifconfig –a fdisk –l 2.你 ...
- linux设置ssh连接时间
相信大家经常遇到SSH连接闲置一会就断开需要重新连接的痛苦,为了使SSH连接保持足够长的时间,我们可以使用如下两种设置 1.sshd服务配置: #vi /etc/ssh/sshd_config Cli ...
- 拼图游戏源码-swift版项目源码
作者fanyinan,源码PuzzleProject,公司的项目中需要一个拼图游戏,之前有手动拼图和随机打乱的功能,近期又由于个(xian)人(zhe)爱(dan)好(teng)自己加入了自动拼图功能 ...
- schtasks /create 计划任务 中文路径 名字都要加“” 子命令 /tn /tr 前面要空格 否则会出错
echo off echo. 清空连接 net use * /del /y echo. 连接 net use \\192.168.1.2\人力资源部\考勤\考勤数据-小莫提供 "密码&quo ...