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
4
1 3 2 0
2
7
1 3 3 2 1 2 3
0
2
2 2
1
题意:给你n天的情况
0 代表休息
1 代表只能参加contest或休息
2 代表只能参加gym或休息
3 代表能参加contest或gym或休息
要求 不能连续参加contest 不能连续参加gym
问 如何安排使得休息日最少 输出休息日的数量
题解: dp处理 dp[i][j] 代表 第i天进行j活动的情况下的前i天的最多非休息日的数量
三个状态的转移方程
dp[i][1]=max(dp[i-1][2],dp[i-1][0])+1;
dp[i][2]=max(dp[i-1][1],dp[i-1][0])+1;
dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]);
结果为n-max(max(dp[n][0],dp[n][1]),dp[n][2])
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 int n; 6 int a[105]; 7 int dp[105][4]; 8 int main() 9 { 10 scanf("%d",&n); 11 memset(dp,0,sizeof(dp)); 12 for(int i=1;i<=n;i++) 13 scanf("%d",&a[i]); 14 if(a[1]==0) 15 dp[1][0]=0; 16 if(a[1]==1) 17 dp[1][1]=1; 18 if(a[1]==2) 19 dp[1][2]=1; 20 if(a[1]==3) 21 { 22 dp[1][1]=1; 23 dp[1][2]=1; 24 dp[1][0]=0; 25 } 26 for(int i=2;i<=n;i++) 27 { 28 if(a[i]==1) 29 { 30 dp[i][1]=max(dp[i-1][2],dp[i-1][0])+1; 31 dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]); 32 } 33 if(a[i]==2) 34 { 35 dp[i][2]=max(dp[i-1][1],dp[i-1][0])+1; 36 dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]); 37 } 38 if(a[i]==0) 39 { 40 dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]); 41 } 42 if(a[i]==3) 43 { 44 dp[i][1]=max(dp[i-1][2],dp[i-1][0])+1; 45 dp[i][2]=max(dp[i-1][1],dp[i-1][0])+1; 46 dp[i][0]=max(max(dp[i-1][0],dp[i-1][1]),dp[i-1][2]); 47 } 48 } 49 int ans=0; 50 for(int i=0;i<=2;i++) 51 ans=max(ans,dp[n][i]); 52 cout<<n-ans<<endl; 53 return 0; 54 }
贪心处理 我gou代码
重点在3状态的处理上 若当前状态为3 则判断前一天状态
若前一天为1状态 则 将当前状态改为2 非休息日++
若前一天为2状态 则 将当前状态改为1 非休息日++
若前一天为0状态 则 将当前状态改为0 非休息日++ 当前状态改为0意味着 无论后一个状态为什么
当前这一天都有相应的状态可以更改 使得当前这一天为非休息日 即当前这一天 对后一天没有影响。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <string> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <stack> 11 #include <sstream> 12 #include <vector> 13 #define PI acos(-1.0) 14 const int inf = (1<<30) - 10; 15 using namespace std; 16 inline int get_int() 17 { 18 int r=0; 19 char c; 20 while((c=getchar())!=' '&&c!='\n') 21 r=r*10+c-'0'; 22 return r; 23 } 24 inline void out(int x) 25 { 26 if(x>10) 27 { 28 out(x/10); 29 } 30 putchar(x % 10 + '0'); 31 putchar('\n'); 32 } 33 /****************************************/ 34 int a[200]; 35 int main() 36 { 37 int n,m; 38 cin>>n; 39 for(int i=0;i<n;i++){ 40 scanf("%d",&a[i]); 41 } 42 m=0; 43 if(a[0]>0){ 44 m++; 45 } 46 if(a[0]==3) 47 a[0]=0; 48 for(int i=1;i<n;i++){ 49 if(a[i]==1){ 50 if(a[i-1]!=1) 51 m++; 52 else a[i]=0; 53 } 54 if(a[i]==2){ 55 if(a[i-1]!=2) 56 m++; 57 else a[i]=0; 58 } 59 if(a[i]==3){ 60 if(a[i-1]==1){ 61 a[i]=2; 62 m++; 63 } 64 if(a[i-1]==2){ 65 a[i]=1; 66 m++; 67 } 68 if(a[i-1]==0){ 69 a[i]=0; 70 m++; 71 } 72 } 73 } 74 printf("%d\n",n-m); 75 return 0; 76 }
另外附一种奇怪搞法 逻辑运算处理
http://blog.csdn.net/nare123/article/details/51966794