結果

問題 No.1374 Absolute Game
ユーザー seiyuseiyu
提出日時 2021-02-05 23:17:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,019 bytes
コンパイル時間 2,145 ms
コンパイル使用メモリ 184,164 KB
実行使用メモリ 10,660 KB
最終ジャッジ日時 2023-09-15 10:48:54
合計ジャッジ時間 4,974 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 39 ms
7,000 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 26 ms
5,904 KB
testcase_21 AC 36 ms
6,756 KB
testcase_22 AC 74 ms
10,288 KB
testcase_23 AC 8 ms
4,380 KB
testcase_24 AC 11 ms
4,380 KB
testcase_25 AC 13 ms
4,436 KB
testcase_26 AC 23 ms
5,312 KB
testcase_27 AC 32 ms
6,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

__attribute__ ((constructor))
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
}

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

    vector<int> c(n);
    vector<pair<int,int>> plus_sort;
    vector<pair<int,int>> a;
    vector<pair<int,int>> minus_sort;

    rep(i, n) cin >> c[i];

    if(n == 1){
        cout << abs(c[0]) << endl;
        return 0;
    }

    rep(i, n) { 
        if(c[i] < 0){
            a.push_back(make_pair(abs(c[i]), -1)); 
        }else{
            a.push_back(make_pair(abs(c[i]), 1)); 
        }
    }
    rep(i, n) { plus_sort.push_back(make_pair(c[i], i)); }
    rep(i, n) { minus_sort.push_back(make_pair(c[i], i)); }

    sort(plus_sort.rbegin(), plus_sort.rend());
    sort(minus_sort.begin(), minus_sort.end());

    int plus_index = 0;
    int minus_index = 0;

    sort(a.rbegin(), a.rend());

    bool a_plus = (a[0].second == 1);
    bool b_plus = (a[1].second == 1);

    ll a_sum = 0;
    ll b_sum = 0;

    set<int> already;

    rep(i, n){
        if(i%2 == 0){
            // Aの手番
            if(a_plus){
                while(already.find(plus_sort[plus_index].second) != already.end())
                    plus_index;
                a_sum += plus_sort[plus_index].first;
                already.insert(plus_sort[plus_index].second);
                // already.insert(plus_index);
                plus_index++;
            }else{
                while(already.find(minus_sort[minus_index].second) != already.end())
                    minus_index;
                a_sum += minus_sort[minus_index].first;
                // already.insert(minus_index);
                already.insert(minus_sort[minus_index].second);
                minus_index++;
            }
        }else{
            // Bの手番
            if(b_plus){
                while(already.find(plus_sort[plus_index].second) != already.end())
                    plus_index;
                b_sum += plus_sort[plus_index].first;
                already.insert(plus_sort[plus_index].second);
                plus_index++;
            }else{
                while(already.find(minus_sort[minus_index].second) != already.end())
                    minus_index;
                b_sum += minus_sort[minus_index].first;
                already.insert(minus_sort[minus_index].second);
                minus_index++;
            }
        }
    }

    cout << abs(a_sum) - abs(b_sum) << endl;

    return 0;
}
0