spoj453 Sums in a Triangle (tutorial) 动态规划

来源:互联网 发布:少女前线g36c数据 编辑:程序博客网 时间:2024/05/21 10:50

题意:给出一个三角矩形,求从顶部到最底部的最大值 

思路:动态规划,用 dp(i,j)表示从(0,0)到(i,j)的最大值,有dp(i,j)= max(dp(i-1,j-1) , dp(i-1,j)+matrix[i][j]

代码如下:

179129892016-10-12 07:11:35Sums in a Triangle (tutorial)acceptededit    ideone it0.173.4MC++14

#include <iostream>#include <fstream>#include <cstring>#include <algorithm>using namespace std;const int ROW = 101;class Solution{public:    void run()    {        int cas;        cin >> cas;        while (cas--)        {            input();            solve();        }    }private:    void input()    {        cin >> row;        for (int i = 0; i < row; i++)        {            for (int j = 0; j <= i; j++)            {                cin >> matrix[i][j];            }        }    }    void solve()    {        dp[0][0] = matrix[0][0];        for (int i = 1; i < row; i++)        {            for (int j = 0; j <= i; j++)            {                if (j == 0)                {                    dp[i][j] = dp[i - 1][j] + matrix[i][j];                }                else if (j == i)                {                    dp[i][j] = dp[i - 1][j - 1] + matrix[i][j];                }                else                {                    dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + matrix[i][j];                }            }        }        int ans = -1;        for (int i = 0; i < row; i++)        {            ans = max(ans, dp[row - 1][i]);        }        cout << ans << endl;    }    void output()    {        for (int i = 0; i < row; i++)        {            for (int j = 0; j <= i; j++)            {                cout << matrix[i][j] << " ";            }            cout << endl;        }        cout << endl;    }private:    int matrix[ROW][ROW];    int dp[ROW][ROW];    int row;};int main(){    streambuf* old;#ifndef ONLINE_JUDGE    ifstream fin("E:\\program\\clion\\example\\spoj.txt");    old = cin.rdbuf(fin.rdbuf());#endif    Solution solver;    solver.run();#ifndef ONLINE_JUDGE    cin.rdbuf(old);#endif    return 0;}

用java做竟然耗时还短些

179150212016-10-12 12:12:57Sums in a Triangle (tutorial)acceptededit    ideone it0.14695MJAVA

import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.PrintWriter;import java.io.IOException;import java.io.StreamTokenizer;public class Main{    private StreamTokenizer cin;    private PrintWriter cout;    private static final boolean DEBUG = true;    private int[][] matrix;    private int row;    private void init()    {        try        {            if (DEBUG)            {                cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(new FileInputStream("e:\\program\\idea\\spoj\\spoj.txt"))));            }            else            {                cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));            }            cout = new PrintWriter(System.out);        }        catch (IOException e)        {            e.printStackTrace();;        }    }    private Integer nextInt()    {        try        {            cin.nextToken();            if (cin.ttype == StreamTokenizer.TT_EOF) return null;            else if (cin.ttype == StreamTokenizer.TT_NUMBER) return (int)(cin.nval);            else return null;        }        catch(Exception e)        {            e.printStackTrace();            return null;        }    }    private void input()    {        row = nextInt();        matrix = new int[row][row];        for (int i = 0; i < row; i++)        {            for (int j = 0; j <= i; j++)            {                matrix[i][j] = nextInt();            }        }    }    private void solve()    {        int[][] dp = new int[row][row];        dp[0][0] = matrix[0][0];        for (int i = 1; i < row; i++)        {            for (int j = 0; j <= i; j++)            {                if (j == 0)                {                    dp[i][j] = dp[i - 1][j]  + matrix[i][j];                }                else if (j == i)                {                    dp[i][j] = dp[i - 1][j - 1] + matrix[i][j];                }                else                {                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - 1]) + matrix[i][j];                }            }        }        int ans = -1;        for (int i = 0; i < row; i++)        {            ans = Math.max(ans, dp[row - 1][i]);        }        cout.println(ans);        cout.flush();    }    public void run()    {        init();        int t = nextInt();        while (t-- > 0)        {            input();            solve();        }    }    public static void main(String[] args)    {// write your code here        new Main().run();    }}



0 0