Vacations
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
————————————————————————————————————————————————
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.
Examples
input
4
1 3 2 0
output
2
input
7
1 3 3 2 1 2 3
output
0
input
2
2 2
output
1
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,结果就是一直改,其实收获还是蛮大的,就是题目没做出来。
错误代码:
——————————————————————————————————————————
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct day{
int work;
int work2;
int biaoji;
}a[200];
int main()
{
int t,i,j,count[4],flag=1,flag2=0,flag3=0,flag4=1;
scanf("%d",&t);
for( i=0; i<t; i++ )
{
scanf("%d",&a[i].work);
if( a[0].work == 0 )
{
flag3=1;
}
}
if( a[0].work == 3 )
{
flag=2;
flag2=1;
}
while( flag )
{
for( i=0; i<t; i++ )
{
a[i].work2=a[i].work;
a[i].biaoji=1;
}
if( a[0].work2 == 3 ) a[0].work2 = flag;
for( i=1; i<t; i++ )
{
if( a[i].work2 == 3 && a[i-1].work2 == 1 )
{
a[i].work2 = 2;
}
else if( a[i].work2 == 3 && a[i-1].work2 == 2 )
{
a[i].work2 = 1;
}
else if( a[i-1].work2 == 0 && a[i].work2 == 3 )
{
for( j=1; j<t-i; j++ )
{
if( a[i+j].work2 == 2 || a[i+j].work2 == 0 )
{
if(j%2 == 0) a[i].work2 = 2;
else a[i].work2 = 1;
break;
}
else if( a[i+j].work2 == 1 )
{
if(j%2 == 0) a[i].work2 = 1;
else a[i].work2 = 2;
break;
}
}
}
}
for( i=0; i<t ; i++ )
printf("%d ",a[i].work2);
printf("\n");
for( i=1; i<t; i++ )
{
if( a[i].work2 == a[i-1].work2 )
{
a[i].biaoji=0;
flag4++;
}
if( i > 1)
{
if( a[i].work2 == a[i-1].work2 && flag4 == 3)
{
a[i].biaoji=1;
flag4--;flag4--;
}
}
if( a[i].work2 != a[i-1].work2 ) flag4 = 1;
if( a[i].work2 == 0 )
a[i].biaoji=0;
if( a[i].work2 == 3 )
a[i].biaoji=1;
}
for( i=0; i<t ; i++ )
printf("%d ",a[i].biaoji);
count[flag]=0;
for( i=0; i<t; i++ )
{
if( a[i].biaoji == 0 )
{
count[flag]++;
}
}
flag--;
}
if( flag2 )
{
if( count[2] > count[1] )
printf("%d\n",count[1]+flag3);
else printf("%d\n",count[2]+flag3);
}
else printf("%d\n",count[1]+flag3);
}