A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows.
Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter tij units of time.
Order is important everywhere, so the painters' work is ordered by the following rules:
- Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n);
- each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on;
- each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest;
- as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter.
Given that the painters start working at time 0, find for each picture the time when it is ready for sale.
The first line of the input contains integers m, n (1 ≤ m ≤ 50000, 1 ≤ n ≤ 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers ti1, ti2, ..., tin (1 ≤ tij ≤ 1000), wheretij is the time the j-th painter needs to work on the i-th picture.
Print the sequence of m integers r1, r2, ..., rm, where ri is the moment when the n-th painter stopped working on the i-th picture.
5 1 1 2 3 4 5
1 3 6 10 15
4 2 2 5 3 1 5 3 10 1
7 8 13 21
题目大意:
有n个画家,且有m幅待完成的画, 每幅画必须按画家顺序来画(即必须先让第一个画家来画某部分,才能让第二个画家来接着画),画家必须按照画的顺序来画(即从1号画开始,不能跳跃)
已知每个画家完成第i幅画的某部分需要的时间,求完成每幅画需要的时间.
解法:
模拟题.(官方说是dp)
ans[j][i] 表示第j副画在第i个画家完成时所需要的时间.
ans[j][i] = max(ans[j][i-1], ans[j-1][i]) + t[j][i]; //其中t[j][i]表示第j幅画让第i个画家来画需要的时间
ans[j][i]必须满足,当前画家有空(ans[j-1][i]),且前面的画家已经画完(ans[j][i-1]);
代码
#include <cstdio>
#define maxm 50010
#define maxn 10
int n, m;
int t[maxm][maxn], ans[maxm][maxn];
int max(int a, int b) {
if (a > b) return(a);
return(b);
}
void init() {
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &t[i][j]);
}
void solve() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans[j][i] = max(ans[j][i-1], ans[j-1][i]) + t[j][i];
for (int i = 1; i <= m; i++) printf("%d ", ans[i][n]);
}
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
init();
solve();
}