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

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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个科技,他们能有从1到m的等级,一开始等级都为0,当第i个科技从j-1级升到j级时,要花费cij(可能为正可能为负,花费为负就赚了),
除此之外,如果所有科技的等级都大于等于等级j,则可以得到dj的钱(可能为正可能为负,收益为负就亏了,同时能否获得dj取决于最小的那个科技的等级是否大于等于等级j,这个是本题的关键),
要求最大收益,注意如果所有科技都不升级,那么收益为0(可能一升级就亏钱,所以就不升啦~)

思路:

题目输入的代价是正的,利润是负的,建立cost前缀和,同时为了方便处理把代价以负数输入,利润以正数输入,记录升到i级的到的利润建立升到第j级总收益的前缀和,n个人都升到第j级的利润(题目是n个科技树,本文把科技统称为人),将n个人都升到第j级的总代价加入的到总收益中接着要枚举当前最小的等级为j,则相当于所有人都已经升到了j级,记录下还可以增加的收益,就是看每个人还可以在已经升到了j级的基础上还可以再自己升多少级(相当于自己偷偷学习),同时要j小于m才判断是否可以增加收益,因为如果j为m就升不了级了,枚举第i个人,升到第j级后是否还能向后升级到多少使得收益是最大的,区间问题,用st表或线段树维护下,但这题暴力枚举也是可以过的,最多只能有n-1个人的收益加入可增加收益,因为如果n个人都增加,那么他们中的最低等级就不是当前的j了,所以如果n个人都可以继续增加收益,则需要把收益最少的那个人的收益从总的可增加收益中减掉,这样才能获得最大合法的可增加收益最后选一个从0到m级中收益最大的作为答案

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e3+;
const ll inf=1e18;
ll in,c[amn][amn],d[amn],s[amn],cnt,nowmin,a[amn],ans,need,maxs,minn;
int main(){
int n,m,T;
ios::sync_with_stdio();
cin>>T;
for(int ca=;ca<=T;ca++){
memset(s,,sizeof s);
memset(a,,sizeof a);
cin>>n>>m;
for(int i=;i<=n;i++)c[i][]=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>in; ///题目输入的代价是正的,利润是负的
c[i][j]=c[i][j-]-in; ///建立cost前缀和,同时为了方便处理把代价以负数输入,利润以正数输入
}
}
for(int i=;i<=m;i++){
cin>>d[i]; ///升到i级的到的利润
}
d[]=s[]=;
for(int j=;j<=m;j++){
s[j]=s[j-]+d[j]; ///建立升到第j级总收益的前缀和,n个人都升到第j级的利润(题目是n个科技树,本文把科技统称为人)
for(int i=;i<=n;i++){
s[j]+=c[i][j]-c[i][j-]; ///n个人都升到第j级的总代价加入的到总收益中
}
}
ans=;
for(int j=;j<=m;j++){ ///枚举当前最小的等级为j,则相当于所有人都已经升到了j级
need=; ///还可以增加的收益,就是看每个人还可以在已经升到了j级的基础上还可以再自己升多少级(相当于自己偷偷学习)
if(j<m){ ///要j小于m才判断是否可以增加收益,因为如果j为m就升不了级了
cnt=;minn=inf;
for(int i=;i<=n;i++){
maxs=;
for(int k=j+;k<=m;k++){
if(maxs<c[i][k]-c[i][j]){
maxs=c[i][k]-c[i][j]; ///枚举第i个人,升到第j级后是否还能向后升级到多少使得收益是最大的,这里本来想用st表或线段树维护的,但当时觉得太麻烦万一思路错了就白浪费那么多时间,就先暴力交一发验证下思路,没想到竟然过了
}
}
if(maxs>){ ///如果最大收益是正的,则加入到可增加的收益中,同时记录加入的人数和所有加入收益的最小值
need+=maxs;
cnt++;
minn=min(minn,maxs);
}
}
if(cnt==n)need-=minn; ///最多只能有n-1个人的收益加入可增加收益,因为如果n个人都增加,那么他们中的最低等级就不是当前的j了,所以如果n个人都可以继续增加收益,则需要把收益最少的那个人的收益从总的可增加收益中减掉,这样才能获得最大合法的可增加收益
}
ans=max(s[j]+need,ans); ///选一个收益最大的作为答案
}
printf("Case #%d: %lld\n",ca,ans);
}
}
/***
有n个科技,他们能有从1到m的等级,一开始等级都为0,当第i个科技从j-1级升到j级时,要花费cij(可能为正可能为负,花费为负就赚了),
除此之外,如果所有科技的等级都大于等于等级j,则可以得到dj的钱(可能为正可能为负,收益为负就亏了,同时能否获得dj取决于最小的那个科技的等级是否大于等于等级j,这个是本题的关键),
要求最大收益,注意如果所有科技都不升级,那么收益为0(可能一升级就亏钱,所以就不升啦~)
题目输入的代价是正的,利润是负的,建立cost前缀和,同时为了方便处理把代价以负数输入,利润以正数输入,记录升到i级的到的利润
建立升到第j级总收益的前缀和,n个人都升到第j级的利润(题目是n个科技树,本文把科技统称为人),将n个人都升到第j级的总代价加入的到总收益中
接着要枚举当前最小的等级为j,则相当于所有人都已经升到了j级,记录下还可以增加的收益,就是看每个人还可以在已经升到了j级的基础上还可以再自己升多少级(相当于自己偷偷学习),同时要j小于m才判断是否可以增加收益,因为如果j为m就升不了级了,
枚举第i个人,升到第j级后是否还能向后升级到多少使得收益是最大的,区间问题,用st表或线段树维护下,但这题暴力枚举也是可以过的,最多只能有n-1个人的收益加入可增加收益,因为如果n个人都增加,那么他们中的最低等级就不是当前的j了,所以如果n个人都可以继续增加收益,则需要把收益最少的那个人的收益从总的可增加收益中减掉,这样才能获得最大合法的可增加收益
最后选一个从0到m级中收益最大的作为答案
***/

