【动态规划】Codeforces 699C Vacations

题目链接:

  http://codeforces.com/problemset/problem/699/C

题目大意:

  N天,A(健身)或B(做比赛)或休息,每天都有4种情况,A可行B可行,A可行B不行,A不行B可行,AB都不行。

  每天选择一种,不能连续两天选择同一种活动(可以连续休息),问最少休息几天。

题目思路:

  【动态规划】

  f[i][j]表示前i天,最后一天状态为j的最多休息天数(最少天数也行),j=0,1,2表示休息,运动和做比赛。

  转移方程挺好推的。

 1 //
 2 //by coolxxx
 3 ////<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define MAX 0x7f7f7f7f
26 #define PI 3.14159265358979323
27 #define N 1004
28 using namespace std;
29 typedef long long LL;
30 int cas,cass;
31 int n,m,lll,ans;
32 int a[N];
33 int f[N][3];
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37     freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43     while(~scanf("%d",&n))
44 //    while(~scanf("%d",&n))
45     {
46         for(i=1;i<=n;i++)
47         {
48             scanf("%d",&a[i]);
49         }
50         for(i=1;i<=n;i++)
51         {
52             if(a[i]==0)
53             {
54                 f[i][0]=max(f[i-1][1],f[i-1][2]);
55                 f[i][0]=max(f[i][0],f[i-1][0]);
56                 f[i][1]=f[i][2]=-MAX;
57             }
58             else if(a[i]==1)
59             {
60                 f[i][0]=max(f[i-1][1],f[i-1][2]);
61                 f[i][0]=max(f[i][0],f[i-1][0]);
62                 f[i][1]=max(f[i-1][1],f[i-1][2]+1);
63                 f[i][1]=max(f[i][1],f[i-1][0]+1);
64                 f[i][2]=-MAX;
65             }
66             else if(a[i]==2)
67             {
68                 f[i][0]=max(f[i-1][1],f[i-1][2]);
69                 f[i][0]=max(f[i][0],f[i-1][0]);
70                 f[i][2]=max(f[i-1][2],f[i-1][1]+1);
71                 f[i][2]=max(f[i][2],f[i-1][0]+1);
72                 f[i][1]=-MAX;
73             }
74             else
75             {
76                 f[i][0]=max(f[i-1][1],f[i-1][2]);
77                 f[i][0]=max(f[i][0],f[i-1][0]);
78                 f[i][1]=max(f[i-1][2],f[i-1][0])+1;
79                 f[i][2]=max(f[i-1][1],f[i-1][0])+1;
80             }
81         }
82         ans=max(f[n][0],f[n][1]);
83         printf("%d\n",n-max(ans,f[n][2]));
84     }
85     return 0;
86 }
87 /*
88 //
89
90 //
91 */

时间: 2024-10-19 11:11:41

【动态规划】Codeforces 699C Vacations的相关文章

CodeForces 699C Vacations

简单$dp$. 记$dp[i][j]$表示$i$天过去了,并且第$i$天的时候是状态$j$的情况下,前$i$天最少休息天数. 递推式很容易得到: $dp[i][0]=min(dp[i-1][0],min(dp[i-1][1],dp[i-1][2]))+1$.$if\left( {1\& a\left[ i \right]} \right)dp\left[ i \right]\left[ 1 \right] = min\left( {dp\left[ {i - 1} \right]\left[ 0

CodeForces 698A Vacations

题目链接 : http://codeforces.com/problemset/problem/698/A 题目大意: 阿Q有n天假期,假期中有三种安排 休息.健身.比赛.每天有三种选择条件: 0 健身房不开门 没有比赛 1 健身房不开门    有比赛 2 健身房开门     没有比赛  3 健身房开门   有比赛 请给阿Q 合理的安排他的假期[阿Q不能连着两天健身或者连着两天比赛],使得阿Q的休息天数最少. 解题思路: ans=n,最多休息ans天 第一天是3 则a[0]=0,ans--; 只

Codeforces 698A - Vacations - [简单DP]

题目链接:http://codeforces.com/problemset/problem/698/A 题意: 有 $n$ 天假期,每天有四种情况:0.体育馆不开门,没有比赛:1.体育馆不开门,有比赛:2.体育馆开门,没有比赛:3.体育馆开门,有比赛. 每天都可以选择一件事做:休息.去体育馆运动.打比赛. 现在有一个限制条件:不能连续两天都去体育馆,或者连续两天都打比赛.要求尽量使得休息的天数最少,求出这个天数. 题解: $f[i][0,1,2]$ 表示前 $i$ 天,第 $i$ 天休息/运动/

CodeForces 698A - Vacations (Codeforces Round #363 (Div. 2))

要么去体育馆,要么去比赛,要么闲在家里 给出每一天体育馆和比赛的有无情况,要求连续两天不能去同一个地方 问最少闲几天 DP方程很容易看出 dp(第i天能去的地方) = min(dp(第i-1天的三种情况)) : dp(第i天呆在家里) = min(dp(第i-1天的三种情况))+1: 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 #define inf 0x3f3f3f3f 5 int a[10

【杂题总汇】Codeforces-67A Partial Teacher

[Codeforces-67A]Partial Teacher 上周刷了一大堆小紫薯的动态规划的题??--老师给我们布置了一场测试,感觉还好吧--虽然我并不是这么擅长动态规划?? +Codeforces 传送门+ ◇ 题目 <手写翻译> 老师要给n个学生发糖,n个学生排成一队.由于老师有偏见??,对于一对相邻的学生,他会给成绩好的学生多一些糖(严格大于),而成绩好的学生少一些糖(严格小于),若相邻学生成绩相等,那么他们得到的糖的数量也是一样的.每个学生都至少得到一颗糖.求老师最少总共要给学生多

Codeforces 830D Singer House - 动态规划

It is known that passages in Singer house are complex and intertwined. Let's define a Singer k-house as a graph built by the following process: take complete binary tree of height k and add edges from each vertex to all its successors, if they are no

动态规划,就是这样! CodeForces 433B - Kuriyama Mirai&#39;s Stones

Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She wi

Codeforces 834D The Bakery - 动态规划 - 线段树

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery. Soon the expenses started to overcome the income, so Slastyona decid

Codeforces 837D Round Subset - 动态规划 - 数论

Let's call the roundness of the number the number of zeros to which it ends. You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible. Input