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] ...
随机推荐
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B
Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...
- Python + request接口测试中Cookie和Session的获取和使用
Cookie和Session的简单理解 由于Http协议是无状态的,所以产生了cookie和session进行状态的管理. 从哪里来,在哪里,到哪里去: --> Cookie是由服务端生成,存 ...
- centOS+uwsgi+nginx 部署flask项目,问题记录
用flask做的项目想要部署到centOS系统上,填了一些坑,终于成功了,记录一下遇到的问题: 此次部署主要是按照这个博客进行的 https://www.cnblogs.com/Ray-liang/p ...
- Django 使用Paginator分页
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger subclass_s = models.subclas ...
- hdu3642Get The Treasury
链接 刚开始看n挺小,以为是二维的线段树,想了一会也没想到怎么解,之后看到z值非常小,想到可以直接枚举z,确定一个坐标,然后把三维转化为二维,把体积转化为面. 枚举z从-500到500,然后用面积并的 ...
- Linux crontab 设置定时任务
crontab crontab 用于设置系统自动执行的周期性任务 # m h dom mon dow user command 17 * * * * root cd / && run- ...
- Wordpress 之 Rewrite Rules
----------------------------------------------------------------- 场景描述: 自定义wp主题中,添加了个关于页面(about.php) ...
- rxjava封装,RxBus封装(上线项目集成,声明周期管理,处理溢出内存,支持同时多个请求。)
Github地址 RxLibrary工程:1.rxjava2 + retrofit2的封装,常用的请求(Get,Post,文件上传,文件下载),防止内存泄漏,简单便捷,支持自定义loading等属性. ...
- import 何时使用 "" 和<> Objective-C
Objective-C在这方面与C/C ++相似.引号是给local本地包含的文件的.(你需要指明相对现有文件的相对路径的).而对于尖括号来说,是一个全局路径. 一般情况下引号用在引用自己项目中的类的 ...
- Jenkins怎么启动和停止服务
笔者没有把Jenkins配置到tomcat中,每次都是用命令行来启动Jenkins.但是遇到一个问题:Jenkins一直是开着的,想关闭也关闭不了.百度了一些资料,均不靠谱(必须吐槽一下百度).于是进 ...