洛谷P4170 [CQOI2007]涂色(区间dp)
程序员文章站
2022-10-06 23:34:04
题意 "题目链接" Sol 震惊,某知名竞赛网站竟照搬省选原题! 裸的区间dp,$f[l][r]$表示干掉$[l, r]$的最小花费,昨天写的时候比较困于是就把能想到的转移都写了。。 cppp // luogu judger enable o2 // luogu judger enable o2 i ......
题意
sol
震惊,某知名竞赛网站竟照搬省选原题!
裸的区间dp,\(f[l][r]\)表示干掉\([l, r]\)的最小花费,昨天写的时候比较困于是就把能想到的转移都写了。。
// luogu-judger-enable-o2 // luogu-judger-enable-o2 #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 = 1e6 + 10, 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; char s[maxn]; int f[501][501]; signed main() { scanf("%s", s + 1); n = strlen(s + 1); memset(f, 0x3f, sizeof(f)); for(int i = 1; i <= n; i++) f[i][i] = 1; for(int len = 2; len <= n; len++) { for(int l = 1; l + len - 1 <= n; l++) { int r = l + len - 1; if(s[l] == s[l + 1]) chmin(f[l][r], f[l + 1][r]); else chmin(f[l][r], f[l + 1][r] + 1); if(s[r] == s[r - 1]) chmin(f[l][r], f[l][r - 1]); else chmin(f[l][r], f[l][r - 1] + 1); if(s[l] == s[l + 1] && s[r] == s[r - 1]) chmin(f[l][r], min(min(f[l + 1][r], f[l][r - 1]), f[l + 1][r - 1] + 1)); else chmin(f[l][r], f[l + 1][r - 1] + 2); if(s[l] == s[r]) chmin(f[l][r],min(min(f[l + 1][r], f[l][r - 1]), f[l + 1][r - 1] + 1)); for(int k = l; k < r; k++) chmin(f[l][r], f[l][k] + f[k + 1][r]); } } cout << f[1][n]; return 0; }
上一篇: 洛谷P3382 【模板】三分法(三分)
下一篇: PHP扩展Memcache分布式部署方案