A - Cut 解説 by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
The topmost card after an operation is the \(K\)-th card from the bottom in the original pile. The \(K\)-th bottommost card is the \((N-K+1)\) card from the top, so the cards after the operation have \(A_{N-K+1},A_{N-K+2},\ldots,A_{N},A_{1},A_{2},\ldots\), and \(A_{N-K}\) written on them from the top. You may print these values.
In many programming languages, indices start from \(0\), so beware of indices.
Sample code (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i=0; i<n; i++) cin >> a[i];
for(int i=n-k; i<n; i++) cout << a[i] << ' ';
for(int i=0; i<n-k; i++) cout << a[i] << ' ';
}
Sample code (Python)
N,K = map(int,input().split())
A = list(map(int,input().split()))
for i in range(N-K,N): print(A[i])
for i in range(N-K): print(A[i])
In languages like Python, one can easily achieve the operation of splitting a pile into two and move the lower part to the top, as follows:
Sample code (Python)
N,K = map(int,input().split())
A = list(map(int,input().split()))
top = A[:-K] # upper
bottom = A[-K:] # lower
B = bottom + top # put the lower part to the top
print(*B)
C++ has a function called rotate
, which does exactly the operation described in the problem statement.
Sample code (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i=0; i<n; i++) cin >> a[i];
rotate(a.begin(), a.begin()+n-k, a.end());
for(int i=0; i<n; i++) cout << a[i] << ' ';
}
投稿日時:
最終更新: