CodeForces 699C - Vacations
题目链接: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:
- 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:
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的更多相关文章
- 【动态规划】Codeforces 698A & 699C Vacations
题目链接: http://codeforces.com/problemset/problem/698/A http://codeforces.com/problemset/problem/699/C ...
- CodeForces 698A Vacations
题目链接 : http://codeforces.com/problemset/problem/698/A 题目大意: 阿Q有n天假期,假期中有三种安排 休息.健身.比赛.每天有三种选择条件: 0 健 ...
- Codeforces 698A - Vacations - [简单DP]
题目链接:http://codeforces.com/problemset/problem/698/A 题意: 有 $n$ 天假期,每天有四种情况:0.体育馆不开门,没有比赛:1.体育馆不开门,有比赛 ...
- CodeForces 698A - Vacations (Codeforces Round #363 (Div. 2))
要么去体育馆,要么去比赛,要么闲在家里 给出每一天体育馆和比赛的有无情况,要求连续两天不能去同一个地方 问最少闲几天 DP方程很容易看出 dp(第i天能去的地方) = min(dp(第i-1天的三种情 ...
- CodeForces #363 div2 Vacations DP
题目链接:C. Vacations 题意:现在有n天的假期,对于第i天有四种情况: 0 gym没开,contest没开 1 gym没开,contest开了 2 gym开了,contest没开 3 ...
- 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 ...
- 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 ...
- Codeforces 698A:Vacations(DP)
题目链接:http://codeforces.com/problemset/problem/698/A 题意 Vasya在n天中,有三件事情可以做,健身.比赛或者休息,但是不能连续两天都是比赛或都是但 ...
- 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] ...
随机推荐
- Bluefish
Bluefish标榜其自身是“一款为熟练的Web设计员和程序员而设的编辑器,但它的UI却很直观,任何初学者都能够很快上手,并在不断地积累中发现和掌握它的其它功能. Bluefish标榜其自身是“一 ...
- vue从入门到开发--2-基本结构
1.App.vue 是根文件,所有的其他组件的执行均需要在此文件内导入并调用才能实现. import (导入其他组件) Test (其他组件的名字) from ‘./components/test’( ...
- ExpandableListView 安卓二级菜单
ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView).ExpandableListView允许有两个层次:一级列表中有二级列表.比如在 ...
- iOS 如何解决并发请求时,只接受最后一个请求返回的结果
大致意思是 虽然NSOperation 的cancel 并不能取消请求,但是可以对这个NSOperation进行标记. 当cancel 属性是YES时,表明 NSOperation虽然已经执行,并 ...
- SQL 基本编程
定义变量 赋值 取值 分支语句 循环语句 定义变量 declare @变量 数据类型 //@必须带着 不然程序不知道变量是什么 不带@ 电脑会报错 例如 declare ...
- HDU 4291 A Short problem 短问题 (递推,找规律)
题意: 给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路: 要求的g( g( g(n ...
- Assertion failure layoutSublayersOfLayer:], /SourceCache
现象:代码在simulator上能够正常运行但是在真机上出现 Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/ ...
- (转)linux自动备份oracle数据库并上传到备份服务器 脚本实现
实际项目中,备份数据是不可缺少的一步,完成数据的自动备份减少个人的工作量,是我们的目标.之前很少写过脚本,不过这些简单的操作还是可以做到的!话不多说,开始具体介绍:oracle版本:10.2.0操作系 ...
- Android(java)学习笔记139:Android中Menu的使用(静态 和 动态)
1. 使用xml定义Menu(静态方法) 菜单资源文件必须放在res/menu目录中.菜单资源文件必须使用<menu>标签作为根节点.除了<menu>标签外,还有另外两个标签用 ...
- Kubernetes介绍与特性
1.Kubernetes 是什么 简单的来说,k8s可以理解为,一个容器平台,一个微服务平台,便携式云平台,我们那可以很快速的搭建一个服务,快速的运行起来 2.Kubernetes特性