Codeforces Round 903 (Div. 3)

Codeforces Round 903 (Div. 3)

Codeforces Round 903 (Div. 3) A. Don’t Try to Count

import java.io.*;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void solve() throws IOException {
        String[] str = bf.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        String x = bf.readLine();
        CharSequence s = bf.readLine();
        int ans = 0;
        while (!x.contains(s) && (ans == 0 || x.length() < 2 * m)) {
            x = x + x;
            ans++;
        }
        if (!x.contains(s)) {
            bw.write("-1\n");
        } else {
            bw.write(ans + "\n");
        }
    }

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
            solve();
        }
        bw.close();
    }
}

Codeforces Round 903 (Div. 3) B. Three Threadlets

import java.io.*;
import java.util.Arrays;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void solve() throws IOException {
        int[] a = new int[4];
        String[] str = bf.readLine().split(" ");
        for (int i = 1; i <= 3; i++) {
            a[i] = Integer.parseInt(str[i - 1]);
        }
        Arrays.sort(a, 1, 4);
        if (a[1] == a[3] || a[1] == a[2] && (a[2] * 2 == a[3] || a[2] * 3 == a[3] || a[2] * 4 == a[3]))
            bw.write("YES\n");
        else if (a[1] * 2 == a[2] && a[1] * 3 == a[3])
            bw.write("YES\n");
        else if (a[1] * 2 == a[2] && a[2] == a[3])
            bw.write("YES\n");
        else
            bw.write("NO\n");
    }

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
            solve();
        }
        bw.close();
    }
}

Codeforces Round 903 (Div. 3) C. Perfect Square

import java.io.*;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void solve() throws IOException {
        int n = Integer.parseInt(bf.readLine());
        char[][] s = new char[n][n];
        for (int i = 0; i < n; i++) {
            s[i] = bf.readLine().toCharArray();
        }
        int ans = 0;
        for (int i = 0; i < n / 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                int sum = s[i][j] + s[j][n - 1 - i] + s[n - 1 - i][n - 1 - j] + s[n - 1 - j][i];
                int max = Math.max(Math.max(s[i][j], s[j][n - 1 - i]), Math.max(s[n - 1 - i][n - 1 - j], s[n - 1 - j][i]));
                ans += max * 4 - sum;
            }
        }
        bw.write(ans + "\n");
    }

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
            solve();
        }
        bw.close();
    }
}

Codeforces Round 903 (Div. 3) D. Divide and Equalize

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    static int[] minp = new int[1000010];
    static ArrayList<Integer> primes = new ArrayList<>();
    static int[] cnt = new int[1000010];

    public static void init(int n) {
        Arrays.fill(minp, 0);
        for (int i = 2; i <= n; i++) {
            if (minp[i] == 0) {
                minp[i] = i;
                primes.add(i);
            }
            for (Integer p : primes) {
                if (i * p > n) {
                    break;
                }
                minp[i * p] = p;
                if (p == minp[i]) {
                    break;
                }
            }
        }
    }

    public static void solve() throws IOException {
        int n = Integer.parseInt(bf.readLine());
        ArrayList<Integer> stk = new ArrayList<>();
        String[] str = bf.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            int x = Integer.parseInt(str[i]);
            while (x > 1) {
                int p = minp[x];
                x /= p;
                stk.add(p);
                cnt[p]++;
            }
        }
        boolean ok = true;
        for (Integer x : stk) {
            if (cnt[x] % n != 0) {
                ok = false;
            }
            cnt[x] = 0;
        }
        bw.write(ok ? "YES\n" : "NO\n");
    }

    public static void main(String[] args) throws IOException {
        init(1000001);

        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
            solve();
        }
        bw.close();
    }
}

Codeforces Round 903 (Div. 3) E. Block Sequence

import java.io.*;
import java.util.Arrays;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void solve() throws IOException {

        int n = Integer.parseInt(bf.readLine());
        String[] str = bf.readLine().split(" ");
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(str[i]);
        }
        int[] dp = new int[n + 1];
        Arrays.fill(dp, n);
        dp[n] = 0;
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = dp[i + 1] + 1;
            if (i + a[i] < n) {
                dp[i] = Math.min(dp[i], dp[i + a[i] + 1]);
            }
        }
        bw.write(dp[0] + "\n");
    }

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
            solve();
        }
        bw.close();
    }
}

Codeforces Round 903 (Div. 3) F. Minimum Maximum Distance

import java.io.*;
import java.util.ArrayList;

public class Main {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static int N = 200010;
    static ArrayList<Integer>[] g = new ArrayList[N];
    static boolean[] mark = new boolean[N];
    static int ans, pos;

    public static void dfs(int u, int p, int dist) {
        if (mark[u]) {
            if (dist > ans) {
                ans = dist;
                pos = u;
            }
        }
        for (Integer v : g[u]) {
            if (v != p) {
                dfs(v, u, dist + 1);
            }
        }
    }

    public static void solve() throws IOException {
        String[] str = bf.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int k = Integer.parseInt(str[1]);
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            mark[i] = false;
        }
        str = bf.readLine().split(" ");
        for (int i = 0; i < k; i++) {
            int x = Integer.parseInt(str[i]);
            mark[x] = true;
            pos = x;
        }
        for (int i = 0; i < n - 1; i++) {
            str = bf.readLine().split(" ");
            int u = Integer.parseInt(str[0]);
            int v = Integer.parseInt(str[1]);
            g[u].add(v);
            g[v].add(u);
        }
        ans = 0;
        dfs(pos, 0, 0);
        dfs(pos, 0, 0);
        bw.write((ans + 1 >> 1) + "\n");
    }

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(bf.readLine());
        while (T-- > 0) {
            solve();
        }
        bw.close();
    }
}

你可能感兴趣的:(#,CodeForces,java,开发语言)