列表

详情


NC52009. 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 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 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 (), 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 (), 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 ().

The last line contains m integers, where the j-th number is ().

We ensure that the sum of  in all test cases is at most .

输出描述

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\times3 + 2\times3=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.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 1580ms, 内存消耗: 20364K, 提交时间: 2019-08-04 11:07:56

#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;
}

C++11(clang++ 3.9) 解法, 执行用时: 498ms, 内存消耗: 16204K, 提交时间: 2019-08-03 20:43:24

#include<bits/stdc++.h>
#define maxn 1005
#define LL long long
using namespace std;
int T,n,m,x;
LL ans,sum,D,s[maxn][maxn],Min[maxn][maxn];
int main() {
	scanf("%d",&T);
	for(int _=1; _<=T; _++) {
		scanf("%d %d",&n,&m);
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++)
				scanf("%d",&x),s[i][j]=s[i][j-1]+x;
			Min[i][m+1]=1ll<<60;
			for(int j=m; j>=0; j--) Min[i][j]=min(Min[i][j+1],s[i][j]);
		}
		ans=D=0;
		for(int j=0; j<=m; j++) {
			if(j) scanf("%d",&x),D+=x;
			sum=1ll<<60;
			for(int i=1; i<=n; i++) if(s[i][j]-Min[i][j]<sum) sum=s[i][j]-Min[i][j];
			for(int i=1; i<=n; i++) sum+=Min[i][j];
			ans=max(ans,D - sum);
		}
		printf("Case #%d: %lld\n",_,ans);
	}
}

上一题