zoukankan      html  css  js  c++  java
  • 洛谷P3146 [USACO16OPEN]248 G

    (Large extbf{Description: } large{有n个数的序列,每次可以合并相邻的两个相同的数,得到的一个数原数加一,输出最大能合并多大。(2 leq n leq 248)}\)

    (Large extbf{Solution: } large{一道典型的区间DP。我们可以枚举合并区间的长度与起点还有中间断点,然后同时更新答案。值得注意的是,最大值并不一定是 ext{f[1][n]}。\})

    (Large extbf{Code: })

    #include <cstdio> 
    #include <algorithm>
    #define LL long long
    #define gc() getchar()
    #define rep(i, a, b) for (int i = (a); i <= (b); ++i)
    using namespace std;
    const int N = 255;
    int n, ans, a, f[N][N];
    
    inline int read() {
    	char ch = gc();
    	LL ans = 0;
    	while (ch > '9' || ch < '0') ch = gc();
    	while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
    	return ans;		
    }
    
    int main() {
    	n = read();
    	rep(i, 1, n) a = read(), f[i][i] = a;
    	rep(i, 2, n) 
    		for (int j = 1; j + i - 1 <= n; ++j) {
    			int en = i + j - 1;
    			rep(k, j, en - 1) 
    				if (f[j][k] == f[k + 1][en] && f[j][k] != 0 && f[k + 1][en] != 0) { f[j][en] = max(f[j][en], f[j][k] + 1); ans = max(ans, f[j][k] + 1); } 
    		}
    	printf("%d
    ", ans);
    	return 0;
    } 
    
  • 相关阅读:
    ssh 配置
    c指针
    centos 配置
    mac terminal 配色
    【webrtc】coturn服务搭建(27)
    【webrtc】SDP(26)
    【webrtc】webrtc通过peerconnection进行音视频互通(25)
    【webrtc】媒体协商的过程(24)
    【webrtc】STUN协议(23)
    【webrtc】webrtc网络传输基本知识(22)
  • 原文地址:https://www.cnblogs.com/Miraclys/p/12469710.html
Copyright © 2011-2022 走看看