結果

問題 No.2488 Mod Sum Maximization
ユーザー ococonomy1ococonomy1
提出日時 2023-09-29 22:45:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,197 bytes
コンパイル時間 1,665 ms
コンパイル使用メモリ 127,728 KB
実行使用メモリ 813,516 KB
最終ジャッジ日時 2023-09-29 22:46:56
合計ジャッジ時間 27,080 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 48 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 1,292 ms
208,540 KB
testcase_05 WA -
testcase_06 MLE -
testcase_07 WA -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 WA -
testcase_11 MLE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1,285 ms
205,268 KB
testcase_16 MLE -
testcase_17 WA -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 AC 1,304 ms
208,032 KB
testcase_21 MLE -
testcase_22 AC 1,296 ms
208,060 KB
testcase_23 MLE -
testcase_24 WA -
testcase_25 TLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
testcase_35 MLE -
testcase_36 AC 2 ms
4,476 KB
testcase_37 AC 1,567 ms
463,976 KB
testcase_38 MLE -
testcase_39 AC 1,424 ms
200,788 KB
testcase_40 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using lg = long long;
using pii = pair<int, int>;
using pll = pair<lg, lg>;
#define TEST cerr << "TEST" << endl
#define AMARI 998244353
//#define AMARI 1000000007
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1200 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'

// 疑似乱数(XorShift)
unsigned long xor128(void) {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}


#define MULTI_TEST_CASE false
void solve(void) {
    clock_t start = clock();
    int n;
    cin >> n;
    lg ans = 0;
    vector<lg> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(i != n -1)ans += a[i];
    }
    ans += a.back() % a[0];
    lg kizyun = ans;
    vector<pair<lg,int>> v(n,pair(LLONG_MIN / 2,-1));
    for(int i = 1; i < n - 1; i++){
        lg temp = kizyun;
        temp -= a[i - 1] % a[i];
        temp -= a[i] % a[i + 1];
        temp -= a[n - 1] % a[0];

        temp += a[i - 1] % a[i + 1];
        temp += a[n - 1] % a[i];
        temp += a[i] % a[0];
        //cerr << i << ' ' << temp - kizyun << el;
        ans = max(ans,temp);
        v[i] = pair(temp - kizyun,i);
    }
    //sort(v.rbegin(),v.rend());
    priority_queue<pair<lg,vector<int>>> que;
    vector<int> tempvv(1);
    for(int i = 0; i < n; i++){
        tempvv[0] = i;
        que.push(pair(v[i].first,tempvv));
    }
    int cccnt = 0;
    while(!que.empty() && clock() - start < TIME_LIMIT){
        lg val = que.top().first;
        //tempには添え字が入っている
        vector<int> temp = que.top().second;
        que.pop();
        sort(temp.begin(),temp.end());
        vector<int> tempv(n);
        int idx = 0,idx2 = 0;
        for(int i = 0; i < n; i++){
            if(temp[idx] == i){
                idx++;
                continue;
            }
            tempv[idx2] = a[i];
            idx2++;
        }
        lg templl = 0;
        for(int i = 0; i < idx2 - 1; i++){
            templl += tempv[i] % tempv[i + 1];
        }
        do{
            for(int i = idx2; i < n; i++){
                tempv[i] = a[temp[i - idx2]];
            }
            lg templl = 0;
            for(int i = idx2 - 1; i < n - 1; i++){
                templl += tempv[i] % tempv[i + 1];
            }
            templl += tempv.back() % tempv[0];
            ans = max(ans,templl);
            if(temp.size() >= 5)break;
        }while(next_permutation(temp.begin(),temp.end()));
        if(temp.size() == 5)continue;
        sort(temp.begin(),temp.end());
        idx = 0;
        temp.push_back(2 * n);
        for(int i = 0; i < n; i++){
            if(temp[idx] == i){
                idx++;
                continue;
            }
            temp[temp.size() - 1] = i;
            que.push(pair(val + v[i].first,temp));
        }
        cccnt++;
    }
    cout << ans << el;
    return;
}

//a = {6545,13043,22287,82918,182145,245798,266209,334121}
//ans = 821460
//こっちが隣接して後ろに行った方

//8 6545 13043 22287 82918 182145 245798 266209 334121

//a = {7219,12725,36670,85461,108153,188204,285431,376068}
//ans = 726358
//こっちがバラバラに後ろに行った方
//7219 36670 85461 108153 285431 376068 188204 12725
//↑この形で最大を与えるらしい
//8 7219 12725 36670 85461 108153 188204 285431 376068

void calc(void) {
    int n = 8;
    vector<int> a(n);
    
    for(int i = 0; i < n; i++){
        if(i == 0)a[i] = xor128() % 10000 + 1;
        else{
            a[i] = a[i - 1] + min(max((unsigned long)0,xor128() % (xor128() / 10000)),(unsigned long)xor128() % 100000) + 1;
        }
    }
    
    cout << "a = {";
    for(int i = 0; i < n; i++){
        if(i)cout << ',';
        cout << a[i];
    }
    cout << "}\n";
    lg ans = 0;
    vector<int> tempv;
    int cnt = 0;
    do{
        lg temp = 0;
        for(int i = 0; i < n - 1; i++){
            temp += a[i] % a[i + 1];
        }
        temp += a.back() % a[0];
        if(temp > ans){
            ans = temp;
            tempv = a;
            cnt = 0;
        }
        if(temp == ans)cnt++;
    }while(next_permutation(a.begin(),a.end()));
    int cnt2 = 0;
    for(int i = 0; i < n - 1; i++){
        if(tempv[i] > tempv[i + 1])cnt2++;
    }
    if(cnt2 >= 2){
        cout << ans << el;
        cout << cnt << el;
        for(int i = 0; i < n; i++)cout << tempv[i] << ' ';
        cout << el;
        cout << el;
    }

    return;
}

int main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    /*
    for(int i = 0; i < 1000; i++){
        //calc();
    }
    */
    int t = 1;
    if(MULTI_TEST_CASE) cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
0