AcWing's 96th weekly match

 Contest - AcWing

1. Perfect number

4876. Perfect Number - AcWing Question Bank

 1. Topic

A positive integer is said to be perfect if it is divisible by 2520.

Given a positive integer n, please calculate how many perfect numbers there are in the range [1,n].

input format

an integer n.

output format

An integer representing the number of perfect numbers in the range [1,n].

data range

The first 3 test points satisfy 1≤n≤3000.
All test points satisfy 1≤n≤10¹⁸.

Input sample:

3000

Sample output:

1

 2. Ideas

 Simple questions, just look at the code

  3. Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        long n=sc.nextLong();
        System.out.println(n/2520);
    }
}

 2. Maximum value

4877. Maximum Value - AcWing Question Bank

 1. Topic

data range

The first 4 test points satisfy 1≤n≤100, 1≤m≤2.
All test points satisfy 1≤n≤1000, 1≤m≤10, 1≤ lᵢ ,hᵢ ,vᵢ ,wᵢ ≤100.

Input sample 1:

10 2 2 1
7 3 2 100
12 3 1 10

Output sample 1:

241

Input sample 2:

100 1 25 50
15 5 20 10

Output sample 2:

200

2. Ideas

 If you see a particularly well-written solution in the solution, let's just read it!

Backpack Problem - 01 Backpack|Complete Backpack_Leng Xixue's Blog-CSDN Blog

You can take a look at this 01 backpack for a better understanding.

 3. Code

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int v0 = sc.nextInt();
        int w0 = sc.nextInt();
        long[] count = new long[n + 1];//背包的容量

        for (int i = 1; i <= m; i++) {//先遍历第1到m个物品
            int l = sc.nextInt(), h = sc.nextInt(), v = sc.nextInt(), w = sc.nextInt();
            int s = l / h;//计算物品i可以放几个
            for (int j = n; j >= 0; j--) {//再倒序遍历背包容量
                for (int k = 1; k <= s && k * v <= j; k++) {//1到s个物品i放与不放 取最大值
                    count[j] = Math.max(count[j], count[j - k * v] + (long) k * w);
                }
            }
        }

        long ans = 0;
        for (int i = 0; i <= n; i++) {//遍历背包容量,先看是否可以放进,再取放与不放的最大值
            if (i >= v0) {
                count[i] = Math.max(count[i], count[i - v0] + w0);
            }
            //求出最大的背包价值
            ans = Math.max(ans, count[i]);
        }
        System.out.println(ans);
    }
}

 3. Maintain the array

4878. Maintain Array - AcWing Question Bank

1. Topic

input format

The first line contains 55 integers n,k,a,b,q.

The next q lines, each line describes an operation, the format is as described in the title.

output format

For each  operation, output a line with an integer representing the result. 2 p

data range

The first 33 test points satisfy 1≤k≤n≤10, 1≤q≤10.
All test points satisfy 1≤k≤n≤2×10⁵, 1≤b<a≤10000, 1≤q≤2×10⁵, 1≤x≤n, 1≤y≤10000, 1≤p≤n−k+ 1.

Input sample 1:

5 2 2 1 8
1 1 2
1 5 3
1 2 1
2 2
1 4 2
1 3 2
2 1
2 3

Output sample 1:

3
6
4

Input sample 2:

5 4 10 1 6
1 1 5
1 5 5
1 3 2
1 5 2
2 1
2 2

Output sample 2:

7
1

 2. Ideas

To make the time complexity of the written code logN, we need to use a tree array. Ordinary methods will run with a timeout.

If you want to know more about tree arrays, you can read this blog

Simple and easy-to-understand detailed explanation of tree arrays - FlushHip's Blog - CSDN Blog

If you want to quickly understand the idea of ​​writing questions, you can read this blog

Detailed Explanation of Tree Array - Xiao Heshan's Blog - CSDN Blog

 3. Code

import java.io.*;

public class Main {
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static int N = 200010, n, k, a, b, q;
    static int[] tree1 = new int[N], tree2 = new int[N], d = new int[N];

    static int lowbit(int x) {
        return x & -x;
    }

    static void add(int[] tree, int x, int v) {
        for (int i = x; i <= n; i += lowbit(i))
            tree[i] += v;
    }

    static long query(int[] tree, int x) {
        long ans = 0;
        for (int i = x; i > 0; i -= lowbit(i))
            ans += tree[i];
        return ans;
    }

    public static void main(String[] args) throws Exception{
        String[] s1 = in.readLine().split(" ");
        n = Integer.parseInt(s1[0]);
        k = Integer.parseInt(s1[1]);
        a = Integer.parseInt(s1[2]);
        b = Integer.parseInt(s1[3]);
        q = Integer.parseInt(s1[4]);

        while (q -- > 0) {
            String[] s2 = in.readLine().split(" ");
            int op = Integer.parseInt(s2[0]);
            if (op == 1) {  //增加   进行单点修改
                int x = Integer.parseInt(s2[1]), y = Integer.parseInt(s2[2]);
                add(tree1, x, Math.min(d[x] + y, b) - Math.min(d[x], b));
                add(tree2, x, Math.min(d[x] + y, a) - Math.min(d[x], a));
                d[x] += y;
            }else {  //查询   进行区间查询
                int p = Integer.parseInt(s2[1]);
                long sum = query(tree1, p - 1) + query(tree2, n) - query(tree2, p + k - 1);
                out.println(sum);
            }
        }
        out.flush();
    }
}

Guess you like

Origin blog.csdn.net/m0_63951142/article/details/130310963