做水题的感觉真好系列

HDU 2084 数塔

1: 1
2: 1 2
3: 1 2 3
4: 1 2 3 4
5: 1 2 3 4 5
dp[i][j]第i行第j个数取得的最大值
dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + a[i][j]。

代码:

/*********************************************************
Problem : 2084 ( 数塔 )     Judge Status : Accepted
RunId : 14525016    Language : G++    Author : G_lory
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
**********************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int a[105][105];
int dp[105][105];
int N;

void solve()
{
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= i; ++j) {
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + a[i][j];
        }
    }
    int ans = dp[N][1];
    for (int i = 2; i <= N; ++i) {
        ans = max(ans, dp[N][i]);
    }
    printf("%d\n", ans);
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &N);
        for (int i = 1; i <= N; ++i)
            for (int j = 1; j <= i; ++j)
                scanf("%d",&a[i][j]);
        solve();
    }
    return 0;
} 

HDU 2044 一只小蜜蜂...

Fibonacci数列。

代码:

/*********************************************************
Problem : 2044 ( 一只小蜜蜂... )     Judge Status : Accepted
RunId : 14532121    Language : G++    Author : G_lory
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
*********************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

typedef long long ll;
ll dp[55];

void init()
{
    dp[0] = dp[1] = 1;

    for (int i = 2; i <= 50; ++i)
        dp[i] = dp[i - 1] + dp[i - 2];
}

int main()
{
    init();
    int t, a, b;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &a, &b);
        printf("%lld\n", dp[b - a]);
    }
    return 0;
}

  

HDU 2041 超级楼梯

同上题。

代码:

/********************************************************
Problem : 2041 ( 超级楼梯 )     Judge Status : Accepted
RunId : 14532404    Language : G++    Author : G_lory
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
*********************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

typedef long long ll;
ll dp[45];

void init()
{
    dp[1] = dp[2] = 1;

    for (int i = 3; i <= 40; ++i)
        dp[i] = dp[i - 1] + dp[i - 2];
}

int main()
{
    init();
    int t, a;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &a);
        printf("%lld\n", dp[a]);
    }
    return 0;
}

  

HDU 2050 折线分割平面

一开始思路就错了,想的是一条直线最大的切割方法是把所有的面的切成两半,当然那是不可能的,最优的方法是把每条已有直线分成两段,这样该直线被已有直线分成已有直线条数+1的段,每一段把所在区域分成两半。

如果是折线,就把原有区域以最优的方式分解两次,注意两次方式相同,不能先分一次,在把第一部分的结果计算进去划第二次。因为是折线,在两条直线划分的基础上要连起来一端,所以少了1个区域。

dp[i] = dp[i-1]+((i-1)*2+1)*2-1; (我竟然忘了怎么通过递推公式求通项 = =……

(i-1)*2 原有直线(一条折线看成两条直线)条数。

((i-1)*2+1) 新加一条线段,被切割成多少分,也就是新加一条线段,多几个区域。

((i-1)*2+1)*2 因为是折线,相当于多加两条线段。

((i-1)*2+1)*2-1 折线会因为合并起来那一端减少一个区域。

代码:

/*********************************************************
Problem : 2050 ( 折线分割平面 )     Judge Status : Accepted
RunId : 14533258    Language : G++    Author : G_lory
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
*********************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

int dp[10005];

void init()
{
    dp[0] = 1;
    dp[1] = 2;
    for (int i = 2; i <= 10000; ++i) {
        //dp[i] = dp[i - 1] + ((i - 1) * 2 + 1) * 2 - 1;
        dp[i] = dp[i - 1] + 4 * i - 3;
    }
}

int main()
{
    int t, n;
    init();
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        printf("%d\n", dp[n]);
    }
    return 0;
}

  

动态规划之HDU水题的更多相关文章

  1. Let the Balloon Rise HDU水题

    题意 让你统计字符串最多的那个串,并输出 分析 直接用map统计,不断更新最大值即可 代码 #include<iostream> #include<algorithm> #in ...

  2. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. HDU 2096 小明A+B --- 水题

    HDU 2096 /* HDU 2096 小明A+B --- 水题 */ #include <cstdio> int main() { #ifdef _LOCAL freopen(&quo ...

  5. [HDU 2602]Bone Collector ( 0-1背包水题 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...

  6. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  7. HDU 5590 ZYB's Biology 水题

    ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  8. HDU 5832 A water problem (带坑水题)

    A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  9. HDU 5538 L - House Building 水题

    L - House Building Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

随机推荐

  1. 检测浏览器对HTML5和CSS3支持情况的利器——Modernizr

    Modernizr是什么? Modernizr 是一个用来检测浏览器功能支持情况的 JavaScript 库. 目前,通过检验浏览器对一系列测试的处理情况,Modernizr 可以检测18项 CSS3 ...

  2. node.js 1Million concurrent connections!

    https://github.com/ashtuchkin/node-millenium http://blog.caustik.com/2012/08/19/node-js-w1m-concurre ...

  3. iOS runloop 资源汇总-b

    RunLoop 是 iOS 和 OSX 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何利 ...

  4. MXNet在64位Win7下的编译安装

    注:本文原创,作者:Noah Zhang  (http://www.cnblogs.com/noahzn/) 我笔记本配置比较低,想装个轻量级的MXNet试试,装完之后报错,不是有效的应用程序,找不到 ...

  5. Seven Python Tools All Data Scientists Should Know How to Use

    Seven Python Tools All Data Scientists Should Know How to Use If you’re an aspiring data scientist, ...

  6. MongoDB开发应用实战

    http://special.csdn.net/mongodb/ http://www.csdn.net/article/2011-03-21/294271 http://blog.itpub.net ...

  7. 网上测试了很多关于PYTHON的WEBSOCKET样例,下面这个才成功了

    这是最底层的, 嘿嘿,我 还是习惯搞个框架来实现急需要的功能... 这个东东玩得很有意思的.. 服务器端的代码: import simplejson import socket import sys ...

  8. 练习PYTHON之EPOLL

    哟,哟,哟,,SELECT,EPOLL之类的,终于出现了. 不能太急了,要缓一缓,缓一缓,再缓一缓~~~~~~~~~ http://scotdoyle.com/python-epoll-howto.h ...

  9. jsp接收相同复合参数 request.getParameterValues()用法

    在网站中往往需要用户选择复选框,此时需要创建多个复选框让用户进行选择: <head> <meta http-equiv="Content-Type" conten ...

  10. spring声明式事务 同一类内方法调用事务失效

    只要避开Spring目前的AOP实现上的限制,要么都声明要事务,要么分开成两个类,要么直接在方法里使用编程式事务 [问题] Spring的声明式事务,我想就不用多介绍了吧,一句话“自从用了Spring ...