国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Vacations -CodeForces-698A

2019-11-08 19:32:23
字體:
供稿:網(wǎng)友

題目:

A. Vacationstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 [adj. 連貫的;連續(xù)不斷的]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 [正數(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.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.Examplesinput
41 3 2 0output
2input
71 3 3 2 1 2 3output
0input
22 2output
1Note

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.

解釋一下題意:

剛?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ò)誤,本人加上了一些注釋,希望能加深理解。新手入門,寫給自己看。如果有問題可以一起交流啊~

枚舉法也能做,這里就不寫代碼了。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 淮南市| 平遥县| 集贤县| 民勤县| 阿勒泰市| 西乡县| 云安县| 淳化县| 富蕴县| 资中县| 瓮安县| 万宁市| 新安县| 百色市| 崇州市| 嘉善县| 永嘉县| 吉林市| 微博| 武川县| 瓦房店市| 大同市| 九江市| 沾化县| 闽清县| 菏泽市| 乡城县| 察雅县| 绵竹市| 桦南县| 高安市| 神木县| 太原市| 同江市| 庐江县| 阆中市| 海丰县| 夏津县| 平谷区| 金门县| 行唐县|