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] ...
随机推荐
- 牛客寒假5-I.炫酷镜子
链接:https://ac.nowcoder.com/acm/contest/331/I 题意: 小希拿到了一个镜子块,镜子块可以视为一个N x M的方格图,里面每个格子仅可能安装`\`或者`/`的镜 ...
- Jasper_crosstab_Parameter_Crosstab Header
corsstab: Q : how to show filed value at crosstab Header Part? A : via pass parameter in crosstab. i ...
- python入门之三元运算,存址方式,深浅拷贝
三元运算 格式: name = 值1 if 条件 else 值2 如果条件为True,那么将值1赋值给name,条件为False,那么将值2赋值给name 存址方式 不同的数据类型在内存中的存址方式不 ...
- 自己项目中PHP常用工具类大全分享
<?php /** * 助手类 * @author www.shouce.ren * */ class Helper { /** * 判断当前服务器系统 * @return string */ ...
- springmvc当要返回中文字符串时出现乱码
当过滤器,页面编码都对,tomcat版本在8以上(8内部默认用utf-8) 在方法参数中加上,produces="text/html;charset=UTF-8" 绝对可以解决!! ...
- 头部和信号栏一个颜色appcloud
<header id="header" > <ul > <li class="active" onclick="api. ...
- nodejs json
var express = require('express');var router = express.Router(); /* GET home page. */ router.get('/', ...
- hihocoder1032 最长回文子串
思路: manacher模板. 实现: #include <iostream> #include <cstring> using namespace std; ]; strin ...
- Random-数组
1.能够使用Random生成随机数 1)import java.util.Random; 2)Random r = new Random(); 3)r.nextIn ...
- 微信支付v3开发(5) 扫码并输入金额支付
关键字:微信支付 微信支付v3 动态native支付 统一支付 Native支付 prepay_id 作者:方倍工作室 本文介绍微信支付下的扫描二维码并输入自定义金额的支付的开发过程. 注意 微信支付 ...