英雄会 XX采药 挑战失败,由于程序运行总时间大于3s导致超时 java采药 动态规划 超时 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个采药就是01背包问题,我改成这样,还是超时 import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int timTotal = scanner.nextInt(); int hrbTotal = scanner.nextInt(); int[][] values = new int[hrbTotal + 1][timTotal + 1]; int lastHerb = hrbTotal; int lastTime = timTotal; int tmpValue1 = 0; int tmpValue2 = 0; int[] time = new int[hrbTotal + 1]; int[] value = new int[hrbTotal + 1]; time[0] = 0; value[0] = 0; int count = 1; while (count <= hrbTotal) { time[count] = scanner.nextInt(); value[count] = scanner.nextInt(); count++; } for (int i = 1; i <= lastHerb; i++) for (int j= 1; j <= lastTime; j++) { if (i == 1) values[0][j] = 0; tmpValue1 = values[i-1][j]; if (j >= time[i]) tmpValue2 = values[i-1][j-time[i]] + value[i]; else tmpValue2 = 0; values[i][j] = tmpValue1>=tmpValue2?tmpValue1:tmpValue2; } System.out.println(values[lastHerb][lastTime]); } }}只是改了几个名字。不知道哪里可以继续优化,还是什么其他问题。。 目前英雄会的OJ模式有问题,Java和C#的代码测试时永远读不到输入,然后就超时了,节哀 这位仁兄,这个消息你是从哪里得到的?官方的,还是自己测试过总结出来的?你把代码逻辑全部删掉,只留一句读取输入,你就会发现结果依然是超时,我向管理员反映过,已确认Java和C#存在这个问题 http://bbs.csdn.net/topics/390724211?page=1 这位仁兄,这个消息你是从哪里得到的?官方的,还是自己测试过总结出来的?你把代码逻辑全部删掉,只留一句读取输入,你就会发现结果依然是超时,我向管理员反映过,已确认Java和C#存在这个问题我试了一下,确实是如此 请帮我理清一个概念,求助 关于泛型编程? Eclipse3.3和JBuilder2007没有应用程序开发吗? java小问题 如何实现单击按钮弹出窗口 我init()方法中提取初始值为何总是为空? 如何在JAVA GUI上嵌入系统软件,如Word 如何传递通过Applet设置的参数 等待409 !· 请问java specification的问题 集群或者分布式,hibernate,如果处理一级缓存的同步或者脏数据问题。 关于实现多线程资源共享
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int timTotal = scanner.nextInt();
int hrbTotal = scanner.nextInt();
int[][] values = new int[hrbTotal + 1][timTotal + 1];
int lastHerb = hrbTotal;
int lastTime = timTotal; int tmpValue1 = 0;
int tmpValue2 = 0;
int[] time = new int[hrbTotal + 1];
int[] value = new int[hrbTotal + 1];
time[0] = 0;
value[0] = 0;
int count = 1; while (count <= hrbTotal) {
time[count] = scanner.nextInt();
value[count] = scanner.nextInt();
count++;
}
for (int i = 1; i <= lastHerb; i++)
for (int j= 1; j <= lastTime; j++) {
if (i == 1)
values[0][j] = 0;
tmpValue1 = values[i-1][j];
if (j >= time[i])
tmpValue2 = values[i-1][j-time[i]] + value[i];
else
tmpValue2 = 0;
values[i][j] = tmpValue1>=tmpValue2?tmpValue1:tmpValue2;
}
System.out.println(values[lastHerb][lastTime]); }
}
}只是改了几个名字。不知道哪里可以继续优化,还是什么其他问题。。
我试了一下,确实是如此