LoginSignup
0
0

More than 3 years have passed since last update.

ABC - 014- A&B&C

Posted at

AtCoder ABC 014 A&B&C

AtCoder - 014

A - けんしょう先生のお菓子配り

    private void solveA() {
        int a = nextInt();
        int b = nextInt();
        int mod = a % b;
        out.println(mod == 0 ? 0 : b - mod);
    }

B - 価格の合計

  • xのどの位置のbitが1なのか?
    • bitが1の位置を商品価格のindexと対応させる
    private void solveB() {
        int n = nextInt();
        int x = nextInt();
        int[] wk = IntStream.range(0, n).map(i -> nextInt()).toArray();

        int sum = 0;
        int index = 0;
        while (x != 0) {
            if ((x & 1) == 1) {
                sum += wk[index];
            }
            x >>= 1;
            index++;
        }

        out.println(sum);
    }

C - AtColor

  • 累積和をとってしまえば簡単なんだけど。。。別解思いつかないなぁ
    private void solveC() {
        int n = nextInt();
        int[][] wk = Stream.generate(() -> new int[] { nextInt(), nextInt() }).limit(n).toArray(int[][]::new);

        int[] imos = new int[1000001];
        for (int i = 0; i < n; i++) {
            imos[wk[i][0]] += 1;
            if (wk[i][1] + 1 < imos.length) {
                imos[wk[i][1] + 1] -= 1;
            }
        }
        for (int i = 1; i < imos.length; i++) {
            imos[i] = imos[i] + imos[i - 1];
        }

        Arrays.sort(imos);
        out.println(imos[imos.length - 1]);
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0