題目:
A. Vacationstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya 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 [adj. 連貫的;連續(xù)不斷的]days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
InputThe first line contains a positive [正數(shù)]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.OutputPrint 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.Examplesinput41 3 2 0output2input71 3 3 2 1 2 3output0input22 2output1NoteIn 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.
解釋一下題意:
剛?cè)腴T,看英文題目有困難,邊讀邊查單詞。
Vasyay有n天的假期。。。
主要看Input
第一行:輸入一個(gè)正數(shù)n,代表假期天數(shù);
第二行:輸入n個(gè)正數(shù)a1..ai...an,每個(gè)數(shù)范圍[0,3],
0-----只能休息
1-----比賽或者休息
2-----鍛煉或者休息
3-----比賽或者鍛煉或者休息
輸出:
求休息時(shí)間的最小值,這里有兩個(gè)限制條件,不能連續(xù)兩天鍛煉或者連續(xù)兩天比賽;
動(dòng)態(tài)規(guī)劃DP
dp[i][0]表示第i天是0的最小休息天數(shù)。
dp[i][1]表示第i天是1的最小休息天數(shù)。
dp[i][2]表示第i天是2的最小休息天數(shù)。
狀態(tài)轉(zhuǎn)移方程:
dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
這是綜合寫法,如果看不明白,可以分成三步;
dp[i][0]=min(dp[i-1][0],dp[i-1][1]);
dp[i][0]=min(dp[i][0],dp[i-1][2]);
dp[i][0]++;因?yàn)榈趇天是狀態(tài)0,所以天數(shù)+1
dp[i][1]=min(min(dp[i-1][0],dp[i-1][2]),dp[i-1][1]+1)+0;
特意寫了一個(gè)+0,這樣就很容易看明白了
如果第i天是狀態(tài)1,如果前一天也是狀態(tài)1,那么今天就會(huì)休息,天數(shù)+1;
dp[i][2]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2]+1)+0;
同理;
AC代碼
參考:http://blog.csdn.net/w446506278/article/details/51965690,博主寫的很簡潔,代碼中出現(xiàn)了個(gè)別錯(cuò)誤,本人加上了一些注釋,希望能加深理解。新手入門,寫給自己看。如果有問題可以一起交流啊~
枚舉法也能做,這里就不寫代碼了。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注