公式
A - カードバトル / Card Battle 解説
by
A - カードバトル / Card Battle 解説
by
kyopro_friends
初心者の方へ
- AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
- また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
問題文の指示通りに操作をシミュレーションします。
カードの強さを表す配列を用意し、吸収されて取り除かれたカードに対しては -1 を入れることにします。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int>a(n);
for(int i=0; i<n; i++) cin >> a[i];
for(int i=0; i<n-1; i++){
if(a[i] == -1){
continue;
}
if(a[i] < a[i+1]){
a[i+1] += a[i] / 2;
a[i] = -1;
}else if(a[i] > a[i+1]){
a[i] += a[i+1] / 2;
a[i+1] = -1;
}
}
int ans = 0;
for(int i=0; i<n; i++){
if(a[i] != -1){
ans++;
}
}
cout << ans << endl;
}
実装例 (Python)
N = int(input())
A = list(map(int, input().split()))
for i in range(N-1):
if A[i] == -1:
continue
if A[i] < A[i+1]:
A[i+1] += A[i] // 2
A[i] = -1
elif A[i] > A[i+1]:
A[i] += A[i+1] // 2
A[i+1] = -1
ans = 0
for a in A:
if a != -1:
ans += 1
print(ans)
投稿日時:
最終更新:
