****Codeforces Round #363 (Div. 2) C. Vacations(DP 动态规划)
2016-07-22 20:25
531 查看
I - Vacations
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
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
Output
Input
Output
Input
Output
1
一枚举
二dp
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
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
22 2
Output
1
一枚举
#include<stdio.h> int main() { int n,i,sum; int a[110]; while(~scanf("%d",&n)) { sum=0; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=2;i<=n;i++) { if(a[i]==1) { if(a[i-1]==1) { sum++; a[i]=0; } } else if(a[i]==2) { if(a[i-1]==2) { a[i]=0; sum++; } } else if(a[i]==3) { if(a[i-1]==1) a[i]=2; else if(a[i-1]==2) a[i]=1; } else sum++; } if(a[1]==0) sum++; printf("%d\n",sum); } return 0; }
二dp
1
23
4
5
6
7
8
9
1011
1213
14
15
16
17
18
19
2021
2223
24
25
26
27
28
29
3031
#include<stdio.h> #include<algorithm> #include<string.h> #define INF 0x3f3f3f using namespace std; int main() { int n,m,i; int dp[110][5]; while(~scanf("%d",&n)) { memset(dp,INF,sizeof(dp)); dp[0][0]=0; for(i=1;i<=n;i++) { scanf("%d",&m); dp[i][0]=min(min(dp[i-1][1],dp[i-1][2]),dp[i-1][0])+1; if(m==1) dp[i][1]=min(dp[i-1][2],dp[i-1][0]); else if(m==2) dp[i][2]=min(dp[i-1][1],dp[i-1][0]); else if(m==3) { dp[i][1]=min(dp[i-1][0],dp[i-1][2]); dp[i][2]=min(dp[i-1][1],dp[i-1][0]); } } printf("%d\n",min(min(dp[n][1],dp[n][2]),dp[n][0])); } return 0; }
相关文章推荐
- 数据解析
- caffe安装
- NYOJ 510 昂贵的聘礼 dfs
- 快速求幂与快速幂模
- java 反射机制的基本知识
- 漫步微积分十——复合函数和链式法则
- JVM系列三:JVM参数设置、分析
- 基于MVC+EasyUI的Web开发框架经验总结(9)--在Datagrid里面实现外键字段的转义操作
- Codeforces Round #343 (Div. 2) B. Far Relative’s Problem (贪心)
- UVa--839 Not so Mobile
- 你收到了吗,我的三行情书。
- 如何对没有缩进的pdf进行修改目录,快速方法!!!
- Jenkins+SonarQube代码质量检查自动化
- 代理服务器工作原理
- 自学java(1)
- Linux下安装视频转换工具ffmpeg
- Spark总结(二)——RDD的Transformation操作
- shiro验证相关
- 使用 Windows Live Writer 离线编辑器发布文章
- 函数覆盖及其使用举例