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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值