結果

問題 No.397 NO MORE KADOMATSU
ユーザー maimai
提出日時 2016-07-17 19:22:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,352 bytes
コンパイル時間 2,224 ms
コンパイル使用メモリ 187,140 KB
実行使用メモリ 24,408 KB
平均クエリ数 1.00
最終ジャッジ日時 2023-09-23 11:12:01
合計ジャッジ時間 9,625 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;
#define debugm(m) printf("L%d %s is..\n",__LINE__,#m);for(auto v:m){for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) printf("L%d %s is => ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;
#define debugaa(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<<endl;}
#define ALL(v) (v).begin(),(v).end()
#define BIGINT 0x7FFFFFFF
#define E107 1000000007

template<typename T1,typename T2>
ostream& operator <<(ostream &o,const pair<T1,T2> p){o<<"("<<p.first<<":"<<p.second<<")";return o;}

int odata[101];
int data[101];

inline bool kadomatu(int a,int b,int c){
    return a!=b&&b!=c&&c!=a&&((a<b && c<b)||(a>b && c>b));
}

template<typename TI>
bool allnonkadomaturetu(TI begin,TI end){
    TI it1=begin;
    TI it2=++begin;
    TI it3=++begin;
    for (;it3!=end;it1=it2,it2=it3,it3++){
        if (kadomatu(*it1,*it2,*it3)) return false;
    }
    return true;
}

int main(){
    int i,j;
    int n;

    cin >> n;

    vector<int> order(n);
    for (i=0;i<n;i++){
        order[i]=i+1;
        scanf("%d",odata+i);
    }

    queue<pair<int,int>> result[4]; 	

    for (int tp=0;tp<8;tp++){
        copy(odata,odata+n,data);
        
        // (´・ω・`)
        if (tp/2==0)
            sort(order.begin(),order.end(),[](int l,int r){return data[l-1]<data[r-1];});
        if (tp/2==1)
            sort(order.begin(),order.end(),[](int l,int r){return data[l-1]>data[r-1];});
        if (tp/2==2)
            stable_sort(order.begin(),order.end(),[](int l,int r){return data[l-1]<data[r-1];});
        if (tp/2==3)
            stable_sort(order.begin(),order.end(),[](int l,int r){return data[l-1]>data[r-1];});
    
        vector<int> v(n);
    
        for (i=0;i<n;i++)
            v[order[i]-1]=i;
    
        if (tp%2==0){
            for (i=0;i<n-1;i++){
                for (j=i;j<n;j++){
                    if (v[j]==i){
                        if (i!=j){
                            result[tp].push(make_pair(i,j));
                            swap(v[i],v[j]);
                            swap(data[i],data[j]);
                            if (allnonkadomaturetu(data,data+n)) goto l_superbreak;
                        }
                        break;
                    }
                }
            }
        }
        if (tp%2==1){
            for (i=n-1;0<i;i--){
                for (j=i;0<=j;j--){
                    if (v[j]==i){
                        if (i!=j){
                            result[tp].push(make_pair(i,j));
                            swap(v[i],v[j]);
                            swap(data[i],data[j]);
                            if (allnonkadomaturetu(data,data+n)) goto l_superbreak;
                        }
                        break;
                    }
                }
            }
        }
    l_superbreak:;
    }
    j=0;
    for (i=1;i<4;i++)
        if (result[i].size()<result[j].size()) j=i;


    cout<<result[j].size()<<endl;
    
    while (!result[j].empty()){
        printf("%d %d\n",result[j].front().first,result[j].front().second);
        result[j].pop();
    }
    cout<<endl;
    cin>>n;

    return 0;

}
0