結果

問題 No.297 カードの数式
ユーザー imulanimulan
提出日時 2016-06-20 03:21:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,594 bytes
コンパイル時間 1,443 ms
コンパイル使用メモリ 153,536 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 22:55:23
合計ジャッジ時間 2,478 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 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;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int main()
{
    int n;
    cin >>n;

    vector<int> num;
    //plus,minus
    int p=1,m=0;

    rep(i,n)
    {
        char c;
        scanf(" %c", &c);
        if(c=='+') ++p;
        else if(c=='-') ++m;
        else num.pb(c-'0');
    }
    sort(all(num));

    //作らなきゃいけない数字の個数
    int a=p+m;
    //今持っている数字カードの個数
    int b=num.size();

    //最終的に持つ数字のリスト
    vector<ll> d;

    //小さい方からa-1個取り出す
    rep(i,a-1) d.pb(num[i]);
    //大きい方から取り出してくっつける
    ll add=0;
    for(int i=b-1; i>=a-1; --i)
    {
        add=add*10+num[i];
    }
    d.pb(add);

    //max,min
    ll x=0, y=0;

    //maxの計算
    //小さい方からm個をマイナス
    rep(i,m) x-=d[i];
    for(int i=m; i<p+m; ++i) x+=d[i];

    //minの計算
    if(m>0)
    {
        //小さい方からp個をプラス
        rep(i,p) y+=d[i];
        for(int i=p; i<p+m; ++i) y-=d[i];
    }
    else
    {
        //プラスしか無い時
        //p個に割り振っていく
        vector<int> el(p);
        rep(i,b)
        {
            el[i%p]=10*el[i%p]+num[i];
        }

        rep(i,p) y+=el[i];
    }

    cout << x << " " << y << endl;
    return 0;
}
0