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]),也就是这个花瓶要还是不要,别忘记输出路径。
代码如下:
========================================================================================================================================
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = ;
const int oo = 1e9+; int dp[MAXN][MAXN];
int A[MAXN][MAXN]; void Path(int x, int y, int M)
{
if(x == )
return ; if(dp[x][y] == dp[x][y-])
Path(x, y-, M);
else
{
Path(x-, y-, M);
printf("%d%c", y, x==M ? '\n':' ');
}
} int main()
{
int M, N;///M朵花,N个花瓶 scanf("%d%d", &M, &N); for(int i=; i<=M; i++)
for(int j=; j<=N; j++)
scanf("%d", &A[i][j]); for(int i=; i<=M; i++)
dp[i][] = -oo; for(int i=; i<=M; i++)
for(int j=; j<=N; j++)
{
dp[i][j] = max(dp[i][j-], dp[i-][j-]+A[i][j]);
} printf("%d\n", dp[M][N]); Path(M, N, M); return ;
}
Little shop of flowers - SGU 104 (DP)的更多相关文章
- 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 ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- 快速切题 sgu104. Little shop of flowers DP 难度:0
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...
- SGU 104
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB PROBLEM Yo ...
- POJ-1157 LITTLE SHOP OF FLOWERS(动态规划)
LITTLE SHOP OF FLOWERS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19877 Accepted: 91 ...
- [POJ1157]LITTLE SHOP OF FLOWERS
[POJ1157]LITTLE SHOP OF FLOWERS 试题描述 You want to arrange the window of your flower shop in a most pl ...
- SGU 104 Little shop of flowers【DP】
浪(吃)了一天,水道题冷静冷静.... 题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=104 题意: 给定每朵花放在每个花盆的值, ...
- 动态规划(方案还原):SGU 104 Little shop of flowers
花店橱窗布置问题 时间限制:3000 ms 问题描述(Problem) 假设你想以最美观的方式布置花店的橱窗,你有F束花,每束花的品种都不一样,同时,你至少有同样数量的花瓶,被按顺序摆成一行.花 ...
- 【SGU 104】Little shop of flowers
题意 每个花按序号顺序放到窗口,不同窗口可有不同观赏值,所有花都要放上去,求最大观赏值和花的位置. 分析 dp,dp[i][j]表示前i朵花最后一朵在j位置的最大总观赏值. dp[i][j]=max( ...
随机推荐
- IOS pop使用代理传值
假如oneViewController页面push到OtherViewController页面,然后你想从OtherViewController页面pop到oneViewController页面的时候 ...
- Azure cache 的配置与应用
最近公司的项目要是用cloud Service 所以研究了下 Azure cache 的配置与使用. 首先创建项目 第二步 配置 cache worker role (1) 点击 cache work ...
- Codeforces 551E - GukiZ and GukiZiana(分块)
Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...
- ul ol dl
1.ul是无序列表,也就是说没有排列限制可以随意加li: <ul> <li>可以随意放置</li> <li>可以随意放置</li> < ...
- 看es6 字符串新方法有感
'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello" 'na'.repeat(0) // " ...
- JavaScript学习心得(一)
一Javascript简介 JavaScript是一种面向对象.弱类型的脚本语言!面向对象编程语言(OOP)意味着你用的几乎所有变量都是对象,对象是一种特殊的变量类型,有自己的子变量(称为属性)及函数 ...
- 移动端Web开发如何处理横竖屏
<!Doctype html> <html> <head> <meta charset="utf-8"> <meta id=& ...
- 机务UI设计小节
1.CSS样式 .header { background-color:#7A8692; color:White; height:30px; font-size:16px; width:100%; li ...
- activity学习(1) 生命周期理解
可以忽略onWindowFocusChanged.onSaveInstanceState.onRestoreInstanceState几个事件,这几个事件官网中的生命周期里面没有提到.忽略掉这几个方法 ...
- 非常好用的正则表达式"\\s+" - 匹配任意空白字符
说起来,博主使用过的正则场景虽然不多,但是就是在这当中,我发现"\\s+"真好用! 详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页 ...