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

首頁 > 學院 > 開發設計 > 正文

POJ3185-The Water Bowls-反轉問題

2019-11-08 18:29:40
字體:
來源:轉載
供稿:網友

原題鏈接 The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6210 Accepted: 2443 Description

The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (PRoperly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls.

Their snouts, though, are so wide that they flip not only one bowl but also the bowls on either side of that bowl (a total of three or – in the case of either end bowl – two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable – it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up? Input

Line 1: A single line with 20 space-separated integers Output

Line 1: The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to 0). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 20 0’s. Sample Input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 Sample Output

3 Hint

Explanation of the sample:

Flip bowls 4, 9, and 11 to make them all drinkable: 0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state] 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4] 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11] Source

USACO 2006 January Bronze 題意:有20個碗排成一排,有些碗口朝上,有些碗口朝下。每次可以反轉其中的一個碗,但是在反轉該碗時,該碗左右兩邊的碗也跟著被反轉(如果該碗為邊界上的碗,則只有一側的碗被反轉)。求最少需要反轉幾次,可以使得所有碗口均朝上。 思路:對于第0只碗可以選擇反轉或者不翻轉,如果反轉第i只碗,那么第i+1,第i+2只碗也會反轉,但是我們只是記錄下來第i只碗是否反轉即可,由于 這里寫圖片描述 的奇偶決定了i處是否需要反轉,而 這里寫圖片描述 決定了i+1處是否需要反轉,其實我們發現二者就是+f[i]和-f[i-k+1]的關系,所以我們維護的其實就是一個區間的和,這樣的話我們就可以把復雜度從O(nk)降低到O(n)

#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 22;int a[maxn],f[maxn];//假設0和21位置有一個瓶子,0位置的瓶子是否倒置作為初始條件,而最后一次操作就停留在19上,我們認為每次操作只是以區間最左端的元素是否倒立來確定是否需要對這個區間進行反轉int c(int first,int k){ memset(f,0,sizeof(f)); int sum=0,res=0; if(first==1){ sum=1;res=1;f[0]=1; } for(int i=1;i<20;i++){ if((sum+a[i])&1) f[i]=1; if(i+1-k>=0) sum -= f[i+1-k]; sum += f[i]; } if((f[18]+f[19]+a[20])&1) return INF; for(int i=1;i<20;i++){ if(f[i]) res++; } return res;}int main(){ for(int i=1;i<=20;i++) scanf("%d",&a[i]); cout << min(c(0,3),c(1,3)) << endl; return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南召县| 龙海市| 灵宝市| 枝江市| 阿拉善右旗| 乌海市| 方山县| 乌兰浩特市| 石门县| 绥中县| 陇川县| 巢湖市| 南木林县| 嵊州市| 广州市| 福建省| 陆良县| 石家庄市| 枝江市| 尉氏县| 遂宁市| 西乌| 武山县| 延庆县| 恩平市| 襄城县| 义乌市| 马尔康县| 洪湖市| 额敏县| 邹平县| 保定市| 荥经县| 颍上县| 措勤县| 潞城市| 黄骅市| 开江县| 富裕县| 张北县| 汉川市|