BZOJ1802: [Ahoi2009]checker(性质分析 dp)
程序员文章站
2022-06-14 23:34:33
题意 "题目链接" Sol 一个不太容易发现但是又很显然的性质: 如果有两个相邻的红格子,那么第一问答案为0, 第二问可以推 否则第一问答案为偶数格子上的白格子数,第二问答案为偶数格子上的红格子数 cpp include define Pair pair define MP(x, y) make_p ......
题意
sol
一个不太容易发现但是又很显然的性质:
如果有两个相邻的红格子,那么第一问答案为0, 第二问可以推
否则第一问答案为偶数格子上的白格子数,第二问答案为偶数格子上的红格子数
#include<bits/stdc++.h> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second //#define int long long #define ll long long #define fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int maxn = 1001, mod = 1e9 + 7, inf = 1e9 + 10; const double eps = 1e-9; template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;} template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;} template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;} template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;} template <typename a> inline void debug(a a){cout << a << '\n';} template <typename a> inline ll sqr(a x){return 1ll * x * x;} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, a[maxn]; ll f[maxn]; signed main() { n = read(); int ans[2] = {0, 0}, flag = 0; memset(f, 0x3f, sizeof(f)); for(int i = 1; i <= n; i++) { a[i] = read(); if(i > 2 && a[i] && a[i] == a[i - 1]) flag = 1; if((!(i & 1))) ans[a[i]]++; if(a[i]) f[i] = 1; } if(!flag) {printf("%d\n%d", ans[0], ans[1]); return 0;} for(int i = 2; i < n; i++) { if(a[i] && a[i + 1]) { for(int j = i - 1; j > 1; j--) chmin(f[j], f[j + 1] + f[j + 2]); for(int j = i + 2; j < n; j++) chmin(f[j], f[j - 1] + f[j - 2]); } } ll out = 0; for(int i = 2; i < n; i += 2) out += f[i]; cout << 0 << "\n" << out; return 0; } /* 5 0 0 1 1 0 */
下一篇: Linux文件权限