[暴力+前缀和]2019牛客暑期多校训练营(第六场)Upgrading Technology的更多相关文章

  1. 2019牛客暑期多校训练营(第六场)C - Palindrome Mouse (回文自动机)

    https://ac.nowcoder.com/acm/contest/886/C 题意: 给出一个串A , 集合S里面为A串的回文字串 , 现在在集合S里面找出多少对(a,b),b为a的字串 分析: ...

  2. 2019牛客暑期多校训练营(第六场)J Upgrading Technology

    传送门 题意: 就是给你n个技能,每个技能最高升到m级,每升一级就是耗费Cij钱,这个Cij可能是负的,如果所有技能都升到或者说超过j等级,就会获得Dj钱,这个Dj也有可能是负值,让你求你最多得到多少 ...

  3. 2019牛客暑期多校训练营(第二场)F.Partition problem

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

  4. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  5. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

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

  6. 2019牛客暑期多校训练营(第一场) B Integration (数学)

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

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

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

  8. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

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

  9. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

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

随机推荐

  1. Dangerous query method called with non-attribute argument(s)

    踩坑 query method. 问题描述 现有model issue,需要对issues进行排序,根据指定的ID集合来决定记录的位置,比如id包含在(4, 6, 9)中的纪录就排在前面,剩下的排在后 ...

  2. JNI 问题 wrong ELF class

    使用JNI发现一个问题, wrong ELF class: ELFCLASS64)主要是机器是64位的OS,默认编译的.so是64位 而java设置的默认是32位 JDK, 所以会出现这个问题.那么就 ...

  3. redis BLPOP

    一.需求 redis中保存了需要download的image url,存储格式为列表. 我需要从列表中获取数据,将图片下载保存到本地. 列表中的数据是一直增加的. 二.实现 使用redis BLPOP ...

  4. 学了C++不知道怎么搞后台开发?先看看这份学习路线吧!

    作者:AJ 在去年结束的秋季招聘中,后台开发或服务器开发的岗位需求一度火热,甚至超过了算法岗.不少同学从诸神黄昏的算法岗战场上退下,转向更偏向工程能力的后台开发岗,从而造成后台开发岗位竞争的大爆发. ...

  5. CSS(0)CSS的引入方式

    CSS (cascading  style  sheet)  层叠样式表 css引入的三种方式: 1.行间样式 <!--在body内写入--> <div></div> ...

  6. Ubutun18.04安装Python3.7.6

    最近因为环境问题,简单记录下Python3.7的安装过程: 下载地址:http://python.org/ftp/python/3.7.6/Python-3.7.6.tgz 编译安装步骤: sudo ...

  7. 记一次苹果APP从账号续费到发布成功的历程

    一.一波三折的续费      最近公司开发的苹果APP的SSL证书到期了,计划重新发布一下该APP,已替换即将到期的SSL证书.近几年随着钉钉.企业微信等在线办公软件超级平台的出现,各企业都会选择其中 ...

  8. 用 git 钩子,检测代码规范性(eslint、standard)

    最终实现效果说明:用 git commit 提交代码之前,利用 pre-commit git 钩子,实现代码规范检测(eslint.standard 规范),符合规范之后才可以提交到 git 仓库.这 ...

  9. 从String到==和hashcode

    public static void main(String[] args) { String s1 = "ni"; String s2 = "hao"; St ...

  10. CyclicBarrier源码探究 (JDK 1.8)

    CyclicBarrier也叫回环栅栏,能够实现让一组线程运行到栅栏处并阻塞,等到所有线程都到达栅栏时再一起执行的功能."回环"意味着CyclicBarrier可以多次重复使用,相 ...