zoukankan      html  css  js  c++  java
  • 动态规划专题[数字三角形][HDU1176 免费馅饼]

    以时间和位置作为动态规划的状态,在时间T时,位置x上的最大馅饼数等于可以转移到这个位置的其他位置的最大值+该位置上的馅饼数量。可以自底向上逐层求出每个位置可以达到的最大数量,也可以自顶向下利用记忆化搜索求出来。状态转移方程为

    [m[i][j] = m[i][j] + max(m[i+1][j-1], m[i+1][j], m[i+1][j+1]) ]

    const int maxn = 100000+5;
    int m[maxn][11];
    
    int main() {
    #ifdef LOCAL
    	freopen("input.txt", "r", stdin);
    #endif
    	
    	int n; while (scanf("%d", &n) && n) {
    		memset(m, 0, sizeof(m));
    		int x, T, maxt = 0;
    		// 初始化动态规划数组
    		for (int i = 0; i < n; ++i) {
    			scanf("%d%d", &x, &T);
    			m[T][x]++;
    			maxt = max(maxt, T);
    		}
    		for (int i = maxt; i > 0; --i) {
    			for (int j = 0; j < 11; ++j) {
    				if (j == 0) m[i][j] = max(m[i+1][0], m[i+1][1])+m[i][j];
    				else if (j == 10) m[i][j] = max(m[i+1][9], m[i+1][10])+m[i][j];
    				else m[i][j] = max(m[i+1][j-1], max(m[i+1][j], m[i+1][j+1]))+m[i][j];
    			}
    		}
    		printf("%d
    ", max(m[1][5], max(m[1][4], m[1][6])));
    
    	}
    	return 0;
    }
    
  • 相关阅读:
    CF703D Mishka and Interesting sum
    CF697D Puzzles
    SCOI2017酱油记
    [BZOJ4730][清华集训2016][UOJ266] Alice和Bob又在玩游戏
    BZOJ4311:向量
    BZOJ4520: [Cqoi2016]K远点对
    BZOJ4555: [Tjoi2016&Heoi2016]求和
    [Codechef November Challenge 2012] Arithmetic Progressions
    agc040
    补题
  • 原文地址:https://www.cnblogs.com/jeffy-chang/p/7004483.html
Copyright © 2011-2022 走看看