답안 #81605

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
81605 2018-10-25T14:47:21 Z luckyboy Krov (COCI17_krov) C++14
98 / 140
438 ms 11424 KB
/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 100005
#define maxm 500005
#define pii pair <int,int>
#define Task ""
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,a[maxn];
long long ans = 1ll*maxc*maxc;
struct BIT
{
    long long t[maxn];
    void Update(int x,int val)
    {
        while (x <= n)
        {
            t[x] += val;
            x += x & -x;
        }
    }
    long long Get(int x)
    {
        long long res = 0;
        while (x)
        {
            res += t[x];
            x -= x & -x;
        }
        return res;
    }
}l1,l2,r1,r2;
vector <int> L,R;
bool check(int i,int x)
{
    int pos1 = upper_bound(L.begin(),L.end(),x - i) - L.begin(),
        pos2 = upper_bound(R.begin(),R.end(),x + i) - R.begin();
    pos1 = l1.Get(pos1);
    pos2 = r1.Get(pos2);
    return (pos1 + pos2) > (n - n / 2);
}
long long Calc(int i,int x)
{
    long long res = 0;
    int il = upper_bound(L.begin(), L.end(), x - i) - L.begin();
	int ir = upper_bound(R.begin(), R.end(), x + i) - R.begin();
	res += 1ll*(x - i) * l1.Get(il) - l2.Get(il);
	res += (l2.Get(n) - l2.Get(il)) - (1ll*(x - i) * (l1.Get(n) - l1.Get(il)));
	res += 1ll*(x + i) * r1.Get(ir) - r2.Get(ir);
	res += (r2.Get(n) - r2.Get(ir)) - (1ll*(x + i) * (r1.Get(n) - r1.Get(ir)));
	return res;
}
int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen(Task".inp", "r",stdin);
    //freopen(Task".out", "w",stdout);
    read(n);
    FOR(i,1,n)
    {
        read(a[i]);
        L.pb(a[i] - i);
        R.pb(a[i] + i);
    }
    sort(L.begin(),L.end());
    sort(R.begin(),R.end());
    L.resize(unique(L.begin(),L.end()) - L.begin());
    R.resize(unique(R.begin(),R.end()) - R.begin());
    FOR(i,1,n)
    {
        int pos = upper_bound(R.begin(),R.end(),a[i] + i) - R.begin();
        r1.Update(pos,1);
        r2.Update(pos,a[i] + i);
    }
    FOR(i,1,n)
    {
        int pos1 = upper_bound(L.begin(),L.end(),a[i] - i) - L.begin(),
            pos2 = upper_bound(R.begin(),R.end(),a[i] + i) - R.begin();
        l1.Update(pos1,1);
        l2.Update(pos1,a[i] - i);
        r1.Update(pos2,-1);
        r2.Update(pos2,-a[i] - i);
        int l = max(i,n-i+1),r = maxc + maxn * 2;
        while (r - l > 1)
        {
            int mid = (l+r) >> 1;
            if (check(i,mid)) r = mid;
            else l = mid;
        }
        ans = min(ans,Calc(i,r));
        ans = min(ans,Calc(i,l));
    }
    write(ans);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 504 KB Output is correct
2 Correct 5 ms 536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 536 KB Output is correct
2 Correct 5 ms 536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 536 KB Output is correct
2 Correct 7 ms 676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 712 KB Output is correct
2 Correct 11 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 952 KB Output is correct
2 Correct 14 ms 1020 KB Output is correct
3 Correct 9 ms 1020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 2128 KB Output is correct
2 Correct 91 ms 2128 KB Output is correct
3 Correct 108 ms 2436 KB Output is correct
4 Correct 130 ms 2860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 3444 KB Output is correct
2 Correct 190 ms 4080 KB Output is correct
3 Correct 121 ms 4264 KB Output is correct
4 Incorrect 118 ms 4704 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 266 ms 5596 KB Output is correct
2 Incorrect 248 ms 7608 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 364 ms 9632 KB Output is correct
2 Correct 341 ms 10716 KB Output is correct
3 Correct 302 ms 10716 KB Output is correct
4 Correct 438 ms 11424 KB Output is correct
5 Correct 79 ms 11424 KB Output is correct