公式

A - Cut 解説 by en_translator


For beginners

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] << ' ';
}

投稿日時:
最終更新: