P1926. Little schoolboy - question brushing Army
subject
Topic background
Mathematics is fire, lighting up the lamp of physics; Physics is a lamp, illuminating the way of chemistry; Chemistry is the road leading to the pit of biology; A creature is a pit, burying a man of reason. Classical Chinese is fire, lighting up the palace lamp of history; History is a lamp, illuminating the road of society; Society is the road leading to the pit of philosophy; Philosophy is a pit, burying liberal arts students—— Little a
Title Description
Small A "brush questions" is very rampant and brazenly "brush questions". He now found n kinds of "questions" he likes in the little schoolboy. Each "question" has his time needed, and the teacher assigned m homework. Each homework has its time needed and score. The teacher stipulates that more than k points are considered as passing. Little A has only r unit time left. He wants to "brush questions" more on the basis of passing.
Input format
First line: n m k r. The second line: n numbers, representing the time required for each "question". The third line: m number. Indicates the time required for each job. The fourth line: m number. Represents the score of each assignment.
Output format
A number represents how many questions a can brush
Sample input and output
Enter #1 copy
3 4 20 100 15 20 50 10 15 40 40 5 5 10 15
Output #1 copy
2
Description / tips
There is no failure
For 100% data, n ≤ 10 , m ≤ 10 , k ≤ 50 , r ≤ 150 n\le 10,m\le 10,k\le 50,r\le 150 n≤10,m≤10,k≤50,r≤150
thinking
This is actually a very common 01 backpack problem, but the difference is that he has to backpack twice.
In the first knapsack process, once the score of the current situation has been greater than the passing score, we will judge the current time required. After many cycles, we calculate the minimum time to pass
Then next, we subtract the minimum time it takes to pass, and the remaining time is the time that allows us to brush questions freely
Then came the most common backpack
The data range here is not large, so it is two-dimensional d p dp dp should also be no problem
But my code uses one dimension d p dp dp, so it can spend less space complexity
You'd better learn d p dp dp state compression to prevent game card data?
t i p s : tips: tips: one dimensional d p dp dp should pay attention to the reverse traversal of the inner loop, and the traversal range is different!!!!
code
package com.lanqiao; import java.util.Scanner; /** * @author Wang Yuzhe **/ public class P1926 { /** * Number of topics A likes */ static int n; /** * Number of questions assigned by the teacher */ static int m; /** * The passing grade prescribed by the teacher */ static int k; /** * Small A remaining time */ static int r; /** * The total time required for the questions he wants to brush */ static int[] requireTimeForLike; /** * The time required for homework specified by the teacher */ static int[] requireTimeForOrder; /** * The score of the homework specified by the teacher */ static int[] scoreForOrder; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); m = scanner.nextInt(); k = scanner.nextInt(); r = scanner.nextInt(); requireTimeForLike = new int[n+1]; requireTimeForOrder = new int[m+1]; scoreForOrder = new int[m+1]; for (int i = 1;i <= n;i ++){ requireTimeForLike[i] = scanner.nextInt(); } for (int i = 1;i <= m;i ++){ requireTimeForOrder[i] = scanner.nextInt(); } for (int i = 1;i <= m;i ++){ scoreForOrder[i] = scanner.nextInt(); } //*************How much time is left when you pass*************** int[] dp = new int[r+1]; int minRequireTime = 0xfffffff; for(int i = 1;i<=m;i++){ for(int j = r;j>=requireTimeForOrder[i];j--){ dp[j] = Math.max(dp[j],dp[j-requireTimeForOrder[i]]+scoreForOrder[i]); //If it takes so long to pass, update the minimum time required if(dp[j] > k){ minRequireTime = Math.min(minRequireTime,j); } } } //From now on, the rest of the time is small A's r -= minRequireTime; //*************Find the maximum number of completed questions******************** dp = new int[r+1]; for(int i = 1;i <=n ;i++){ for(int j=r;j>=requireTimeForLike[i];j--){ dp[j] = Math.max(dp[j],dp[j-requireTimeForLike[i]]+1); } } System.out.println(dp[r]); } }