結果

問題 No.297 カードの数式
ユーザー itezpaceitezpace
提出日時 2016-09-10 10:41:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 3,909 bytes
コンパイル時間 545 ms
コンパイル使用メモリ 70,852 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 22:55:38
合計ジャッジ時間 1,522 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll ary[12];
int main(){
  ll n;
  cin>>n;
  char a;
  ll o=0;
  ll s=0;
  for(ll i=0; i<n; ++i){
    cin>>a;
    if(a=='+'){
      ary[10]+=1;
      o+=1;
    } else if(a=='-'){
      ary[11]+=1;
      o+=1;
    } else{
      ll b=a;
      b-=48;
      ary[b]+=1;
      s+=1;
    }
  }
  ll p=0;
  ll m=0;
  if(ary[10]) p=1;
  if(ary[11]) m=1;
  ll x=0;
  ll y=0;
  vector<ll> v1,v2,v3;
  string s1,s2;
  ll d;
  if(p==1 && m==0){
    ll ary2[12];
    for(ll i=0; i<12; ++i){
      ary2[i]=ary[i];
    }
    while(1){
      if(ary2[10]==0) break;
      ary2[10]-=1;
      for(ll i=0; i<10; ++i){
        if(ary2[i]>0){
          v1.push_back(i);
          ary2[i]-=1;
          break;
        }
      }
    }
    for(ll i=9; i>=0; --i){
      while(1){
        if(ary2[i]==0) break;
        char c=i+'0';
        s1+=c;
        ary2[i]-=1;
      }
    }
    d=stol(s1);
    v2.push_back(d);
    x=v2[0];
    for(ll i=0; i<v1.size(); ++i){
      x+=v1[i];
    }
    for(ll i=0; i<12; ++i){
      ary2[i]=ary[i];
    }
    ll f=s-o;
    for(ll i=0; i<f-1; ++i){
      if(ary2[0]==0) break;
      ary2[0]-=1;
    }
    v1.clear();
    v2.clear();
    ll e=ary[10]+1;
    vector<string> vs(e);
    vector<int> vi;
    for(ll i=0; i<10; ++i){
      for(ll j=0; j<ary2[i]; ++j){
        vi.push_back(i);
      }
    }
    for(ll i=0; i<vi.size(); ++i){
      ll g=i%e;
      char h=vi[i]+'0';
      vs[g]+=h;
    }
    for(ll i=0; i<vs.size(); ++i){
      ll q=stol(vs[i]);
      y+=q;
    }
  } else if(p==0 && m==1){
    ll ary2[12];
    for(ll i=0; i<12; ++i){
      ary2[i]=ary[i];
    }
    while(1){
      if(ary2[11]==0) break;
      ary2[11]-=1;
      for(ll i=0; i<10; ++i){
        if(ary2[i]>0){
          v1.push_back(i);
          ary2[i]-=1;
          break;
        }
      }
    }
    for(ll i=9; i>=0; --i){
      while(1){
        if(ary2[i]==0) break;
        char c=i+'0';
        s1+=c;
        ary2[i]-=1;
      }
    }
    d=stol(s1);
    v2.push_back(d);
    x=v2[0];
    y-=v2[0];
    for(ll i=0; i<v1.size(); ++i){
      x-=v1[i];
      if(i==0){
        y+=v1[i];
      } else {
        y-=v1[i];
      }
    }
  } else if(p==1 && m==1){
    ll ary2[12];
    for(ll i=0; i<12; ++i){
      ary2[i]=ary[i];
    }
    while(1){
      if(ary2[11]==0) break;
      ary2[11]-=1;
      for(ll i=0; i<10; ++i){
        if(ary2[i]>0){
          v1.push_back(i);
          ary2[i]-=1;
          break;
        }
      }
    }
    while(1){
      if(ary2[10]==0) break;
      ary2[10]-=1;
      for(ll i=0; i<10; ++i){
        if(ary2[i]>0){
          v2.push_back(i);
          ary2[i]-=1;
          break;
        }
      }
    }
    for(ll i=9; i>=0; --i){
      while(1){
        if(ary2[i]==0) break;
        char c=i+'0';
        s1+=c;
        ary2[i]-=1;
      }
    }
    d=stol(s1);
    v3.push_back(d);
    x=v3[0];
    for(ll i=0; i<v1.size(); ++i){
      x-=v1[i];
    }
    for(ll i=0; i<v2.size(); ++i){
      x+=v2[i];
    }
    for(ll i=0; i<12; ++i){
      ary2[i]=ary[i];
    }
    v1.clear();
    v2.clear();
    v3.clear();
    while(1){
      if(ary2[10]==-1) break;
      ary2[10]-=1;
      for(ll i=0; i<10; ++i){
        if(ary2[i]>0){
          v1.push_back(i);
          ary2[i]-=1;
          break;
        }
      }
    }
    while(1){
      if(ary2[11]==1) break;
      ary2[11]-=1;
      for(ll i=0; i<10; ++i){
        if(ary2[i]>0){
          v2.push_back(i);
          ary2[i]-=1;
          break;
        }
      }
    }
    for(ll i=9; i>=0; --i){
      while(1){
        if(ary2[i]==0) break;
        char c=i+'0';
        s2+=c;
        ary2[i]-=1;
      }
    }
    d=stol(s2);
    v3.push_back(d);
    y=0;
    for(ll i=0; i<v1.size(); ++i){
      y+=v1[i];
    }
    for(ll i=0; i<v2.size(); ++i){
      y-=v2[i];
    }
    y-=v3[0];
  }
  cout<<x<<" "<<y<<endl;
  return 0;
}
0