Submission #2769356


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 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 int MOD = 1000000007;
#define MAX_N 200010

ll N;
ll A[MAX_N] = {};

ll func(ll n) {
    ll res = 0;
    REP(i, N) {
        res += abs(A[i] - n);
    }
    return res;
}

signed main()
{
    cin >> N;
    REP(i, N) {
        cin >> A[i];
        A[i] = A[i] - (i + 1);
    }
    ll ans = LLINF;
    ll lb = -1000000000;
    ll ub = 1000000000;
    while(1) {
        ll mid = lb + (ub - lb) / 2;
        ll cand1 = func(mid+1);
        ll cand2 = func(mid);
        ll cand3 = func(mid-1);
        if(cand2 <= cand1 && cand2 <= cand3) {
            ans = cand2;
            break;
        }
        else if(cand1 <= cand2 && cand1 <= cand3) {
            lb = mid;
        }
        else if(cand3 <= cand2 && cand3 <= cand1) {
            ub = mid;
        }
    }
    printf("%lld\n", ans);
    return 0;
}

Submission Info

Submission Time
Task C - Linear Approximation
User VTR
Language C++14 (GCC 5.4.1)
Score 300
Code Size 1281 Byte
Status AC
Exec Time 97 ms
Memory 1792 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 4
AC × 19
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 AC 17 ms 512 KB
subtask_1_03.txt AC 16 ms 512 KB
subtask_1_04.txt AC 47 ms 1024 KB
subtask_1_05.txt AC 82 ms 1664 KB
subtask_1_06.txt AC 94 ms 1792 KB
subtask_1_07.txt AC 89 ms 1792 KB
subtask_1_08.txt AC 86 ms 1792 KB
subtask_1_09.txt AC 92 ms 1792 KB
subtask_1_10.txt AC 95 ms 1792 KB
subtask_1_11.txt AC 97 ms 1792 KB