2018 9th Blue Bridge Cup Java Programming Undergraduate Group B Final Defense (Programming Topics)

2018 Ninth Blue Bridge Cup Java Programming Undergraduate Group B Final Individual Problem Solution Summary:

https://blog.csdn.net/daixinliangwyx/article/details/90258768

 

Question 6

Title: Defense

Xiao Ming is playing a game recently. Interested in defense in the game.
We think that the parameter that directly affects defense is "defense performance", denoted as d, and there are two defense values ​​A and B on the panel, which have a logarithmic relationship with d, A=2^d, B=3^d (note that any When the above formula is established).
During the game, there may be some props that increase the defense value A by a value, and other props that increase the defense value B by a value.
Now Xiao Ming has n1 props to increase the value of A and n2 props to increase the value of B. The amount of increase is known.

Now it is known whether the prop used for the i-th time increases the value of A or B, but it is uncertain which prop to use. Please find a way to use the prop with the smallest lexicographical order to maximize the final defense performance.

Initially, the defense performance is 0, that is, d=0, so A=B=1.

[Input format]
The first line of input contains two numbers n1, n2, separated by spaces.
The number of n1 in the second line represents the increase of those props that increase the A value.
The number of n2 in the third row represents the increase of those props that increase the B value.
The fourth line is a string of length n1+n2, consisting of 0s and 1s, indicating the order in which the props are used. 0 means using an item that increases the A value, 1 means using an item that increases the B value. The input data is guaranteed to have exactly n1 0s and n2 1s.

[Output format]
For each set of data, output n1+n2+1 lines, and the first n1+n2 lines output the usage of the props in order. If the props with increased A value are used, output Ax, where x is the props in this type of props number (starting at 1). If you use an item that increases the B value, output Bx. The last line outputs an uppercase E.

[Sample input 1]
1 2
4
2 8
101

[Sample output 1]
B2
A1
B1
E

【Sample input 2】
3 0
7 11 13

000

[Sample output 2]
A1
A2
A3
E

[Example description]
For the first set of test data, the operation process is as follows:
operation d A B
initial 0 1 1
B2 2 4 9
A1 3 8 27
B1 log3(29) 2^(log3(29)) 29

It can be shown that this value is the largest.
For the second set of test data, it can be seen that no matter what order is used, A is always 32 at the end, that is, d is always 5, and B is always 243. 

[Data scale]
For 20% of the data, the string length <= 10000;
for 70% of the data, the string length <= 200000;
for 100% of the data, the string length <= 2000000, each input increment value is not more than 2^30.


Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms


Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".

All code is placed in the same source file, after debugging, copy and submit the source code.
Don't use the package statement. Do not use features of jdk1.7 and above.
The name of the main class must be: Main, otherwise it will be processed as invalid code.


Solution: Because d=log2(A)=log3(B) is true at any time, when performing a cross operation on A and B to increase the x value, such as operating on A first, it must be the first to increase the larger ai to gain the greatest benefit , because the first increase of ai will make d increase greatly this time, and at the same time, B will also increase greatly, and there will be better benefits for the next increase of B; the operation of B is similar to the above, that is, to ai and bi are sorted by a structure, first by the size of the value and then by the subscript order (lexicographical order). Of course, pay attention to a situation, that is, the benefits are the same no matter what order is used. Under what circumstances does this happen? Obviously, when operating a certain defense value continuously, no matter what the larger value is first increased It is still small. Before operating another defense value, the total value of the defense value increase is the same, and the total impact value of d and another defense value is the same, so at this time, it is necessary to operate in lexicographical order. .

Code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class MainB {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);
    public static int n1, n2, d, a, b, len, ka, kb, k;
    public static String s;
    public static A[] ai = new A[2000010];
    public static B[] bi = new B[2000010];
    public static int[] order;

    public static void main(String[] args) {
        d = 0;
        a = 1;
        b = 1;
        n1 = in.nextInt();
        n2 = in.nextInt();
        if (n1 == 0) s = in.nextLine();//吸取空行
        for (int i = 1; i <= n1; i++) {
            ai[i] = new A();
            ai[i].id = i;
            ai[i].value = in.nextInt();
        }
        if (n2 == 0) s = in.nextLine();
        for (int i = 1; i <= n2; i++) {
            bi[i] = new B();
            bi[i].id = i;
            bi[i].value = in.nextInt();
        }
        s = in.nextLine();
        Arrays.sort(ai, 1, n1+1);
        Arrays.sort(bi, 1, n2+1);
        len = s.length();
        ka = 1;
        kb = 1;
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == '0') {
                if (s.charAt(i) == '1') {
                    out.println("A" + ai[ka++].id);
                    out.flush();
                } else {//出现连续的0
                    order = new int[len-i+5];
                    k = 0;
                    order[k++] = ai[ka++].id;//将这一段连续的'0'对应的id存在一个数组里
                    int j = i + 1;
                    for (j = i+1; j < len; j++) {
                        if (s.charAt(j) != '0') break;
                        order[k++] = ai[ka++].id;
                    }
                    Arrays.sort(order, 0, k);//按id从小到大排(即字典序从小到大)
                    i = j - 1;//调整i,使i下一次循环是从后面第一个'1'处开始
                    for (j = 0; j < k; j++) {
                        out.println("A" + order[j]);
                        out.flush();
                    }
                }
            } else {
                if (s.charAt(i) == '0') {
                    out.println("B" + bi[kb++].id);
                    out.flush();
                } else {//出现连续的1
                    order = new int[len-i+5];
                    k = 0;
                    order[k++] = bi[kb++].id;//将这一段连续的'1'对应的id存在一个数组里
                    int j = i + 1;
                    for (j = i+1; j < len; j++) {
                        if (s.charAt(j) != '1') break;
                        order[k++] = bi[kb++].id;
                    }
                    Arrays.sort(order, 0, k);//按id从小到大排(即字典序从小到大)
                    i = j - 1;//调整i,使i下一次循环是从后面第一个'0'处开始
                    for (j = 0; j < k; j++) {
                        out.println("B" + order[j]);
                        out.flush();
                    }
                }
            }
        }
        out.println("E");
        out.flush();
        out.close();
    }

    static class A implements Comparable<A>{
        int id, value;

        @Override
        public int compareTo(A o) {
            if (o.value - this.value != 0) {
                return o.value - this.value;
            } else {
                return this.id - o.id;
            }
        }
    }

    static class B implements Comparable<B>{
        int id, value;

        @Override
        public int compareTo(B o) {
            if (o.value - this.value != 0) {
                return o.value - this.value;
            } else {
                return this.id - o.id;
            }
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326611344&siteId=291194637