0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC431の記録

Last updated at Posted at 2025-11-08

AtCoder431の感想と自分が解いたところまでの解説を書いていきます
(今回はA,B,Cを, python, A,Bをjavaでも書きます)
AtCoder Beginner Contest 431
今回からちゃんと解説書きます

1.感想

今回からint main()signed main()にしました(intをllに変換するときのエラー防止)
A問題:
簡単だった
1分以内で解けた
B問題:
簡単だった
クエリ系楽しい
C問題:
Cにしては簡単?
解いている間ずっと体が重いとか外からだとよくわからんことを言っていた
D問題:
普通にわからん
何でUnionFindの準備したんだろ
E問題:
普通にわからん
迷路かな?
F問題
next_permutationを今日使えるようになってTLEしてもWAは出したくなかったが確かめたときに違ったから諦めた

2.結果

変わらなかった

3.解説

A問題 Robot Balance

ロボットが倒れるならH-B足して重くする
そのままでいいなら0
C++での解法

#include <bits/stdc++.h>
using namespace std;

signed main(){
    int H, B;
    cin >> H >> B;
    cout << max(0, H-B) << endl;
    return 0;
}

pythonでの解法

H, B = map(int, input().split())
print(max(0, H-B))

javaでの解法

import java.util.*;

public class Main{
    static Scanner javain = new Scanner(System.in);
    
    public static void main(String[] args){
        int H = javain.nextInt();
        int B = javain.nextInt();
        
        if (H >= B) System.out.println(H-B);
        else System.out.println(0);
    }
}

B問題Robot Weight

付いてるか外れているかを調べてつけてあったら外す、外れていたら付ける

C++での解法

#include <bits/stdc++.h>
using namespace std;

signed main(){
    int X, N, Q;
    cin >> X >> N;
    vector<int> W(N);
    for (int &i : W) cin >> i;
    cin >> Q;
    
    vector<bool> connected(N, false);
    
    while (Q--){
        int x;
        cin >> x;
        x--;
        if (!connected[x]){
            X += W[x];
            connected[x] = true;
        }
        else{
            X -= W[x];
            connected[x] = false;
        }
        cout << X << endl;
    }
}

pythonでの解法

X = int(input())
N = int(input())
W = list(map(int, input().split()))
Q = int(input())
connected = [False]*N

for _ in range(Q):
    x = int(input())-1
    if not connected[x]:
        X += W[x]
        connected[x] = True
    else:
        X -= W[x]
        connected[x] = False
    print(X)

javaでの解法

import java.util.*;

public class Main{
    static Scanner javain = new Scanner(System.in);
    
    public static void main (String[] args){
        int X = javain.nextInt();
        int N = javain.nextInt();
        int[] W = new int[N];
        for (int i = 0; i < N; i++) W[i] = javain.nextInt();
        int Q = javain.nextInt();
        boolean[] connected = new boolean[N];
        for (int i = 0; i < N; i++) connected[i] = false;
        
        for (int _ = 0; _ < Q; _++){
            int x = javain.nextInt()-1;
            if (!connected[x]){
                X += W[x]
                connected[x] = true;
            }
            else{
                X -= W[x]
                connected[x] = false;
            }
            System.out.println(X);
        }
    }
}

C問題 Robot Factory

Kこの頭と体の組み合わせを作るために軽い頭Kこと重い体K子を取ってその中で軽い方(重い方でもOK)から倒れないか見ていく

C++での解法

#include <bits/stdc++.h>
using namespace std;

signed main(){
    int N, M, K;
    cin >> N >> M >> K;
    vector<int> H(N), B(M);
    vector<bool> used(M, false);
    for (int &i : H) cin >> i;
    for (int &i : B) cin >> i;
    sort(H.begin(), H.end());
    sort(B.rbegin(), B.rend());
    while ((int)H.size() > K) H.pop_back();
    while ((int)B.size() > K) B.pop_back();
    reverse(B.begin(), B.end());
    
    for (int i = 0; i < K; i++){
        if (H[i] > B[i]){
            puts("No");
            return 0;
        }
    }
    
    puts("Yes");
    return 0;
}

pythonでの解法

from collections import deque

N, M, K = map(int, input().split())
H = sorted(deque(list(map(int, input().split()))))
B = sorted(deque(list(map(int, input().split()))))

while len(H) > K:
    H.pop()
while len(B) > K:
    B.popleft()

ans = "Yes"
for i in range(K):
    if H[i] > B[i]:
        ans = "No"
        break

print(ans)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?