有一个物品队列 \frak BB,初始时为空。现在共有三种操作。每个操作会给定三个整数 \mathrm{op},x,yop,x,y,其中 \mathrm{op}op 表示操作种类,x,yx,y 是操作的参数。操作分为如下三种:
对于操作 22 和 33,请忽略多余的参数。
本题强制在线。强制在线的方式请见输入格式。
输入 #1复制
10 10 1 3 4 1 5 5 3 4 1 3 12 5 1 14 3 3 1 8 2 11 11 3 2 11 2 8 8 3 12 8
输出 #1复制
4 9 10 9 4
解码后的输入数据为:
10 10
1 3 4
1 5 5
3 4 1
3 8 1
1 7 10
3 8 1
2 1 1
3 8 1
2 1 1
3 5 1
对于十次操作,物品序列的情况如下;
对于全部数据,1\le n\le 3\times 10^41≤n≤3×104,1\le m_{\max}\le 2\times 10^41≤mmax≤2×104,1\le x, y\le 2\times 10^41≤x,y≤2×104。
首先来个内存不通过的代码:使用暴力dfs
import java.util.*;import java.util.Scanner;public class Main {public static Integer n;public static Integer max;public static Integer x[];public static Integer y[];public static Integer op[];public static Integer zt[];public static Integer num = 0;public static Integer lastans = 0;public static List listx = new ArrayList<>();public static List listy = new ArrayList<>();public static void main(String[] args) {Scanner sca = new Scanner(System.in);n = sca.nextInt();max = sca.nextInt();x = new Integer[n];y = new Integer[n];op = new Integer[n];zt = new Integer[n];for(int i = 0;i1) {listx.remove(listx.size()-1);listy.remove(listy.size()-1);}}else if (op[i]==3) {
// 搜寻最大的价值dfs(x[i],0);System.out.println(num);lastans = num;num = 0;
// System.out.println(maxs);}// System.out.println(x[i]+","+y[i]+" :"+listx+" "+listy);}}
// 剩余当前背包mx 4 当前背包内价值my 第几个物品的索引index public static int dfs(Integer mx,Integer my) {if(listx.size()==0) {return 0;}for(int i = 0;i=listx.get(i)) {//这个包是否能装下该物品mx = mx-listx.get(i);//剩余容量my = my+listy.get(i);//当前价值if(my>num) {//存储最大的有效价值num = my;}zt[i]=1;
// 进行下一个搜索dfs(mx, my);mx = mx+listx.get(i);//剩余容量my = my-listy.get(i);//当前价值zt[i]=0;}else {zt[i] = 1;
// 进行下一个搜索dfs(mx, my);zt[i] = 0;}}}return 0;}}

对代码进行优化 ,不会了...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;import java.util.*;public class Main {public static List listx = new ArrayList<>(),listy = new ArrayList<>();//存放大小 价值public static int dp[][],dp1[],dp2[],m;public static void main(String[] args) throws IOException {BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));int lastans = 0;String s = reader.readLine();int n = Integer.parseInt(s.split(" ")[0]);//一共几行int max = Integer.parseInt(s.split(" ")[1]);//背包体积最大值int ma = 0;dp1 = new int[max+1];dp2 = new int[max+1];for(int i=0;i=x;j--) {dp1[j] = Math.max(dp1[j], dp1[j-x]+y);}}else if(op==2){for(int j = max;j>=x;j--) {dp1[j] = Math.min(dp1[j], dp1[j-x]-y);}}}}}
