CodeForces 699C
来源:互联网 发布:企业网络公关 编辑:程序博客网 时间:2024/06/08 11:31
题目链接: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-699C-Vacations
- CodeForces - 699C Vacations
- codeforces 699C Vacations
- CodeForces 699C
- Codeforces 699 C. Vacations ( 贪心
- CodeForces 699C Vacations 贪心
- 【CodeForces】699C - Vacations(贪心)
- codeforces 699C Vacations (贪心)
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- 零基础学seo教程并非易事
- 再谈Docker容器单机网络:利用iptables trace和ebtables log
- 对通过函数申请内存的理解
- 关于Java工程的资源目录
- 研究生手册(二)
- CodeForces 699C
- 开发一个井字游戏
- 面向对象开发公众号,php代码封装类,函数
- JQuery源码解析–extend篇
- 给定一个没有重复数的数组。建立一棵最大树
- pandas之get_dummies
- 资源帖
- VS2010中添加lib库引用
- Java