Codeforces Round #612 (Div. 2) C. Garland

Codeforces Round #612 (Div. 2) C. Garland

Vadim loves decorating the Christmas tree, so he got a beautiful garland as a present. It consists of n light bulbs in a single row. Each bulb has a number from 1 to n (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.

Codeforces Round #612 (Div. 2) C. Garland

Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by ?). For example, the complexity of 1 4 2 3 5 is 2 and the complexity of ?1 3 5 7 6 4 2? is ??.

No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.

Input

The first line contains a single integer ? — the number of light bulbs on the garland.

The second line contains n integers ? — the number on the ? bulb, or ? if it was removed.

Output

Output a single number — the minimum complexity of the garland.

Examples

input

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n103" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 5
0 5 0 2 3</pre>

output

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n106" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 2</pre>

input

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n109" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 7
1 0 0 5 0 0 2</pre>

output

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n112" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1</pre>

Note

In the first example, one should place light bulbs as 1 5 4 2 3. In that case, the complexity would be equal to 2, because only (5,4)and (2,3) are the pairs of adjacent bulbs that have different parity.

In the second case, one of the correct answers is 1 7 3 5 6 4 2.

思路:動態(tài)規(guī)劃

一維:長度违帆,

二維:剩余偶數(shù)個數(shù)

三維:當前數(shù)奇偶性

界限:

  • 當存在偶數(shù)且第一個數(shù)為0或偶數(shù):?

  • 當?shù)谝粋€數(shù)為 00 或奇數(shù):?

狀態(tài)轉(zhuǎn)移:

  • 當前填偶數(shù):?

  • 當前填奇數(shù):?

AC:

<pre mdtype="fences" cid="n13" lang="cpp" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, a[maxn], dp[maxn][maxn][2];
int main(){
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
memset(dp, 0x3f, sizeof(dp));
if(n > 1 && (a[1] == 0 || a[1] % 2 == 0))
dp[1][n / 2 - 1][0] = 0;
if(a[1] % 2 == 1 || a[1] == 0)
dp[1][n / 2][1] = 0;
for (int i = 2; i <= n; i++){
for (int j = 0; j <= n / 2; j++){
for (int k = 0; k < 2; k++){
if(j > 0 && (a[i] == 0 || a[i]%2 == 0))
dp[i][j - 1][0] = min(dp[i][j - 1][0], dp[i - 1][j][k] + (k == 1));
if(i+j-1 < n && (a[i] == 0 || a[i] % 2 == 1))
dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][k] + (k == 0));
}
}
}
cout << min(dp[n][0][0], dp[n][0][1]) << endl;
return 0;
}</pre>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末呈队,一起剝皮案震驚了整個濱河市壤蚜,隨后出現(xiàn)的幾起案子哆窿,更是在濱河造成了極大的恐慌务甥,老刑警劉巖麻蹋,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宛徊,死亡現(xiàn)場離奇詭異歧杏,居然都是意外死亡陪每,警方通過查閱死者的電腦和手機影晓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來檩禾,“玉大人挂签,你說我怎么就攤上這事∨尾” “怎么了饵婆?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長戏售。 經(jīng)常有香客問我侨核,道長,這世上最難降的妖魔是什么灌灾? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任搓译,我火速辦了婚禮,結(jié)果婚禮上锋喜,老公的妹妹穿的比我還像新娘些己。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布段标。 她就那樣靜靜地躺著涯冠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪怀樟。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天盆佣,我揣著相機與錄音往堡,去河邊找鬼。 笑死共耍,一個胖子當著我的面吹牛虑灰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播痹兜,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼穆咐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了字旭?” 一聲冷哼從身側(cè)響起对湃,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎遗淳,沒想到半個月后拍柒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡屈暗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年拆讯,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片养叛。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡种呐,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出弃甥,到底是詐尸還是另有隱情爽室,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布淆攻,位于F島的核電站肮之,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏卜录。R本人自食惡果不足惜戈擒,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望艰毒。 院中可真熱鬧筐高,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至稽屏,卻和暖如春扮宠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背狐榔。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工坛增, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人薄腻。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓收捣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親庵楷。 傳聞我的和親對象是個殘疾皇子罢艾,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353