답안 #552677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
552677 2022-04-23T14:48:01 Z pokmui9909 Krov (COCI17_krov) C++17
112 / 140
1500 ms 14464 KB
#include <bits/stdc++.h>
#pragma GCC optimization("O3")
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
using ll = long long;
  
ll N;
ll A[100005];
ll B[100005];
ll C[100005];
struct BIT{
    ll T[300015] = {};
    void update(ll k, ll v){while(k <= 300010) T[k] += v, k += k & -k;}
    ll sum(ll k){ll r = 0;while(k >= 1) r += T[k], k -= k & -k; return r;}
    ll query(ll l, ll r) {return sum(r) - sum(l - 1);}
};
BIT pfs, pfc, sfs, sfc;
vector<ll> V;
const ll INF = 1e10;
ll Idx1(ll k){
    return lower_bound(V.begin(), V.end(), k) - V.begin();
}
ll Idx2(ll k){
    return upper_bound(V.begin(), V.end(), k) - V.begin() - 1;
}
  
ll f(ll X, ll K){
    ll r = 0;
    K -= X;
    r += K * pfc.query(Idx1(-INF), Idx2(K)) - pfs.query(Idx1(-INF), Idx2(K));
    r += pfs.query(Idx1(K + 1), Idx2(INF)) - K * pfc.query(Idx1(K + 1), Idx2(INF));
  
    K += 2 * X;
    r += K * sfc.query(Idx1(-INF), Idx2(K)) - sfs.query(Idx1(-INF), Idx2(K));
    r += sfs.query(Idx1(K + 1), Idx2(INF)) - K * sfc.query(Idx1(K + 1), Idx2(INF));
    return r;
}
  
int main(){
    cin.tie(0) -> sync_with_stdio(false);
  
    V.push_back(-INF - 1);
    V.push_back(-INF);
    V.push_back(INF);
    cin >> N;
    for(ll i = 1; i <= N; i++){
        cin >> A[i];
        B[i] = A[i] + i;
        C[i] = A[i] - i;
        V.push_back(A[i]); V.push_back(B[i]); V.push_back(C[i]);
    }
    sort(V.begin(), V.end());
    V.erase(unique(V.begin(), V.end()), V.end());
    ll ans = 1e18;
    for(ll i = 1; i <= N; i++){
        sfs.update(Idx1(B[i]), B[i]);
        sfc.update(Idx1(B[i]), 1);
    }
    for(ll i = 1; i <= N; i++){
        pfs.update(Idx1(C[i]), C[i]);
        pfc.update(Idx1(C[i]), 1);
        sfs.update(Idx1(B[i]), -B[i]);
        sfc.update(Idx1(B[i]), -1);
        ll L = 0, R = 2e9;
        while(R - L > 3){
            ll p = (L + R) / 2, q = p + 1;
            if(f(i, p) <= f(i, q)) R = q;
            else L = p;
        }
        L = max(L, i);
        L = max(L, N + 1 - i);
        for(ll j = L; j <= R; j++){
            ans = min(ans, f(i, j));
        }
    }
    cout << ans;
}

Compilation message

krov.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization("O3")
      | 
krov.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization("Ofast")
      | 
krov.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    4 | #pragma GCC optimization("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 596 KB Output is correct
2 Correct 14 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 628 KB Output is correct
2 Correct 13 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 736 KB Output is correct
2 Correct 21 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 864 KB Output is correct
2 Correct 33 ms 752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 852 KB Output is correct
2 Correct 32 ms 828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 1288 KB Output is correct
2 Correct 72 ms 996 KB Output is correct
3 Correct 34 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 3372 KB Output is correct
2 Correct 413 ms 2656 KB Output is correct
3 Correct 465 ms 3416 KB Output is correct
4 Correct 600 ms 4168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 761 ms 4932 KB Output is correct
2 Correct 836 ms 5716 KB Output is correct
3 Correct 611 ms 5968 KB Output is correct
4 Correct 656 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1319 ms 5868 KB Output is correct
2 Correct 1224 ms 9900 KB Output is correct
3 Correct 688 ms 4592 KB Output is correct
4 Execution timed out 1538 ms 8728 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1583 ms 14464 KB Time limit exceeded
2 Halted 0 ms 0 KB -