链接: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. zctf 2016 android writeup - Jieming的博客

    本文为2016年zctf中android的writeup. 首先点我下载题目.使用jeb反编译,对username和password进行部分验证后,再将username+password及一个数据库查 ...

  2. Python-控制语句及函数

    if-elif-else for while 函数 函数定义 空函数 pass 返回多个值 可变参数 * 关键字参数 ** 控制语句 if - elif - else 比如,输入用户年龄,根据年龄打印 ...

  3. gcc编译器常用选项的含义

    -w: 关闭编译时的警告, 也就是编译后不显示任何warning,因此有时编译中会出现一些诸如数据转换之类的可忽略警告, -Wall: 显示编译后所有警告 -W: 显示警告,但是只是显示编译器认为的会 ...

  4. 用hugo建博客的记录 · 老张不服老

    前后累计折腾近6个小时,总算把搭建hugo静态博客的整个过程搞清楚了.为什么用了这么久?主要还是想偷懒,不喜欢读英文说明.那就用中文记录一下过程吧.还是中文顺眼啊. 某日发现自己有展示些东西给外网的需 ...

  5. Go基础学习(二)

    数组[array] 数组定义[定义后长度不可变] 12 symbol := [...]string{USD: "$", EUR: "€", GBP: " ...

  6. 【基础篇】hexo博客搭建教程

    [基础篇]搭建hexo博客(一) 作者:Huanhao bilibili:Mrhuanhao 前言 你是否想拥有属于自己的博客?你是否无奈与自己不会写网站而烦恼? 不要担心,本系列教程将会实现你白嫖的 ...

  7. 脚本化处理linux云服务器第二硬盘初始化

    #!/usr/bin/bash # 可以带参数 method=$ size=$ mydir=$ [ $#== ]&&{ echo -e "Missing parameter! ...

  8. Pycharm2019.2激活至2089年

    PyCharm作为日常开发常用工具,过段时间就需要再次激活是个问题,今早找到个方法很实用,亲测也是有效的.激活成功如下图: 具体步骤如下: 1. 下载破解补丁和激活码[小哈学Java公众号提供],使用 ...

  9. 纯JS实现KeyboardNav(学习笔记)一

    纯JS实现KeyboardNav(学习笔记)一 这篇博客只是自己的学习笔记,供日后复习所用,没有经过精心排版,也没有按逻辑编写 GitHub项目源码 预览地址 最终效果 KeyboardNav使用指南 ...

  10. 学习使用Guava Cache

    官方文档:https://github.com/google/guava/wiki/CachesExplained 目录 一.guava cache介绍 二.快速入门 2.1.引入依赖 2.2.第一个 ...