結果

問題 No.930 数列圧縮
ユーザー face4face4
提出日時 2019-11-22 22:54:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,849 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 76,456 KB
実行使用メモリ 17,868 KB
最終ジャッジ日時 2024-04-19 12:17:04
合計ジャッジ時間 4,992 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 33 ms
7,712 KB
testcase_14 AC 42 ms
11,768 KB
testcase_15 AC 36 ms
6,944 KB
testcase_16 AC 45 ms
8,404 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 43 ms
8,400 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

struct Node{
    int x;
    Node* right;
    Node* left;
};

Node* newNode(int val){
    Node* t = (Node*)(malloc(sizeof(Node)));
    t->x = val;
    t->left = NULL;
    t->right = NULL;
    return t;
}

int main(){
    int n;
    cin >> n;
    vector<int> v(n);
    for(int i = 0; i < n; i++)  cin >> v[i];
    if(v[0] == n){
        cout << "No" << endl;
        return 0;
    }
    if(v.back() == n){
        cout << "Yes" << endl;
        for(int i = n-2; i >= 0; i--)   cout << v[i] << " \n"[i==0];
        return 0;
    }
    for(int i = 0; i < n; i++)  v[i]--;
    vector<Node*> vn;
    for(int i = 0; i < n; i++){
        vn.push_back(newNode(i));
    }
    for(int i = 0; i+1 < n; i++){
        vn[v[i]]->right = vn[v[i+1]];
    }
    for(int i = 1; i < n; i++){
        vn[v[i]]->left = vn[v[i-1]];
    }
    vector<int> ans;
    vector<bool> used(n, 0);
    for(int i = 0; i < n-1; i++){
        if(vn[i]->right == NULL){
            cout << "No" << endl;
            return 0;
        }
        while(vn[i]->right->right != NULL){
            used[vn[i]->right->x] = true;
            ans.push_back(vn[i]->right->x);
            vn[i]->right->right->left = vn[i];
            vn[i]->right = vn[i]->right->right;
        }
        used[i] = true;
        ans.push_back(i);
        if(ans.size() == n-2){
            for(int j = 0; j < n; j++){
                if(!used[j]){
                    ans.push_back(j);
                    break;
                }
            }
        }else if(ans.size() == n-1){
            break;
        }
        vn[i]->right->left = vn[i]->left;
        if(vn[i]->left != NULL)  vn[i]->left->right = vn[i]->right;
    }
    cout << "Yes" << endl;
    for(int i = 0; i < n-1; i++){
        cout << ans[i]+1 << " \n"[i+1==n-1];
    }
    return 0;
}
0