[SCOI2005]掃雷(遞推)
阿新 • • 發佈:2018-11-08
題面
Solution
我們考慮第一個雷如果確定了,顯然後面每一個地方是否有雷都確定了,於是考慮2次遞推。
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<iostream> #include<queue> #define ll long long #define file(a) freopen(a".in","r",stdin)//;freopen(a".out","w",stdout) using namespace std; inline int gi(){ int sum=0,f=1;char ch=getchar(); while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();} while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return f*sum; } inline ll gl(){ ll sum=0,f=1;char ch=getchar(); while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();} while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return f*sum; } int a[500010],flag[500010],ans1,ans2; int main(){ #ifndef ONLINE_JUDGE file("example"); #endif int i,j,n,m,k; n=gi();for(i=1;i<=n;i++)a[i]=gi(); flag[1]=0;ans1=1,ans2=1; for(i=2;i<=n+1;i++){ flag[i]=a[i-1]-flag[i-1]-flag[i-2]; if(flag[i]<0 || flag[i]>1){ans1=0;break;} } if(flag[n+1])ans1=0; flag[1]=1; for(i=2;i<=n+1;i++){ flag[i]=a[i-1]-flag[i-1]-flag[i-2]; if(flag[i]<0 || flag[i]>1){ans2=0;break;} } if(flag[n+1])ans2=0; printf("%d\n",ans1+ans2); return 0; }