Practice.
代码略去了快读模板
A. Add Plus Minus Sign
题意
给定一个长度为 
思路
无视第一位,配对 
时间复杂度:
对应AC代码
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.concurrent.atomic.*;
public class Main{
    public static void main(String[] args) throws Exception{
        Console console = new Console();
        int t = console.nextInt();
        while(t -- > 0){
            int n = console.nextInt();
            char[] input = console.next().toCharArray();
            int pre = -1, now = input[0] - '0';
            char[] ans = new char[n - 1];
            if(now == 1) pre = -2;
            for(int i=0;i<n-1;i++){
                now = input[i + 1] - '0';
                if(now == 0) ans[i] = '+';
                else{
                    if(pre == -1) pre = i;
                    else{
                        if(pre != -2) ans[pre] = '+';
                        ans[i] = '-';
                        pre = -1;
                    }
                }
            }
            if(pre != -1 && pre != -2) ans[pre] = '+';
            console.println(String.valueOf(ans));
        }
        console.close();
    }
} 略微简单的打卡题
B. Coloring
题意
给定 
思路
我们不妨开一个数组记录所有的数量,然后降序排序。
显然,若一个颜色 
遍历排序后的数组,若 
当然,配对次数过多时,我们会发现段数会减少 
时间复杂度:
对应AC代码
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.concurrent.atomic.*;
public class Main{
    public static void main(String[] args) throws Exception{
        Console console = new Console();
        int t = console.nextInt();
        while(t -- > 0){
            int n = console.nextInt(), m = console.nextInt(), k = console.nextInt();
            Integer[] a = new Integer[m];
            for(int i=0;i<m;i++) a[i] = console.nextInt();
            Arrays.sort(a, Comparator.comparingInt(o -> -o));
            int d = n / k + (n % k == 0 ? 0 : 1), l = n % k;
            boolean ans = true;
            for(int i=0;i<n;i++){
                int cur = a[i];
                if(cur > d) {
                    ans = false;
                    break;
                }
                if(l == 0) break;
                l --;
                if(l == 0) d --;
            }
            console.println(ans ? "YES" : "NO");
        }
        console.close();
    }
} 有点小贪心,因为不用考虑拿什么出来组合
C. Ice and Fire
题意
对于 
比赛机制为选择任意两个选手进行比赛,直至剩余最后一人,判定该选手为胜者。
思路
显然地,最后的获胜情况和最后一个输赢判定有直接关系。
更进一步地说,最后一段相同连续区间的长度决定了胜者的数量。
举一个例子,若 
因而,我们唯一关心的就是从后往前第一个不同的字符出现的位置,对于所有 
时间复杂度:
对应AC代码
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.concurrent.atomic.*;
public class Main{
    public static void main(String[] args) throws Exception{
        Console console = new Console();
        int t = console.nextInt();
        while(t -- > 0){
            int n = console.nextInt();
            char[] in = console.next().toCharArray();
            int p0 = 1, p1 = 1;
            for(int i=1;i<n;i++){
                int now = in[i - 1] - '0';
                if(now == 0){
                    p0 = i + 1;
                    console.print(p1 + " ");
                }else{
                    p1 = i + 1;
                    console.print(p0 + " ");
                }
            }
            console.println();
        }
        console.close();
    }
} 想通了就很简单的思维题
- 本文链接 https://floating-ocean.github.io/blog_old/posts/266401019/
 - 版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!