Official
B - 宝石の分配 / Distribution of Jewels Editorial
by
B - 宝石の分配 / Distribution of Jewels Editorial
by
kyopro_friends
問題文の指示通り、高橋君と青木君のそれぞれの持つ宝石の価値の合計を管理しながら宝石の分配をシミュレーションすることで解けます。
言語によってはオーバーフローに注意してください。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
long long k;
cin >> n >> k;
vector<int>a(n);
for(int i=0; i<n; i++) cin >> a[i];
long long takahashi = 0, aoki = 0;
for(int i=0; i<n; i++){
if(takahashi + a[i] <= k){
takahashi += a[i];
}else{
aoki += a[i];
}
}
if(takahashi > aoki){
cout << "Takahashi" << endl;
}else if(takahashi < aoki){
cout << "Aoki" << endl;
}else{
cout << "Draw" << endl;
}
}
実装例 (Python)
N, K = map(int, input().split())
A = list(map(int, input().split()))
takahashi, aoki = 0, 0
for a in A:
if takahashi + a <= K:
takahashi += a
else:
aoki += a
if takahashi > aoki:
print("Takahashi")
elif takahashi < aoki:
print("Aoki")
else:
print("Draw")
posted:
last update:
