2022-02-09 swipe questions and punch in every day

Posted by linuxdream on Thu, 10 Feb 2022 04:26:53 +0100

1, ACWing: 01 knapsack problem

(1) Title Description

        

There are # NN items and a backpack with a capacity of # VV #. Each item can only be used once.

The volume of item , ii , is , vivi and the value is , wi.

Solve which items are loaded into the backpack, so that the total volume of these items does not exceed the backpack capacity, and the total value is the largest.
Output maximum value.

Input format

Two integers in the first line, N, VN and V, are separated by spaces, indicating the number of items and the volume of the backpack respectively.

Next, there are # NN lines, with two integers # VI, wivi and WI in each line, separated by spaces, representing the volume and value of the # second article respectively.

Output format

Output an integer representing the maximum value.

Data range

0<N,V≤10000<N,V≤1000
0<vi,wi≤10000<vi,wi≤1000

sample input

4 5
1 2
2 4
3 4
4 5

Output example:

8

(2) Code implementation

        

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int[][] dp=new int[10005][10005];
        int n=in.nextInt();
        int m=in.nextInt();
        int[] v=new int[10005];
        int[] w=new int[10005];
        for(int i=1;i<n;i++){
            int a=in.nextInt();
            v[i]=a;
            int b=in.nextInt();
            w[i]=b;
        }
        dp[0][0]=0;
        for(int i=1;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=dp[i-1][j];
                if(j>=v[i]){
                    dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
                }
            }
        }
        int res=0;
        for(int i=0;i<=m;i++) res=Math.max(res,dp[n][i]);
        System.out.println(res);
    }
}

ACWing: complete knapsack problem

(1) Title Description

        

There are , NN items and a backpack with a capacity of , VV , and there are unlimited items available for each item.

The volume of item , ii , is , vivi and the value is , wi.

Solve which items are loaded into the backpack, so that the total volume of these items does not exceed the backpack capacity, and the total value is the largest.
Output maximum value.

Input format

Two integers in the first line, N, VN and V, are separated by spaces, representing the number of items and the volume of the backpack respectively.

Next, there are # NN lines, with two integers # VI, wivi and WI in each line, separated by spaces, representing the volume and value of # ii # items respectively.

Output format

Output an integer representing the maximum value.

Data range

0<N,V≤10000<N,V≤1000
0<vi,wi≤10000<vi,wi≤1000

sample input

4 5
1 2
2 4
3 4
4 5

Output example:

10

(2) Code implementation

        

import java.util.*;

public class Main{
    static Scanner in=new Scanner(System.in);
    static int N=1010;
    static int v[] =new int[N],w[]=new int[N],f[][]=new int[N][N];
    public static void main(String args[]){
        int n=in.nextInt(),m=in.nextInt();
        for(int i=1;i<=n;i++){
            v[i]=in.nextInt();w[i]=in.nextInt();
        }
        for (int i = 1; i <= n; i++)
            for (int j = 0; j <= m; j++)
            {
                f[i][j] = f[i - 1][j];
                if (j >= v[i])
                    f[i][j] = Math.max(f[i][j], f[i][j - v[i]] + w[i]);
            }
        System.out.println(f[n][m]);
    }
}

III. performance analysis of the 11th Blue Bridge Cup F

[problem description]
Xiaolan organized an exam for the students. The total score of the paper is 100, and the score of each student is
An integer from 0 to 100.
Please calculate the highest score, lowest score and average score of this exam.
[input format]
The first line of input contains an integer n, which indicates the number of examinees.
The next n lines contain an integer from 0 to 100, representing a student's score.
[output format]
Output three lines.
The first line contains an integer indicating the highest score.
The second line contains an integer representing the lowest score.
The third line contains a real number, rounded to exactly two decimal places, indicating the average score.
[sample input]
7
80
92
56
74
88
99
10
[sample output]
99
10
71.29
[evaluation case scale and agreement]
For 50% of the evaluation cases, 1 ≤ n ≤ 100.
For all evaluation cases, 1 ≤ n ≤ 10000.


(2) Code implementation

        

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        int max=0;
        int min=1000;
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        double mid=0;
        for (int i = 0; i < n; i++) {
            int x=scanner.nextInt();
            max=Math.max(x,max);
            min=Math.min(x,min);
            mid+=x;
        }
        System.out.println(max);
        System.out.println(min);
        System.out.printf("%.2f",(mid/n));
    }
}

Topics: Algorithm