思路

模拟整个过程就好了,数组循环移位+数组逆序

#include<iostream>
#include<vector>
#include<numeric>

using namespace std;

void shift_flip(vector<int>& cards, int n, int k){
    vector<int> nums = cards;
    // shift
    for(int i = 0; i < n; i ++)
        cards[i] = nums[(i + k) % n];
    // flip
    for(int i = 0; i < n/4; i ++)
        swap(cards[i], cards[n/2 - i - 1]);
}

int main(){
    int n, k;
    while(cin >> n >> k){
        vector<int> cards(n);
        iota(cards.begin(), cards.end(), 1);
        int x;
        for(int i = 0; i < k; i ++){
            cin >> x;
            shift_flip(cards, n, x);
        }
        for(int card : cards)
            cout << card << " ";
        cout << endl;
    }
    return 0;
}

我们来看下另外一种方法 - 经典的三次翻转法,我们可以这么做:

  • 先把[0, x - 1]翻转
  • 然后把[x, n - 1]翻转
  • 最后把[0, n - 1]翻转

img

#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>

using namespace std;

void shift_flip(vector<int>& cards, int n, int k){
    // shift
    reverse(cards.begin(), cards.begin() + k);
    reverse(cards.begin() + k, cards.end());
    reverse(cards.begin(), cards.end());
    // flip
    for(int i = 0; i < n/4; i ++)
        swap(cards[i], cards[n/2 - i - 1]);
}

int main(){
    int n, k;
    while(cin >> n >> k){
        vector<int> cards(n);
        iota(cards.begin(), cards.end(), 1);
        int x;
        for(int i = 0; i < k; i ++){
            cin >> x;
            shift_flip(cards, n, x);
        }
        for(int card : cards)
            cout << card << " ";
        cout << endl;
    }
    return 0;
}