【SGU】SGU每日练1·Little shop of flowers【DP】

题目大意:

给你n*m的矩形(m >= n)

每个节点mp[i][j]有一个权值,从第一行走到最后一行,每一行只准选择一个数且对于i行,所选数的列数要严格大于i-1行选择的列数

问你最大权值是多少,并输出选择的n个列数

思路:

DP方程非常好想:DP[i][j] = max(DP[i][j - 1], DP[i - 1][j - 1] + mp[i][j]);

找路径的话,可以每行可以从从i+1到m,也可以直接从i - 1开始找

也可以在DP里面做标记,状态转移的时候将此点记录!

但是不能想的太无脑,你懂的

附上代码;

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#pragma warning(disable:4996)

#define Zero(a) memset(a, 0, sizeof(a))
#define Neg(a)  memset(a, -1, sizeof(a))
#define All(a) a.begin(), a.end()
#define PB push_back
#define repf(i,a,b) for(int i = a;i <= b; i++)
#define repff(i,a,b) for(int i = a; i < b; ++i)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,n,1
#define ld rt << 1
#define rd rt << 1 | 1
#define ll long long
#define MAXN 105
#define INF 0x3f3f3f3f
#define mod 10007
using namespace std;
vector<int>go;
int n, m, dp[MAXN][MAXN], gg[MAXN][MAXN], mp[MAXN][MAXN];
void init(){
    memset(gg, 0, sizeof(gg));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j){
            scanf("%d", &mp[i][j]);
            if(i != 0)dp[i][j] = -INF;
            else dp[i][j] = 0;
        }
    for (int i = 0; i <= n; ++i)
        for (int j = 0; j <= m; ++j){
            if (i != 0)dp[i][j] = -INF;
            else dp[i][j] = 0;
        }
        go.clear();
}

int main(){
    while (~scanf("%d%d", &n, &m)){
        init();
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j){
                if (dp[i - 1][j - 1] + mp[i][j] > dp[i][j - 1]){
                    gg[i][j] = 1;
                    //cout << "go" << endl;
                    dp[i][j] = dp[i - 1][j - 1] + mp[i][j];
                }
                else{
                    dp[i][j] = dp[i][j - 1];
                }
            }
        printf("%d\n", dp[n][m]);
        int nowx = n,nowy = m;
        while (nowx && nowy){
            if (gg[nowx][nowy] == 1){
                go.push_back(nowy);
                nowx--;
                nowy--;
            }
            else{
                nowy--;
            }
        }
        int len = go.size();
        for (int i = len - 1; i >= 0; --i){
            if (i != len - 1) printf(" ");
            printf("%d", go[i]);
        }
        puts("");
    }
    return 0;
}
时间: 2024-10-14 23:18:17

【SGU】SGU每日练1·Little shop of flowers【DP】的相关文章

POJ1157 LITTLE SHOP OF FLOWERS DP

题目 http://poj.org/problem?id=1157 题目大意 有f个花,k个瓶子,每一个花放每一个瓶子都有一个特定的美学值,问美学值最大是多少.注意,i号花不能出如今某大于i号花后面.问最大美学值是多少 解题思路 dp[i][j]表示将第i个花插入第k个瓶子的最大美学值. 状态转移方程为dp[i][j] = max(dp[i-1][(i-1)~(k-f+i-1)]) + value[i][j] 代码 #include <cstdio> const int maxn = 110;

sgu 104 Little Shop of Flowers

经典dp问题,花店橱窗布置,不再多说,上代码 #include <cstdio> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #define N 150 #define inf 0x7f7f7f7f using namespace std; int n, m; int val[N][N], f[N][N]; int fa[N][N];

POJ 题目1157 LITTLE SHOP OF FLOWERS(DP)

LITTLE SHOP OF FLOWERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19457   Accepted: 8966 Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different

【DP】POJ-1157 LITTLE SHOP OF FLOWERS

LITTLE SHOP OF FLOWERS Time Limit: 1000MS Memory Limit: 10000K Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in

POJ1157——LITTLE SHOP OF FLOWERS

LITTLE SHOP OF FLOWERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18481   Accepted: 8512 Description You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different

POJ 1157 LITTLE SHOP OF FLOWERS (线性dp)

OJ题目:click here~~ 题目分析:f个束花,编号为1-- f.v个花瓶,编号为1 -- v.编号小的花束,所选花瓶的编号也必须比编号大的花束所选花瓶的编号小,即花i 选k, 花j选t ,如果i < j ,则定有k < t . 如果 i > j , 则定有 k > t . 每束花放在每个花瓶里有一个值.求f束花,能得到的最大值. 设dp[ i ][ j ] 为第 i 束花选择了第 j 个花瓶 , 则转移方程为 dp[ i ][ j ] =  max(dp[ i  - 1]

【SGU】SGU每日练1&#183;Coprimes【数论】欧拉函数

先介绍欧拉函数及其相关定理吧: 1.欧拉函数:对于任意一个数X,将其分解质因数为X=(P1^b1)*(P2^b2)*(P3^b3)...... 则小于X的与X互质的数的个数N为N = X * (1 - 1 / p1) * (1 - 1 / p2)....... 显然对于质数p,Euler(p) = p - 1: 2.一个数的所有质因子之和是euler(n)*n/2: 3.a^Euler(n) % n = 1,这里可以用模运算来证明: 具体实现: 1.先筛素数 2.从2开始遍历素数,能整除则乘,然

Little shop of flowers - SGU 104 (DP)

题目大意:把 M 朵花插入 N 个花瓶中,每个花插入不同的花瓶都有一个价值A[Mi][Nj],要使所有的花都插入花瓶,求出来最大的总价值(花瓶为空时价值是0). 分析:dp[i][j]表示前i朵花插入前j个花瓶的最大价值,那么比较容易看出 dp[i][j] = max(dp[i][j-1], dp[i][j-1]+A[i][j]),也就是这个花瓶要还是不要,别忘记输出路径. 代码如下: =======================================================

[POJ1157]LITTLE SHOP OF FLOWERS

试题描述 You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutiv