测试输入简介
1//测试数量
3//武器数量
10//初始金币
A//武器名 2//可升级次数 10//第一次提升战斗力 1//第一次消耗金币 20//第二次提升战斗力 10//第二次消耗金币
B 3 1 5 2 3 10 1
C 2 1 9 20 1
代码(其实可以动态规划,原谅我的懒惰吧)
package test1;
import java.util.*;
public class Main {
public static class Record {
int value;
int level[];
public Record() {
}
public Record(int value, int[] level) {
this.value = value;
this.level = level;
}
int printLevel() {
System.out.println(this.hashCode());
for (int i = 0; i < level.length; ++i) {
System.out.print(level[i] + " ");
}
System.out.println();
return 0;
}
}
static Record solve(int n, int c, int levelArray[], ArrayList w[], ArrayList v[]) {
if (n == -1 || c <= 0) {
return new Record(0, new int[levelArray.length]);
}
for (int i = n; i >= 0; --i) {
Record temp1 = solve(n - 1, c, levelArray, w, v);
Record temp2 = new Record(0, new int[levelArray.length]);
int maxLevel = levelArray[i];
int count = c;
int a[] = levelArray.clone();
int pd = 0;
while (--maxLevel >= 0) {
a[i] = maxLevel;
count = count - (int) w[i].get(a[i]);
if(count < 0){
break;
}
Record tempA = solve(n - 1, count, a, w, v);
pd += (int) v[i].get(a[i]);
System.out.println(pd);
temp2.value = tempA.value + pd;
int levelT = temp2.level[i]+1;
temp2.level = tempA.level;
temp2.level[i] += levelT;
}
return (temp1.value > temp2.value ? temp1 : temp2);
}
return new Record(0, new int[levelArray.length]);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; ++t) {
int itemNum = in.nextInt();
int gold = in.nextInt();
String name[] = new String[itemNum];
int levelUP[] = new int[itemNum];
ArrayList powerUP[] = new ArrayList[itemNum];
ArrayList cost[] = new ArrayList[itemNum];
int maxLevel = 0;
for (int item = 0; item < itemNum; ++item) {
name[item] = in.next();
levelUP[item] = in.nextInt();
maxLevel = (maxLevel < levelUP[item]) ? levelUP[item] : maxLevel;
powerUP[item] = new ArrayList<>(levelUP[item]);
cost[item] = new ArrayList<>(levelUP[item]);
for (int l = 0; l < levelUP[item]; ++l) {
powerUP[item].add(0, in.nextInt());
cost[item].add(0,in.nextInt());
}
}
Record r = solve(itemNum - 1, gold, levelUP, cost, powerUP);
System.out.println((r.value));
for(int item = 0; item < itemNum; ++item){
System.out.println(name[item]+"+"+r.level[item]);
}
}
}
}