题干:

链接:https://ac.nowcoder.com/acm/contest/886/J?&headNav=acm&headNav=acm&headNav=acm&headNav=acm
来源:牛客网

Rowlet is playing a very popular game in the pokemon world. Recently, he has encountered a problem and wants to ask for your help.

In this game, there is a technology tree system. There are n kinds of technology in this game, each of them has m levels numbered from 1 to m. In the beginning, all technologies have no level (regard as level 0). When the i-th technology is at the (j - 1)-th level, the player can pay cijc_{i j}cij​ pokedollars (currency used in this game) to upgrade this technology into the j-th level. However, sometimes upgrading is so easy that the cost might be negative, which implies the player may gain profit from upgrading technologies.

Moreover, if all technologies have been upgraded to level j, the player will gain an additional profit of djd_{j}dj​ pokedollars. However, sometimes too many technologies of the same level might be confusing, hence the profit can be negative as well.

Rowlet wants to determine the optimal strategy that can bring him the most pokedollars. Help him to find the maximum gain. Note that Rowlet may upgrade nothing, and in that case, the profit is zero.

输入描述:

There are multiple test cases. The first line contains an integer T (1≤T≤101 \leq T \leq 101≤T≤10), indicating the number of test cases. Test cases are given in the following.For each test case, the first line contains two integers n, m (1≤n,m≤10001 \leq n, m \leq 10001≤n,m≤1000), representing the number of technologies and the number of levels respectively.The i-th of the next n lines contains m integers, where the j-th number is cijc_{i j}cij​ (−109≤cij≤109-10^{9} \leq c_{i j} \leq 10^{9}−109≤cij​≤109).The last line contains m integers, where the j-th number is djd_{j}dj​ (−109≤dj≤109-10^{9} \leq d_{j} \leq 10^{9}−109≤dj​≤109).We ensure that the sum of n⋅mn \cdot mn⋅m in all test cases is at most 2×1062 \times 10^{6}2×106.

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1, and y denotes the answer(in pokedollars) to this test case.

示例1

输入

复制

2
2 2
1 2
2 -1
4 1
3 3
1 2 3
1 2 3
1 2 3
6 7 8

输出

复制

Case #1: 2
Case #2: 4

说明

 

In the first example, Rowlet can upgrade the first technology to level 1 and the second technology to level 2, which costs 1 + 2 - 1 = 2 pokedollars, but Rowlet can get 4 pokedollars as the bonus of upgrading all technologies to level 1, so the answer is 4 - 2 = 2 pokedollars.

In the second example, Rowlet can upgrade all technologies to level 2, which costs 1×3+2×3=91\times3 + 2\times3=91×3+2×3=9 pokedollars, but Rowlet can get 6 pokedollars as the bonus of upgrading all technologies to level 1 and 7 pokedollars as the bonus of upgrading all technologies to level 2, so the answer is 6 + 7 - 9 = 4 pokedollars.

题目大意:

就是给你n个技能,每个技能最高升到m级,只能从下往上连续的点技能(n*m矩阵)每升一级就是耗费Cij钱,这个Cij可能是负的,如果所有技能都升到或者说超过j等级,就会获得Dj钱,这个Dj也有可能是负值,让你求你最多得到多少钱(技能没有固定说要升到多少级,你也可以不升,这样就获得了0)

解题报告:

直接枚举最小等级的做法我们就不介绍了哈,比较简单,,而且其实不用线段树求后缀最小值的。直接可以预处理出来后缀最小值,然后直接取就可以,时间复杂度是O(n*m)的。

这里说另外一种dp方式:

dp[i][j]代表前i个技能升级到最小等级是j 的最大收益。根据这个最小等级j是否来自第i个技能,显然有转移方程:

dp[i][j]=max(①,②)

①dp[i-1][j]+第i行的max[j,m]              ②前i-1行的max[j,m] + 第i行的sum[j]。

当然如果这样做的话,需要再用dp预处理一个东西dd[][]数组,dd[i][j]代表前i个物品只能取后j种等级,且每种科技中只能取一个值的最小收益(但因为给定的值的含义是花费,所以可以认为是给定数据的最大值)。

