Blue Bridge Cup real questions 6

triangular palindrome

Problem Description

For a positive integer n, if there is a positive integer k such that n=1+2+3+⋯+k=k(k+1)/2, then n is called a triangular number. For example, 66066 is a trigonometric number because 66066=1+2+3+⋯+363.

An integer is said to be a palindromic number if reading all digits from left to right is the same as reading all digits from right to left. For example, 66066 is a palindromic number, and 8778 is also a palindromic number.

If an integer n is both triangular and palindromic, we call it a triangular palindrome. For example 66066 is a triangular palindrome number.

Excuse me, what is the first triangular palindrome number greater than 20220514?

answer submission

This is a fill-in-the-blank question, you just need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. If you fill in the extra content, you will not be able to score.

Running limit
Maximum running time: 1s
Maximum running memory: 256M

analyze

Direct violent enumeration is enough to judge whether it is a palindrome and whether it is a triangular number

Code

package com.zxy.exam;

public class _三角回文数 {
    
    
    public static void main(String[] args) {
    
    
        int i = 20220514;
        while(true){
    
    
            if(isHUIWEN(i) && isSANJIAO(i)){
    
    
                System.out.println(i);
                break;
            }
            i++;
        }
    }
    public static boolean isSANJIAO(int n){
    
    
        int k = (int)Math.sqrt(2*n)-1;
        while(k*(k+1)<= 2*n){
    
    
            if(k*(k+1)==2*n){
    
    
                return true;
            }
            k++;
        }
        return false;
    }
    public static boolean isHUIWEN(int n){
    
    
        char[] a = (n+"").toCharArray();
        int l = 0;
        int r = a.length-1;
        while(l <= r){
    
    
            if(a[l]!=a[r]) return false;
            l++;
            r--;
        }

        return true;
    }
}

topic description

Xiaolan likes eating chocolate very much. He eats a piece of chocolate every day.

One day Xiaolan went to the supermarket to buy some chocolates. There are many kinds of chocolates on the shelves in the supermarket. Each type of chocolate has its own price, quantity and remaining shelf life. Xiaolan only eats chocolates that have not expired. May I ask how much Xiaolan can buy for xx days at least? chocolate.

input format

The first line of input contains two integers xx, nn, which respectively represent the number of days you need to eat chocolate and the number of types of chocolate.

The next line nn describes the chocolates on the shelf, where line ii contains three integers aiai, bibi, cici, indicating that the unit price of the ii type of chocolate is aiai, and there are bibi days left in the shelf life (bibi days from now can be eaten), The quantity is cici.

output format

Output an integer representing Xiaolan's minimum cost. If there is no purchase plan to let Xiaolan eat for xx days, output −1−1.

Input and output samples

input#1

10 3
1 6 5
2 7 3
3 10 10
output #1

18
Instructions/Tips

【Example description】

An optimal plan is to buy 55 yuan for the 11th kind, 22 yuan for the 22nd kind, and 33 yuan for the 33rd kind. Eat the 11th species for the first 55 days, the 22nd species for the 66th and 77th days, and the 33rd species for the 88th to 1010th days.

code and ideas

 import java.io.*;
import java.util.*;
public class 巧克力_优先队列 {
    
    
    static PrintWriter out = new PrintWriter(System.out);
    static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in = new StreamTokenizer(ins);
 
 
    static class Node implements Comparable<Node> {
    
    
        int id;
        int price_a;
        int day_b;
        int number_c;
 
        Node(int id, int price_a, int day_b, int number_c) {
    
    
            this.id = id;
            this.price_a = price_a;
            this.day_b = day_b;
            this.number_c = number_c;
        }
 
        //按保质期从高到低进行排序
        @Override
        public int compareTo(Node o) {
    
    
            if (o.day_b > this.day_b) {
    
    
                return 1;
            } else {
    
    
                return -1;
            }
        }
    }
    static class Node1  {
    
    
        int id;
        int price_a;
        int day_b;
        int number_c;
 
        Node1(int id, int price_a, int day_b, int number_c) {
    
    
            this.id = id;
            this.price_a = price_a;
            this.day_b = day_b;
            this.number_c = number_c;
        }
    }
 
