Codeforces Round 895 (Div. 3)

Codeforces Round 895 (Div. 3)

Codeforces Round 895 (Div. 3) A. Two Vessels

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 a = Integer.parseInt(str[0]);
        int b = Integer.parseInt(str[1]);
        int c = Integer.parseInt(str[2]);
        int d = Math.abs(a - b);
        int ans = (d + 2 * c - 1) / (2 * c);
        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 895 (Div. 3) B. The Corridor or There and Back Again

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());
        int ans = 1000000000;
        for (int i = 0; i < n; i++) {
            String[] str = bf.readLine().split(" ");
            int d = Integer.parseInt(str[0]);
            int s = Integer.parseInt(str[1]);
            ans = Math.min(ans, d + (s - 1) / 2);
        }
        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 895 (Div. 3) C. Non-coprime Split

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 l = Integer.parseInt(str[0]);
        int r = Integer.parseInt(str[1]);
        for (int i = Math.max(4, l); i <= r; i++) {
            for (int j = 2; j * j <= i; j++) {
                if (i % j == 0) {
                    bw.write(j + " " + (i - j) + "\n");
                    return;
                }
            }
        }
        bw.write("-1\n");
    }

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

Codeforces Round 895 (Div. 3) D. Plus Minus Permutation

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 long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    public static long lcm(long a, long b) {
        return a * b / gcd(a, b);
    }

    public static void solve() throws IOException {
        String[] str = bf.readLine().split(" ");
        long n = Long.parseLong(str[0]);
        long x = Long.parseLong(str[1]);
        long y = Long.parseLong(str[2]);
        long l = lcm(x, y);
        long pos = n / x - n / l;
        long neg = n / y - n / l;
        long ans = (n + n - pos + 1) * pos / 2 - (neg + 1) * neg / 2;
        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 895 (Div. 3) E. Data Structures Fan

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());
        String[] str = bf.readLine().split(" ");
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(str[i]);
        }
        int[] sum = new int[n + 1];
        String s = bf.readLine();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '1') {
                ans ^= a[i];
            }
            sum[i + 1] = sum[i] ^ a[i];
        }
        int q = Integer.parseInt(bf.readLine());
        while (q-- > 0) {
            str = bf.readLine().split(" ");
            int tp = Integer.parseInt(str[0]);
            if (tp == 1) {
                int l = Integer.parseInt(str[1]);
                int r = Integer.parseInt(str[2]);
                l--;
                ans ^= sum[l] ^ sum[r];
            } else {
                int g = Integer.parseInt(str[1]);

                if (g == 1) {
                    bw.write(ans + " ");
                } else {
                    bw.write((ans ^ sum[n]) + " ");
                }
            }
        }
        bw.write("\n");
    }

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

Codeforces Round 895 (Div. 3) F. Selling a Menagerie

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

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 + 1];
        int[] in = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            a[i] = Integer.parseInt(str[i - 1]);
            in[a[i]]++;
        }
        str = bf.readLine().split(" ");
        int[] c = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            c[i] = Integer.parseInt(str[i - 1]);
        }
        Queue<Integer> Q = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            if (in[i] == 0) {
                Q.add(i);
            }
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        while (!Q.isEmpty()) {
            int x = Q.remove();
            arrayList.add(x);
            if (--in[a[x]] == 0)
                Q.add(a[x]);
        }
        for (int i = 1; i <= n; i++) {
            if (in[i] != 0) {
                int x = i, mid = c[i], s = i;
                do {
                    x = a[x];
                    in[x] = 0;
                    if (mid > c[x]) {
                        mid = c[x];
                        s = x;
                    }
                }
                while (x != i);
                x = s;
                do {
                    x = a[x];
                    arrayList.add(x);
                }
                while (x != s);
            }
        }
        for (Integer ai : arrayList)
            bw.write(ai + " ");
        bw.write("\n");
    }

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

Codeforces Round 895 (Div. 3) G. Replace With Product

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));

    public static void solve() throws IOException {
        int n = Integer.parseInt(bf.readLine());
        String[] str = bf.readLine().split(" ");
        int[] a = new int[n + 1];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(str[i]);
        }
        int l = 0, r = n - 1;
        while (l < r && a[l] == 1) l++;
        while (l < r && a[r] == 1) r--;
        long res = 1;
        boolean ok = false;
        for (int i = l; i <= r; i++) {
            res *= a[i];
            if (res > 1000000000L) {
                ok = true;
                bw.write((l + 1) + " " + (r + 1) + "\n");
                break;
            }
        }
        if (ok) return;
        ArrayList<Integer> p = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (a[i] > 1) {
                p.add(i);
            }
        }
        int sum = 0;
        for (int i = 0; i < n; i++) sum += a[i];
        int L = 1, R = 1;
        int ans = sum;
        for (int i = 0; i < p.size(); i++) {
            int ll = p.get(i);
            int ress = 1;
            int s = 0;
            for (int j = i; j < p.size(); j++) {
                int rr = p.get(j);
                ress *= a[rr];
                s += a[rr] - 1;
                int v = sum - (rr - ll + 1) - s + ress;
                if (v > ans) {
                    ans = v;
                    L = ll + 1;
                    R = rr + 1;
                }
            }
        }
        bw.write(L + " " + R + "\n");
    }

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

你可能感兴趣的:(#,CodeForces,java,数据库,linux)