然后几个细节注意一下就好了。比如ans不能在dp的过程中取(这是个废话,,麻瓜错误)。再比如 i==1 的时候要跳过②那种情况。其实活着直接i==1的时候特殊处理一下就行。再比如suf[][m+1]必须要初始化,就跟前缀和数组一样只不过平时sum[0]=0了所以没管。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e3 + 5;
int n,m;
ll a[MAX][MAX],suf[MAX][MAX],d[MAX],sum[MAX][MAX];
ll dd[MAX][MAX];
ll dp[MAX][MAX];//dp[i][j]代表前i个树,最小等级是j的最大收益。
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) scanf("%lld",&a[i][j]),suf[i][j] = 0,sum[i][j] = sum[i][j-1] + a[i][j];}for(int i = 1; i<=n; i++) {ll tmp = 0;suf[i][m+1] = 0;for(int j = m; j>=1; j--) tmp += a[i][j],suf[i][j] = max(suf[i][j+1],tmp);}ll ans = 0;for(int i = 1; i<=m; i++) scanf("%lld",d+i),d[i] += d[i-1];for(int i = 1; i<=n; i++) {for(int j = 1; j<=m+1; j++) {dd[i][j] = dd[i-1][j] + sum[i][m] - suf[i][j];}}for(int i = 1; i<=n; i++) {for(int j = 0; j<=m; j++) {//别忘考虑j==0的情况//如果第i个是j等级的话,那前i-1个可以随便取ll tmp = -sum[i][j] + d[j];tmp -= dd[i-1][j+1];//for(int k = 1; k<=i-1; k++) tmp -= sum[k][m] - suf[k][j+1];dp[i][j] = tmp;if(i == 1) continue;dp[i][j] = max(tmp,dp[i-1][j] - (sum[i][m] - suf[i][j+1]));
//              ans = max(ans,dp[i][j]);}}for(int i = 0; i<=m; i++) ans = max(ans,dp[n][i]);printf("Case #%d: %lld\n",++iCase,ans);}return 0 ;
}
/*
1
2 2
-1 -1
-1 -1
-4 -4
*/

【2019牛客暑期多校训练营(第六场)- J】Upgrading Technology(dp)相关推荐

  1. 2019牛客暑期多校训练营(第一场)E-ABBA(dp)

    链接:https://ac.nowcoder.com/acm/contest/881/E 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  2. 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...

  3. 2019牛客暑期多校训练营(第一场)

    传送门 参考资料: [1]:官方题解(提取码:t050 ) [2]:标程(提取码:rvxr ) [3]:牛客题解汇总 A.Equivalent Prefixes(单调栈) •题意 定义两个数组 u,v ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 【2019牛客暑期多校训练营(第二场)- E】MAZE(线段树优化dp,dp转矩阵乘法,线段树维护矩阵乘法)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/E?&headNav=acm 来源:牛客网 Given a maze with N rows an ...

  6. 【2019牛客暑期多校训练营(第二场)- F】Partition problem(dfs,均摊时间优化)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/F 来源:牛客网 Given 2N people, you need to assign each of ...

  7. 【2019牛客暑期多校训练营(第二场) - D】Kth Minimum Clique(bfs,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/D 来源:牛客网 Given a vertex-weighted graph with N vertice ...

  8. 【2019牛客暑期多校训练营(第一场) - A】Equivalent Prefixes(单调栈,tricks)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Two arrays u and v each with m distinct elem ...

  9. 【2019牛客暑期多校训练营(第一场) - H】XOR(线性基,期望的线性性)

    题干: 链接:https://ac.nowcoder.com/acm/contest/881/H 来源:牛客网 Bobo has a set A of n integers a1,a2,-,ana1, ...

  10. 2019牛客暑期多校训练营(第九场)H Cutting Bamboos(主席树+二分)

    链接:https://ac.nowcoder.com/acm/contest/889/H 来源:牛客网 题目描述 There are n bamboos arranged in a line. The ...

最新文章

  1. 交换机SPAN功能配置
  2. 【大数据分析常用算法】1.二次排序
  3. 数字孪生城市应用【案例集】,附下载
  4. 马斯克很着急:加速打造“月球电梯”,他要从NASA手中抢到这一单
  5. U盘中的autorun.inf
  6. sql 统计用的sql
  7. Spring事务操作-事务引入
  8. 多名员工拉横幅讨薪却跑错地方?暴风集团回应...
  9. 【转】fatal error C1010: unexpected end of file解决方案
  10. Java后端学习路线图,你真的只需要这一张!
  11. 对于代理服务器的理解
  12. python调用arcpy函数_AGS Python开发-ArcPy开发基础
  13. 三星帝国的风险:四大业务同荣同损
  14. 34个非常实用的JS一行代码
  15. java初中学历_20岁学java初中学历
  16. kubeadmin安装高可用k8s集群
  17. win10计算机搜索记录怎么删除,Win10系统
  18. forEach,$.each()以及$().each()的比较
  19. 磁盘阵列怎么组linux系统,Linux系统下如何设置磁盘阵列?
  20. Android 淡出效果手写板

热门文章

  1. eclipse快捷键Alt + /
  2. 承博士:让云计算落地生根的中国云计算平台
  3. 解读设计模式----命令模式(Command Pattern)
  4. 数据库调优都涉及哪些方面
  5. Java集合容器面试题
  6. [Markdown语法][快速入门][CSDN]
  7. elementui图片上传php,vue+element-ui+富文本————图片上传
  8. nodejs cluster ip hash_redis集群架构了解一下?一致性hash了解吗?
  9. layui 鼠标移入变为小手_游戏技术上不去?看看外设选对没!鼠标篇
  10. 一个网络资深者发起的思考