2019牛客暑期多校训练营(第六场)J Upgrading Technology 【暴力+前缀和】

题目描述

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.

题目链接:

https://ac.nowcoder.com/acm/contest/886/J

题意:

有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级后是否还能向后升级到多少使得收益是最大的,区间问题,暴力枚举也是可以过的,最多只能有n-1个人的收益加入可增加收益,因为如果n个人都增加,那么他们中的最低等级就不是当前的j了,所以如果n个人都可以继续增加收益,则需要把收益最少的那个人的收益从总的可增加收益中减掉,这样才能获得最大合法的可增加收益最后选一个从0到m级中收益最大的作为答案

代码:

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(register int i=a;i<=b;i++)
typedef long long ll;
const ll mod=10010;
 
int a[1010][1010],b[1010];
int n,m;
ll s[1010][1010];
int main(){
    int T;
    cin>>T;
    rep(kase,1,T){
        cin>>n>>m;
        rep(i,1,n)rep(j,1,m){
            cin>>a[i][j];
            a[i][j]=-a[i][j];
        }
        rep(i,1,m)cin>>b[i];
        rep(i,1,n){
            s[i][m]=0;
            for(int j=m-1;j>=0;j--)
                s[i][j]=max(0ll,s[i][j+1]+a[i][j+1]);
        }
        ll ans=0,now=0;
        rep(j,0,m){
            ll cnt=0,minn=1e18;
            now+=b[j];
            rep(i,1,n){
                now+=a[i][j];
                cnt+=s[i][j];
                minn=min(minn,s[i][j]);
            }
            ans=max(ans,cnt-minn+now);
        }
        cout<<"Case #"<<kase<<": "<<ans<<endl;
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值