結果

問題 No.831 都市めぐり
ユーザー Manuel1024
提出日時 2021-12-19 02:24:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,731 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 73,600 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-09-15 14:22:59
合計ジャッジ時間 1,872 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long int;

ll guchoku(int n){
    vector<ll> arr(n);
    for(int i = 0; i < n; i++) arr[i] = i+1;
    ll ans = 1LL << 60;
    do{
        ll res = 0;
        for(int i = 1; i < n; i++){
            res += arr[i]*arr[i-1] + arr[i]-arr[i-1];
        }
        res += arr[0]*arr[n-1] + arr[0]-arr[n-1];
        ans = min(ans, res);
        // for(int i = 0; i < n; i++) cout << arr[i] << " ";
        // cout << res << endl;
    }while(next_permutation(arr.begin(), arr.end()));
    do{
        ll res = 0;
        for(int i = 1; i < n; i++){
            res += arr[i]*arr[i-1] + arr[i]-arr[i-1];
        }
        res += arr[0]*arr[n-1] + arr[0]-arr[n-1];
        if(res == ans){
            for(int i = 0; i < n; i++) cout << arr[i] << " ";
            cout << res << endl;
        }
    }while(next_permutation(arr.begin(), arr.end()));
    return ans;
}

ll solve(int n){
    if(n == 1) return 0;
    if(n == 2) return 4;
    if(n%2 == 1){
        vector<ll> b(n/2);
        b[0] = 10;
        for(int i = 1; i < b.size(); i++){
            b[i] = b[i-1] + 8*(i+1);
        }
        ll ans = 1;
        // for(int i = 0; i < b.size(); i++) cout << b[i] << " ";
        for(auto &it: b) ans += it;
        return ans;
    }else{
        vector<ll> b(n/2-1);
        b[0] = 17;
        for(int i = 1; i < b.size(); i++){
            b[i] = b[i-1] + 8*i+12;
        }
        ll ans = 4;
        // for(int i = 0; i < b.size(); i++) cout << b[i] << " ";
        for(auto &it: b) ans += it;
        return ans;
    }
}

int main(){
    int n;
    cin >> n;
    cout << solve(n) << endl;
    // cout << guchoku(n) << endl;
    return 0;
}
0