답안 #951511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951511 2024-03-22T04:19:04 Z kilkuwu Xylophone (JOI18_xylophone) C++17
100 / 100
48 ms 744 KB
#include <bits/stdc++.h>

#ifndef LOCAL
#include "xylophone.h"
#else 
void solve(int N);
int query(int s, int t);
void answer(int i, int a);

static const int __N_MAX = 5000;
static const int __Q_MAX = 10000;
static int __N;
static int __A[__N_MAX];
static bool __used[__N_MAX];
static int __query_c;
static int __answer_c;

int query(int s, int t) {
	if(__query_c >= __Q_MAX) {
		printf("Wrong Answer\n");
		exit(0);
	}
	__query_c++;
	if(!(1 <= s && s <= t && t <= __N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	int mx = 0, mn = __N + 1;
	for(int i = s - 1; i < t; i++) {
		if(mx < __A[i]) {
			mx = __A[i];
		}
		if(mn > __A[i]) {
			mn = __A[i];
		}
	}
	return mx - mn;
}

void answer(int i, int a) {
	__answer_c++;
	if(!(1 <= i && i <= __N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	if(!(1 <= a && a <= __N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	if(__used[i - 1]) {
		printf("Wrong Answer\n");
		exit(0);
	}
	if(a != __A[i - 1]) {
		printf("Wrong Answer\n");
		exit(0);
	}
	__used[i - 1] = true;
}

int main() {
	__query_c = 0;
	__answer_c = 0;
	if(scanf("%d", &__N) != 1) {
		fprintf(stderr, "Error while reading input\n");
		exit(1);
	}
	for(int i = 0; i < __N; i++) {
		if(scanf("%d", __A + i) != 1) {
			fprintf(stderr, "Error while reading input\n");
			exit(1);
		}
		__used[i] = false;
	}
	solve(__N);
	if(!(__answer_c == __N)) {
		printf("Wrong Answer\n");
		exit(0);
	}
	printf("Accepted : %d\n", __query_c);
}

#endif

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

void solve(int N) {
  int lb = 1, rb = N;
  while (lb < rb) {
    int mid = (lb + rb + 1) / 2;
    if (query(mid, N) != N - 1) {
      rb = mid - 1;
    } else {
      lb = mid;
    }
  }

  // we know A[lb] == 1

  // we will know lb + 1
  
  std::vector<int> filled(N + 1, 0);
  std::vector<int> A(N + 1), D(N + 1, -1);
  A[lb] = 1;
  filled[1] = 1;

  if (lb + 1 <= N) {
    D[lb + 1] = query(lb, lb + 1);
    A[lb + 1] = A[lb] + D[lb + 1];
    filled[A[lb + 1]] = 1;
  }

  if (lb - 1 >= 1) {
    D[lb - 1] = query(lb - 1, lb);
    A[lb - 1] = A[lb] + D[lb - 1];
    filled[A[lb - 1]] = 1;
  }

  for (int L = lb - 2; L >= 1; L--) {
    D[L] = query(L, L + 1);
    A[L] = A[L + 1];
    int vp = A[L] + D[L];
    int vm = A[L] - D[L];
    
    if (vp > N || filled[vp]) {
      A[L] = vm;
    } else if (vm < 1 || filled[vm]) {
      A[L] = vp;
    } else {
      int tie = query(L, L + 2);
      if (tie == D[L] + D[L + 1]) {
        A[L] = (A[L + 1] < A[L + 2]) ? vm : vp; 
      } else {
        A[L] = (A[L + 1] < A[L + 2]) ? vp : vm;
      }
    }

    filled[A[L]] = 1;
  }

  for (int R = lb + 2; R <= N; R++) {
    D[R] = query(R - 1, R);
    A[R] = A[R - 1];

    int vp = A[R] + D[R];
    int vm = A[R] - D[R];

    if (vp > N || filled[vp]) {
      A[R] = vm;
    } else if (vm < 1 || filled[vm]) {
      A[R] = vp;
    } else {
      int tie = query(R - 2, R);
      if (tie == D[R] + D[R - 1]) {
        A[R] = (A[R - 2] < A[R - 1]) ? vp : vm;
      } else {
        A[R] = (A[R - 2] < A[R - 1]) ? vm : vp;
      }
    }

    filled[A[R]] = 1;
  }

  for (int i = 1; i <= N; i++) answer(i, A[i]);
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 688 KB Output is correct
17 Correct 3 ms 440 KB Output is correct
18 Correct 4 ms 436 KB Output is correct
19 Correct 7 ms 448 KB Output is correct
20 Correct 6 ms 692 KB Output is correct
21 Correct 7 ms 436 KB Output is correct
22 Correct 5 ms 440 KB Output is correct
23 Correct 6 ms 692 KB Output is correct
24 Correct 5 ms 444 KB Output is correct
25 Correct 4 ms 428 KB Output is correct
26 Correct 6 ms 696 KB Output is correct
27 Correct 6 ms 436 KB Output is correct
28 Correct 5 ms 696 KB Output is correct
29 Correct 5 ms 692 KB Output is correct
30 Correct 7 ms 444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 688 KB Output is correct
17 Correct 3 ms 440 KB Output is correct
18 Correct 4 ms 436 KB Output is correct
19 Correct 7 ms 448 KB Output is correct
20 Correct 6 ms 692 KB Output is correct
21 Correct 7 ms 436 KB Output is correct
22 Correct 5 ms 440 KB Output is correct
23 Correct 6 ms 692 KB Output is correct
24 Correct 5 ms 444 KB Output is correct
25 Correct 4 ms 428 KB Output is correct
26 Correct 6 ms 696 KB Output is correct
27 Correct 6 ms 436 KB Output is correct
28 Correct 5 ms 696 KB Output is correct
29 Correct 5 ms 692 KB Output is correct
30 Correct 7 ms 444 KB Output is correct
31 Correct 12 ms 692 KB Output is correct
32 Correct 17 ms 472 KB Output is correct
33 Correct 31 ms 468 KB Output is correct
34 Correct 33 ms 688 KB Output is correct
35 Correct 24 ms 472 KB Output is correct
36 Correct 22 ms 680 KB Output is correct
37 Correct 26 ms 472 KB Output is correct
38 Correct 25 ms 488 KB Output is correct
39 Correct 29 ms 472 KB Output is correct
40 Correct 26 ms 468 KB Output is correct
41 Correct 35 ms 544 KB Output is correct
42 Correct 21 ms 468 KB Output is correct
43 Correct 25 ms 468 KB Output is correct
44 Correct 22 ms 728 KB Output is correct
45 Correct 32 ms 468 KB Output is correct
46 Correct 32 ms 472 KB Output is correct
47 Correct 29 ms 688 KB Output is correct
48 Correct 37 ms 712 KB Output is correct
49 Correct 33 ms 704 KB Output is correct
50 Correct 32 ms 492 KB Output is correct
51 Correct 31 ms 744 KB Output is correct
52 Correct 29 ms 432 KB Output is correct
53 Correct 28 ms 476 KB Output is correct
54 Correct 31 ms 484 KB Output is correct
55 Correct 31 ms 484 KB Output is correct
56 Correct 34 ms 472 KB Output is correct
57 Correct 28 ms 472 KB Output is correct
58 Correct 30 ms 544 KB Output is correct
59 Correct 28 ms 532 KB Output is correct
60 Correct 48 ms 472 KB Output is correct