結果

問題 No.2519 Coins in Array
ユーザー Nzt3Nzt3
提出日時 2023-10-27 22:36:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,869 bytes
コンパイル時間 2,507 ms
コンパイル使用メモリ 216,052 KB
実行使用メモリ 6,004 KB
最終ジャッジ日時 2023-10-27 22:36:55
合計ジャッジ時間 6,635 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 36 ms
5,916 KB
testcase_24 AC 36 ms
5,916 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 37 ms
5,920 KB
testcase_28 AC 37 ms
5,916 KB
testcase_29 AC 37 ms
6,004 KB
testcase_30 AC 34 ms
5,768 KB
testcase_31 AC 8 ms
4,348 KB
testcase_32 AC 19 ms
4,612 KB
testcase_33 AC 25 ms
4,348 KB
testcase_34 AC 14 ms
4,348 KB
testcase_35 AC 23 ms
4,348 KB
testcase_36 AC 12 ms
4,348 KB
testcase_37 AC 6 ms
4,348 KB
testcase_38 AC 19 ms
4,348 KB
testcase_39 AC 12 ms
4,348 KB
testcase_40 AC 29 ms
5,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;

pair<ll,vector<array<int,2>>>dfs(vector<ll> v){
  assert(!v.empty());
  if(v.size()==1){
    return make_pair(v[0],vector<array<int,2>>());
  }
  int N=v.size();
  vector<array<int,2>>ret;
  ll m=1e18;
  for(int i=0;i<N;i++){
    for(int j=i+1;j<N;j++){
      vector v2(0,0ll);
      for(int k=0;k<N;k++){
        if(k!=i&&k!=j)v2.push_back(v[k]);
      }
      if(gcd(v[i],v[j])!=1)v2.push_back(0);
      else v2.push_back(v[i]*v[j]-v[i]-v[j]+1);
      auto [n,rv]=dfs(v2);
      if(n<m){
        m=n;
        rv.push_back({i+1,j+1});
        ret=rv;
      }
    }
  }
  return make_pair(m,ret);
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin>>N;
  vector A(N,0ll);
  int idx1=-1;
  for(int i=0;i<N;i++){
    cin>>A[i];
    if(A[i]==1){
      idx1=i;
    }
  }
  if(idx1==-1){
    if(N<4){
      auto [m,v]=dfs(A);
      reverse(v.begin(),v.end());
      cout<<m<<'\n';
      for(auto i:v)cout<<i[0]<<' '<<i[1]<<'\n';
    }else{
      cout<<"0\n";
      vector odd(0,0),even(0,0);
      for(int i=0;i<N;i++){
        if(A[i]%2){
          odd.push_back(i);
        }else{
          even.push_back(i);
        }
      }
      while(even.size()<2){
        int k1=odd.back();
        odd.pop_back();
        int k2=odd.back();
        odd.pop_back();
        for(int &i:even){
          if(i>k1)i-=1;
        }
        for(int &i:even){
          if(i>k2)i-=1;
        }
        even.push_back(odd.size()+even.size());
        cout<<k1+1<<' '<<k2+1<<'\n';
      }
      cout<<even[0]+1<<' '<<even[1]+1<<'\n';
      for(int i=odd.size()+even.size()-1;i>1;i--){
        cout<<i<<" 1\n";
      }
    }
  }else{
    cout<<"0\n";
    if(idx1==0){
      cout<<"1 2\n";
    }else{
      cout<<idx1+1<<" 1\n";
    }
    for(int i=N-1;i>1;i--){
      cout<<i<<" 1\n";
    }
  }
}
0