Vacations(动态规划加贪心)
来源:互联网 发布:手机算量软件 编辑:程序博客网 时间:2024/05/22 04:36
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
41 3 2 0
2
71 3 3 2 1 2 3
0
22 2
1给你n天的情况,求人最少能休息几天:ai = 0,如果第i个天假期健身房是封闭和不进行比赛;
ai = 1,如果第i个一天的假期健身房是封闭的,但比赛进行;
ai = 2,如果第i个一天的假期健身房开放和不进行比赛;
ai = 3,如果第i个一天的假期健身房是开放和比赛。
任意连续的两天不能连续健身或比赛运用动态规划求解:dp[i][j]是第i天做第j件事最少休息天数#include<stdio.h>#include<algorithm>#include<iostream>using namespace std;int main(){int dp[1500][4];int n,a[10000];while(~scanf("%d",&n)){for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++)for(int j=0;j<=3;j++)dp[i][j]=100000;for(int i=1;i<=n;i++){if(a[i]==0){dp[i][0]=min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+1;} if(a[i]==1){dp[i][0]=min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+1;dp[i][2]=min(dp[i-1][0],dp[i-1][1]);}if(a[i]==2){dp[i][0]=min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+1;dp[i][1]=min(dp[i-1][0],dp[i-1][2]);}if(a[i]==3){dp[i][0]=min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+1;dp[i][2]=min(dp[i-1][0],dp[i-1][1]);dp[i][1]=min(dp[i-1][0],dp[i-1][2]);}}int ans;ans=min(dp[n][0],min(dp[n][1],dp[n][2]));printf("%d\n",ans);}}
- Vacations(动态规划加贪心)
- CodeForces 698A Vacations (动态规划)
- CodeForces 698A——Vacations(动态规划)
- codeforce 698A Vacations——贪心——动态规划
- Vacations【贪心】
- Codeforces Round #363 (Div. 2) C. Vacations(简单动态规划)
- Codeforces 698A: Vacations(贪心)
- CodeForces 698A—Vacations(贪心)
- 【CodeForces】699C - Vacations(贪心)
- 【Codeforces】-698A-Vacations(贪心)
- codeforces 699C Vacations (贪心)
- Codeforces Round #363 (Div. 2) C. Vacations(DP 动态规划)
- ****Codeforces Round #363 (Div. 2) C. Vacations(DP 动态规划)
- 【动态规划】Codeforces 698A & 699C Vacations
- 加等式(动态规划)
- 动态规划、贪心、dynamic programming(DP)
- BUYING FEED(贪心+树状动态规划)
- 贪心算法(动态规划问题)
- 鞍点-week7-C语言习题集
- 如何在Android Studio中创建assets文件夹
- 七步从AngularJS菜鸟到专家(4和5):指令和表达式
- JAVA学习笔记--基本事件
- MFC类别型录网之对象动态创建
- Vacations(动态规划加贪心)
- 102. Binary Tree Level Order Traversal
- HDU 5324 (CDQ分治 树状数组)
- 调用百度报Cannot read property 'lng' of null错误
- hdu 5742 It's All In The Mind(2016 Multi-University Training Contest 2——思维题)
- hdu 5187 zhx's contest(快速幂,快速乘法,排列组合)
- git stash 使用有感
- 在方法中定义内部类
- TCP/IP协议簇常见问题