답안 #116987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
116987 2019-06-14T11:00:17 Z evpipis Gap (APIO16_gap) C++17
100 / 100
62 ms 2460 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
typedef long long ll;

const int len = 1e5+5;
const ll maxi = 1e18;

//#define TEST

#ifndef TEST
#include "gap.h"
#endif // TEST

#ifdef TEST
static void my_assert(int k){ if (!k) exit(1); }

static int subtask_num, N;
static long long A[100001];
static long long call_count;

long long findGap(int, int);

void MinMax(long long s, long long t, long long *mn, long long *mx)
{
	int lo = 1, hi = N, left = N+1, right = 0;
	my_assert(s <= t && mn != NULL && mx != NULL);
	while (lo <= hi){
		int mid = (lo+hi)>>1;
		if (A[mid] >= s) hi = mid - 1, left = mid;
		else lo = mid + 1;
	}
	lo = 1, hi = N;
	while (lo <= hi){
		int mid = (lo+hi)>>1;
		if (A[mid] <= t) lo = mid + 1, right = mid;
		else hi = mid - 1;
	}
	if (left > right) *mn = *mx = -1;
	else{
		*mn = A[left];
		*mx = A[right];
	}
	if (subtask_num == 1) call_count++;
	else if (subtask_num == 2) call_count += right-left+2;
}

int main()
{
	FILE *in = stdin, *out = stdout;
	my_assert(2 == fscanf(in, "%d%d", &subtask_num, &N));
	my_assert(1 <= subtask_num && subtask_num <= 2);
	my_assert(2 <= N && N <= 100000);
	for (int i=1;i<=N;i++) my_assert(1 == fscanf(in, "%lld", A+i));
	for (int i=1;i<N;i++) my_assert(A[i] < A[i+1]);

	fprintf(out, "%lld\n", findGap(subtask_num, N));
	fprintf(out, "%lld\n", call_count);
}
#endif

long long findGap(int t, int n){
    ll ans = 0, l = 0, r = maxi, mn, mx;

    if (t == 1){
        vector<ll> vec;

        while (l <= r && vec.size() != n){
            MinMax(l, r, &mn, &mx);
            if (mn == mx){
                if (mn != -1)
                    vec.pb(mn);
                break;
            }

            l = mn+1, r = mx-1;
            vec.pb(mn), vec.pb(mx);
        }

        sort(vec.begin(), vec.end());

        for (int i = 0; i+1 < vec.size(); i++)
            ans = max(ans, vec[i+1]-vec[i]);
    }
    else{
        ll mi, mx; MinMax(0, maxi, &mi, &mx);
        ll L, R, len = mx - mi + 1;
        ll blok = len / n;
        ll lst = mi;
        ans = blok;
        for(ll i = mi + 1;i < mx;){
            MinMax(i, i + ans - 1, &L, &R); i += ans;
            if(L != -1){
                ans = max(ans, L - lst);
                lst = R;
            }
        }
        ans = max(ans, mx - lst);
        return ans;
        /*MinMax(l, r, &mn, &mx);

        ll low = (mx-mn)/(n-1), pre = mn, up = mx;

        //printf("mn = %lld, mx = %lld, l = %lld, r = %lld\n", mn, mx, l, r);

        l = mn+1;
        while (l <= up){
            MinMax(l, l+low-1, &mn, &mx);

            //printf("from %lld to %lld, mn = %lld, mx = %lld\n", l, l+low-1, mn, mx);

            if (mn != -1){
                ans = max(ans, mn-pre);
                pre = mx;
            }

            l += low;
        }*/
    }

    //printf("ans = %lld\n", ans);
    return ans;
}

Compilation message

gap.cpp: In function 'long long int findGap(int, int)':
gap.cpp:69:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (l <= r && vec.size() != n){
                          ~~~~~~~~~~~^~~~
gap.cpp:83:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i+1 < vec.size(); i++)
                         ~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 15 ms 896 KB Output is correct
17 Correct 15 ms 896 KB Output is correct
18 Correct 14 ms 1024 KB Output is correct
19 Correct 18 ms 896 KB Output is correct
20 Correct 10 ms 896 KB Output is correct
21 Correct 62 ms 2416 KB Output is correct
22 Correct 53 ms 2288 KB Output is correct
23 Correct 50 ms 2288 KB Output is correct
24 Correct 52 ms 2288 KB Output is correct
25 Correct 52 ms 2288 KB Output is correct
26 Correct 50 ms 2460 KB Output is correct
27 Correct 50 ms 2292 KB Output is correct
28 Correct 50 ms 2288 KB Output is correct
29 Correct 50 ms 2288 KB Output is correct
30 Correct 42 ms 2296 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 9 ms 512 KB Output is correct
17 Correct 9 ms 512 KB Output is correct
18 Correct 9 ms 512 KB Output is correct
19 Correct 9 ms 640 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 29 ms 1152 KB Output is correct
22 Correct 29 ms 1152 KB Output is correct
23 Correct 29 ms 1152 KB Output is correct
24 Correct 29 ms 1152 KB Output is correct
25 Correct 54 ms 1152 KB Output is correct
26 Correct 29 ms 1144 KB Output is correct
27 Correct 34 ms 1144 KB Output is correct
28 Correct 30 ms 1152 KB Output is correct
29 Correct 30 ms 1152 KB Output is correct
30 Correct 23 ms 1152 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct