結果

問題 No.297 カードの数式
ユーザー 0w10w1
提出日時 2016-11-29 17:24:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,479 bytes
コンパイル時間 1,517 ms
コンパイル使用メモリ 173,020 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 22:56:10
合計ジャッジ時間 2,589 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

typedef long long ll;

ll get_max( const vector< int > &dig, int st ){
  ll res = 0;
  for( int i = dig.size() - 1; i >= st; --i )
    res = res * 10 + dig[ i ];
  return res;
}

signed main(){
  int N; cin >> N;
  vector< string > C( N );
  for( int i = 0; i < N; ++i )
    cin >> C[ i ];
  vector< int > dig;
  int pcnt = 0, mcnt = 0;
  for( int i = 0; i < N; ++i ){
    pcnt += C[ i ][ 0 ] == '+';
    mcnt += C[ i ][ 0 ] == '-';
    if( '0' <= C[ i ][ 0 ] and C[ i ][ 0 ] <= '9' )
      dig.emplace_back( C[ i ][ 0 ] - '0' );
  }
  // for( int i = 0; i < dig.size(); ++i ) cout << dig[ i ] << " \n"[ i + 1 == dig.size() ];
  ll ans1 = 0, ans2 = 0;
  sort( dig.begin(), dig.end() );
  int ptr = 0;
  for( int i = 0; i < mcnt; ++i )
    ans1 -= dig[ ptr++ ];
  for( int i = 0; i < pcnt; ++i )
    ans1 += dig[ ptr++ ];
  ans1 += get_max( dig, ptr );
  if( mcnt ){
    ptr = 0;
    for( int i = 0; i < pcnt + 1; ++i )
      ans2 += dig[ ptr++ ];
    for( int i = 0; i < mcnt - 1; ++i )
      ans2 -= dig[ ptr++ ];
    ans2 -= get_max( dig, ptr );
  } else{
    ptr = dig.size() - 1;
    int len = ( dig.size() + pcnt ) / ( pcnt + 1 );
    ll pw = 1;
    for( int i = 0; i < len; ++i, pw *= 10 ){
      int x = pcnt + ( not ( ( ptr + 1 ) / ( len - i ) == pcnt and ( ptr + 1 ) % ( len - i ) == 0 ) );
      for( int j = 0; j < x; ++j )
        ans2 += pw * dig[ ptr-- ];
    }
  }
  cout << ans1 << " " << ans2 << endl;
  return 0;
}
0