浪(吃)了一天,水道题冷静冷静....


题目链接:

http://acm.sgu.ru/problem.php?contest=0&problem=104

题意:

给定每朵花放在每个花盆的值,编号大的花只能放在编号小的花的后面,每朵花都要放到花盆里,问如何放才能使得总值最大?

分析:

还是一道比较水的dp...

每个位置有两种情况:放与不放。

不放的话\(dp[i][j-1][0]\)就等于前面最近的放了花盆的值。

放的话,就更新\(dp[i][j][1]\)。

这样保证对于每个花盆\(i\)花\(j\),\(max(dp[i - 1][j - 1][0] ,dp[i - 1][j - 1][1])\)表示放置第\(j-1\)号花后得到的最大的值。

最后逆推一遍,获得路径即可。

代码:

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
const int maxn = 100 + 5;
int dp[maxn][maxn][2];
int v[maxn][maxn];
stack<int>s;
int main (void)
{
int n, m;
scanf("%d%d", &m, &n);
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
scanf("%d", &v[i][j]);
}
}
memset(dp, -0x3f, sizeof(dp));
dp[0][0][0] = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
dp[i][j - 1][0] = max(dp[i - 1][j - 1][0] ,dp[i - 1][j - 1][1]);
dp[i][j][1] = max(dp[i - 1][j - 1][0], dp[i - 1][j - 1][1]) + v[j][i];
}
}
int ans = max(dp[n][m][1], dp[n][m][0]);
printf("%d\n", ans);
int sta = n;
for(int i = m; i >= 0; i--){
for(int j = sta; j >= 0; j--){
if(dp[j][i][1] == ans && ans){
s.push(j);
sta = j - 1;
ans -= v[i][j];
break;
}
}
}
while(!s.empty()){
int res =s.top();
s.pop();
printf("%d ", res);
}
printf("\n");
return 0;
}

SGU 104 Little shop of flowers【DP】的更多相关文章

  1. SGU 104. Little shop of flowers (DP)

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...

  2. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  3. 动态规划(方案还原):SGU 104 Little shop of flowers

    花店橱窗布置问题 时间限制:3000 ms 问题描述(Problem)    假设你想以最美观的方式布置花店的橱窗,你有F束花,每束花的品种都不一样,同时,你至少有同样数量的花瓶,被按顺序摆成一行.花 ...

  4. sgu 104 Little Shop of Flowers

    经典dp问题,花店橱窗布置,不再多说,上代码 #include <cstdio> #include <cstring> #include <iostream> #i ...

  5. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  6. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  8. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  9. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

随机推荐

  1. 云原生技术图谱 (CNCF Landscape)

    转自:https://raw.githubusercontent.com/cncf/landscape/master/landscape/CloudNativeLandscape_latest.jpg

  2. zuul 整理

    网关: 为了解决ip+端口的不友好性而产生.具有服务代理的功能nginx 功能: 1.验证与安全保障: 识别面向各类资源的验证要求并拒绝那些与要求不符的请求. 2.审查与监控: 在边缘位置追踪有意义数 ...

  3. 富通天下(W 笔试)

    纸质算法题目 1.给你一个字符串,找出其中第一个只出现过一次的字符及其位置 正解:一层for循环,循环按序取出字符串中的单个字符,循环体内部使用String类的indexOf(),从当前字符下标往后搜 ...

  4. @click.native 会触发原生 click事件 vue

    @click.native 会触发原生 click事件 vue

  5. 【前端】pid2children iview tree json

    <script> import inBody from '../inBody' export default { components:{ inBody } ,data () { retu ...

  6. es 集群部署

    下载 [root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1 ...

  7. Vue项目结构梳理

    Vue项目结构图: 简单介绍目录结构 build目录是一些webpack的文件,配置参数什么的,一般不用动 config是vue项目的基本配置文件 node_modules是项目中安装的依赖模块 sr ...

  8. Java ArrayList中去掉相同的元素并保留相同元素中的最后一个

    实现思路:将list对象循环两次,拿外层数据和里面的数据对比,一样的删除外层(外层元素肯定比内存的靠前),如果一样的话,删除外层数据,这样最后输出外层数据的list,结果就能保证唯一性,并且保留了后面 ...

  9. 前段开发 react native tab功能

    import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, } from 'react-nati ...

  10. Oracle批量更新数据,使用begin end

    /* 使用begin end批量更新 注意end后面必须使用;结束 并且每条update语句都要用;来结束 所以close为;END; 是为了补全语法 */ <foreach collectio ...