Submission #13210202


Source Code Expand

Copy
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define ALL(name) name.begin(),name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

int N;
vector<int> A;

int func(int val) {
    int ret = 0;
    REP(i, N) {
        ret += abs(A[i] + val);
    }
    return ret;
}


// [lb, ub] の範囲の値を下に凸な func 関数で得られるとき、
// 三分探索で最小解を求める
// func は返り値 ll, 引数ひとつで型 ll の関数を与える
ll tri_search(ll lb, ll ub, function< ll(ll) > func) {
    while(ub - lb > 1) {
        ll t1 = (2 * lb + ub) / 3;
        ll t2 = (lb + 2 * ub) / 3;
        if(func(t1) > func(t2)) {   // 上に凸ならここの不等号を逆転させれば OK
            if(lb == t1) { break; } // もう変化しないくらい範囲が狭くなった
            lb = t1;
        }
        else {
            if(ub == t2) { break; }
            ub = t2;
        }
    }

    // この時点でだいたい ub - lb <= 6 くらいになってるはず
    ll ret = LLINF;                 // 上に凸ならここを -LLINF にする
    for(ll i = lb; i <= ub; ++i) {
        ret = min(ret, func(i));    // 上に凸ならここを max にする
    }
    return ret;
}

signed main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);

    cin >> N;
    A.resize(N);
    int ans = 0;
    REP(i, N) { 
        cin >> A[i];
        A[i] -= (i + 1);
        ans += abs(A[i]);
    }
    if(ans == 0) {
        printf("0\n");
        return 0;
    }

    ans = min(ans, tri_search(-LLINF + 10, LLINF - 10, func));

    printf("%lld\n", ans);

    return 0;
}

Submission Info

Submission Time
Task C - Linear Approximation
User VTR
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2122 Byte
Status WA
Exec Time 62 ms
Memory 1792 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 300
Status
AC × 4
AC × 9
WA × 10
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 256 KB
sample_02.txt AC 1 ms 256 KB
sample_03.txt AC 1 ms 256 KB
sample_04.txt AC 1 ms 256 KB
subtask_1_01.txt AC 1 ms 256 KB
subtask_1_02.txt WA 12 ms 512 KB
subtask_1_03.txt WA 11 ms 512 KB
subtask_1_04.txt WA 31 ms 1024 KB
subtask_1_05.txt WA 54 ms 1664 KB
subtask_1_06.txt WA 60 ms 1792 KB
subtask_1_07.txt WA 62 ms 1792 KB
subtask_1_08.txt WA 62 ms 1792 KB
subtask_1_09.txt WA 62 ms 1792 KB
subtask_1_10.txt WA 62 ms 1792 KB
subtask_1_11.txt WA 62 ms 1792 KB