    public static void main(String[] args) throws IOException {
    
    
        String[] sp = ins.readLine().split(" ");
        int x = Integer.parseInt(sp[0]);
        int n = Integer.parseInt(sp[1]);
        Node[] nodes = new Node[n];
        int[] nums = new int[n];
        long ans = 0;
        for (int i = 0; i < n; i++) {
    
    
            String[] sp1 = ins.readLine().split(" ");
            nodes[i] = new Node(i, Integer.parseInt(sp1[0]), Integer.parseInt(sp1[1]), Integer.parseInt(sp1[2]));
        }
        //按保质期从高到低进行排序
        Arrays.sort(nodes);
        int j = 0;
//        Prio<Node1> priority_queue = new LinkedList<>();
        PriorityQueue<Node1>priority_queue=new PriorityQueue<>(
                new Comparator<Node1>() {
    
    
                    @Override
                    public int compare(Node1 o1, Node1 o2) {
    
    
                        return o1.price_a-o2.price_a;
                    }
                }
        );
        for (int i = x; i >= 1; i--) {
    
    
            while (j < n && nodes[j].day_b >= i) {
    
    
                priority_queue.offer(new Node1(nodes[j].id,nodes[j].price_a,nodes[j].day_b,nodes[j].number_c));
                j++;
            }
            //如果出现空队列表示没有选择了
            if (priority_queue.size() == 0) {
    
    
                out.println(ans);
                out.println(-1);
                out.flush();
                return;
            }
            Node1 node = priority_queue.peek();
            //表示当前id的物品个数+1
            nums[node.id]++;
//            System.out.println("id:"+node.id+"价格:"+node.price_a+"购买数量:"+nums[node.id]);
            //加上当前物品的价格
            ans += node.price_a;
            //表示当前物品全部选完了
            if (node.number_c == nums[node.id]) {
    
    
//                System.out.println("物品id:"+node.id+"出队");
                //当前种类的物品已经全部选完了,所以当前物品出队
                priority_queue.poll();
            }
        }
        out.println(ans);
        out.flush();
    }
}

[Blue Bridge Cup 2017 Province B] k times interval

topic description

Given a length NNN 的数列, A 1 , A 2 , ⋯ A N A_1,A_2, \cdots A_N A1,A2,AN, if one of the continuous subsequences A i , A i + 1 , ⋯ A j ( i ≤ j ) A_i,A_{i+1}, \cdots A_j(i \le j)Ai,Ai+1,Aj(ij ) sum isKKA multiple of K , we call this interval[ i , j ] [i,j][i,j ] isKKK times interval.

Can you find out how many KKs there are in the sequenceK- fold interval?

input format

The first line contains two integers NNN andKKK ( 1 ≤ N , K ≤ 1 0 5 ) (1 \le N,K \le 10^5)(1N,K105)

following NNEach line of N lines contains an integer A i A_iAi( 1 ≤ A i ≤ 1 0 5 ) (1 \le A_i \le 10^5)(1Ai105)

output format

Output an integer representing KKThe number of K -fold intervals.

Example #1

Sample Input #1

5 2
1  
2  
3  
4  
5

Sample output #1

6

hint

Time limit 2 seconds, 256M. The Eighth Blue Bridge Cup in 2017

analyze

The topic is very simple, my first reaction is violence, directly enumerate the left and right intervals in a double cycle

import java.util.*;
public class Main{
    
    
    static int res;
    public static void main(String[] args){
    
    
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] a = new int[n+1];
        for(int i = 0;i<n;i++){
    
    
            a[i]=scan.nextInt();
        }
        for(int i = 0;i<n;i++){
    
    
            int q = 0;
            for(int j=i;j<n;j++){
    
    
                q+=a[j];
                if(q%k==0) res++;
            }
        }
        System.out.println(res);
    }
}

There is nothing wrong with the code, but it will time out,

When we consider optimization, interval and problems, we naturally think of the prefix sum, which can optimize one dimension, but if it is not enough to optimize this alone, it will still time out, so we have to think of a new method to judge whether Satisfy the K times interval, according to the congruence theorem , so there are the following ideas

It is known that a<b, suma, and sumb are the prefix sum of a and b (the sum of all numbers from the first number in the sequence to itself), and suma%k=x, sumb%k=x. Then you can get (sumb-suma)%k=0. That is, the interval c generated by ba must be a k-fold interval

Then we can calculate the number of intervals with prefix and modulus k values ​​of 0, 1, 2...k-1 respectively, and then calculate them separately.

import java.util.Scanner;

public class Main {
    
    
    // 后面用不到原数列,所以只存储前缀和
    public static int[] sum = new int[100005];
    // 用来统计相同余数的的个数
    public static long[] remainder = new long[100005];
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        long ans = 0;
        // 前0项和是0,注意余数为0开始就出现一次
        remainder[0] = 1;
        int n = sc.nextInt();
        int k = sc.nextInt();
        for (int i = 1; i <= n; i++) {
    
    
            int num = sc.nextInt();
            sum[i] = sum[i - 1] + num;
            remainder[sum[i] % k]++;
        }
        sc.close();
        for (int i = 0; i < k; i++)
            ans += (remainder[i] * (remainder[i] - 1)) >> 1;
        System.out.println(ans);
    }
}

Guess you like

Origin blog.csdn.net/m0_64102491/article/details/130049078