二维背包10分,大佬们看看

P1734 最大约数和

所以为何要二维·-· 大可去掉前面一维,就不用管到底这个物品拿到哪儿了,反正后面更新都是更新成$dp[j]=dp[j-i]+c[i]$(第 $i$ 个数物品体积为$i$,价格为$c[i]$应该不用解释了)
by Kogenta @ 2021-08-26 16:12:42


@[Kogenta](/user/386890) ------------ # 我只会二维,太菜了
by 机智的娃 @ 2021-08-26 16:14:56


你不会是先学二维的吧?
by Kogenta @ 2021-08-26 17:21:25


@[机智的娃](/user/396613) 选数字 $i$ 占的背包体积是 $i$ ,贡献的话我看你这里写的是 $jz[i]$ ,那么转移方程就 $a[j]=max(a[j],a[j-i]+jz[i])$ 就可以了 (个人建议弄懂这个再去弄二维的,二维的第一维 $i$ 是表示前 $i$ 个数字可以得出的最大贡献,这题没必要把简单的问题复杂化)
by Kogenta @ 2021-08-26 17:27:18


``` import java.io.*; import java.io.BufferedReader; import java.lang.reflect.Array; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.text.Format; import java.text.SimpleDateFormat; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Map; import java.util.HashMap; public class Main { static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static StringBuilder stringBuilder = new StringBuilder(); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static String s = ""; static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); static int n = 0; static int min = (int) 2e9; static int sum = 0; static long sum1 = 0; static int max = -1; static int m1=0; static int m2=0; static int m3=0; static int m = 0; static int ans = 0; static int l = 0; static int r = 0; StringBuilder stringBuilder1=new StringBuilder(); static Map<Map<Integer,Integer> ,Integer> map= new TreeMap<Map<Integer, Integer>, Integer>(); static Map<Integer,Integer> map1= new TreeMap<>(); static Map<Integer,Integer> map2= new TreeMap<>(); static Map<String,Integer> map3= new TreeMap<>(); static int[] h = new int[15]; static boolean[] booleans=new boolean[101]; static int[][] b2 = new int[1][1]; static int[] dp = new int[5]; static int[][] a2 = new int[20][20]; static int[] a=new int[2022]; static int[] lie=new int[2022]; static int[] b=new int[15]; static int[] a1={0,1,-1,0,0}; static int[] b1={0,0,0,1,-1}; static String[] s1=new String[1005]; static Set<String> set=new TreeSet<>(); static Queue<Integer> queun=new LinkedList<Integer>();//单队列 static Deque<Integer> deque=new ArrayDeque<>();//双队列 static PriorityQueue<Integer> xiao = new PriorityQueue(); static PriorityQueue<Integer> xiao1 = new PriorityQueue(); static PriorityQueue<Integer> xiao2 = new PriorityQueue(); static PriorityQueue<Integer> da_1 = new PriorityQueue(Collections.reverseOrder()); static PriorityQueue<Long> da = new PriorityQueue(Collections.reverseOrder()); public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); n=Scanf_Int(); int[][] dp=new int[n+1][1500]; for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++){ dp[i][j]=dp[i-1][j]; if (j>=i){ dp[i][j]=Math.max(dp[i][j],dp[i-1][j-i]+ys_sum(i)); } } } out.print(dp[n][n]); out.close(); } public static int ys_sum(int x){ int sum=0; for (int i=2;i<=x;i++){ if (x%i==0) { sum += (x / i); } } return sum; } public static int mod(int i,int n){ return i%n; } public static boolean pd_sushu(int x){ for (int i=2;i<x;i++){ if ((x&1)==0){ return false; } } return true; } public static int find_daiquan(int n,int[] f,int[] front){ if(f[n]==n)return f[n]; int fn=find_daiquan(f[n],f,front); front[n]+=front[f[n]]; return f[n]=fn; } public static int find_dei(int x,int[] f){ if (f[x]==x){ return x; } return f[x]=find_dei(f[x],f); } public static void unity1(int x,int y,int[] f,int[] z){ int a=find_daiquan(x,f,z); int b=find_daiquan(y,f,z); f[a]=b; } public static void unity(int x,int y,int[] f){ int a=find_dei(x,f); int b=find_dei(y,f); f[a]=b; } public static void add(int a,int b,int v,Node[] nodes){ if (v==0){ nodes[b].w=a; nodes[b].q=nodes[a].q; nodes[a].q=b; nodes[nodes[b].q].w=b; } else { nodes[b].q=a; nodes[b].w=nodes[a].w; nodes[a].w=b; nodes[nodes[b].w].q=b; } } public static void jian(int a,Node[] nodes){ for (int i=nodes[0].w;i!=0;i=nodes[i].w){ if (i==a){ nodes[nodes[i].w].q=nodes[i].q; nodes[nodes[i].q].w=nodes[i].w; } } } public static long ef1(int[] a,int[] b ,int l,int r,int x){ sum=0; int mid = (l + r) / 2; if (l>r){ return sum; } if (b[x]<=a[1]){ sum+=(a[1]-b[x]); return sum; } if (b[x]>=a[n]){ sum+=(b[x]-a[n]); return sum; } if (b[x] >= a[mid - 1] && b[x] <= a[mid]) { sum += Math.min(a[mid] - b[x], b[x] - a[mid - 1]); return sum; } if (b[x]<a[mid]&&b[x]<a[mid-1]){ return ef1(a,b,l,mid-1,x); } if (b[x]>a[mid]&&b[x]>a[mid-1]){ return ef1(a,b,mid+1,r,x); } return sum; } public static int gys(int a,int b){ if (a==0||b==0){ return 0; } if (a%b==0){ return b; } return gys(b,a%b); } public static int gbs(int a,int b){ return a*b/gys(a,b); } public static long ks_mod(long a,long b){ long x=1,y=a; while (b>0){ if ((b&1)==1){ x*=y; x%=m1; } y*=y; y%=m1; b>>=1; } return x; } public static double log2(int n){ return Math.log(n)/Math.log(2); } public static void gb(int l,int r){ if (l==r){ return; } int mid=(l+r)/2; int z=mid+1,k=l,x=l,p=l; gb(l,mid); gb(mid+1,r); while (k<=mid&&z<=r){ if (a[k]<=a[z]){ b[x++]=a[k++]; } else { b[x++]=a[z++]; sum+=mid+1-k; } } while (k<=mid){ b[x++]=a[k++]; } while (z<=r){ b[x++]=a[z++]; } for (int i=l;i<=r;i++){ a[i]=b[i]; } } public static void cr(long[] a){ for (int i=1;i<a.length;i++){ long b=a[i]; int b1=i-1; while (b1>=0&&b>a[b1]){ a[b1+1]=a[b1]; b1--; } a[b1+1]=b; } } static int Scanf_Int() throws IOException { in.nextToken(); return (int) in.nval; } static long Scanf_Long() throws IOException { in.nextToken(); return (long) in.nval; } static double Scanf_Double() throws IOException{ in.nextToken(); return in.nval; } static char Scanf_Char()throws IOException{//输入字符后可以空格,也可以换行 in.nextToken(); return in.sval.charAt(0); } static String Scanf_String() throws IOException{ /*不能用这个来转变为字符数组和字符串数组,用bf.readLine(); 输入字符串后可以空格,也可以换行*/ in.nextToken(); return in.sval; } } class Node implements Comparable<Node> { int q,w; double e; String s; public Node(int q, int w) { this.q = q; this.w = w; } public Node(int q, String s) { this.q = q; this.s = s; } public Node(int q, int w, double e) { this.q = q; this.w = w; this.e = e; } @[Override](/user/542747) public int compareTo(Node o) { if (o.e>=this.e){ return 1; } return -1; } } class Node1 implements Comparable<Node1>{ int t,l,r,x; String s; public Node1(int t, int x, String s) { this.t = t; this.x = x; this.s = s; } @[Override](/user/542747) public int compareTo(Node1 o) { return o.t-this.t; } } 二维的,你看看,我感觉是68那 ```
by CYHMMZDAN @ 2022-11-15 13:02:14


|