답안 #951516

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
951516 2024-03-22T04:40:23 Z kilkuwu Xylophone (JOI18_xylophone) C++17
100 / 100
94 ms 768 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) {
  std::vector<int> D1(N + 1);
  std::vector<int> D2(N + 1);
  
  for (int i = 2; i <= N; i++) {
    D1[i] = query(i - 1, i);
  }
  for (int i = 3; i <= N; i++) {
    D2[i] = query(i - 2, i);
  }
  std::vector<int> ans(N + 1);

  auto try_out = [&](int lb, std::vector<int>& A) -> bool {
    dbg(lb);
    std::vector<int> filled(N + 1, 0);
    A[lb] = 1;
    filled[1] = 1;

    auto in_range = [&](int x) {
      return 1 <= x && x <= N && !filled[x];
    };

    if (lb + 1 <= N) {
      A[lb + 1] = A[lb] + D1[lb + 1];
      if (!in_range(A[lb + 1])) return false;
      filled[A[lb + 1]] = 1;
    }

    if (lb - 1 >= 1) {
      A[lb - 1] = A[lb] + D1[lb];
      if (!in_range(A[lb - 1])) return false;
      filled[A[lb - 1]] = 1;
    }
    dbg(A);

    for (int L = lb - 2; L >= 1; L--) {
      A[L] = A[L + 1];
      int vp = A[L] + D1[L + 1];
      int vm = A[L] - D1[L + 1];

      if (vp > N || filled[vp]) {
        A[L] = vm;
      } else if (vm < 1 || filled[vm]) {
        A[L] = vp;
      } else {
        int tie = D2[L + 2];
        if (tie == D1[L + 1] + D1[L + 2]) {
          A[L] = (A[L + 1] < A[L + 2]) ? vm : vp; 
        } else {
          A[L] = (A[L + 1] < A[L + 2]) ? vp : vm;
        }
      }

      if (!in_range(A[L])) return false;
      filled[A[L]] = 1;
    }
    for (int R = lb + 2; R <= N; R++) {
      A[R] = A[R - 1];

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

      if (vp > N || filled[vp]) {
        A[R] = vm;
      } else if (vm < 1 || filled[vm]) {
        A[R] = vp;
      } else {
        int tie = D2[R];
        if (tie == D1[R] + D1[R - 1]) {
          A[R] = (A[R - 2] < A[R - 1]) ? vp : vm;
        } else {
          A[R] = (A[R - 2] < A[R - 1]) ? vm : vp;
        }
      }
      
      if (!in_range(A[R])) return false;
      

      filled[A[R]] = 1;
    }
    dbg(A);

    return true;
  };

  for (int lb = 1; lb <= N; lb++) {
    std::vector<int> A(N + 1);

    if (try_out(lb, A)) {
      ans = A;
      break;
    }

  }

  for (int i = 1; i <= N; i++) {
    answer(i, ans[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 340 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 2 ms 344 KB Output is correct
13 Correct 2 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 340 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 2 ms 344 KB Output is correct
13 Correct 2 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 2 ms 436 KB Output is correct
17 Correct 5 ms 440 KB Output is correct
18 Correct 10 ms 436 KB Output is correct
19 Correct 10 ms 444 KB Output is correct
20 Correct 11 ms 444 KB Output is correct
21 Correct 7 ms 444 KB Output is correct
22 Correct 10 ms 428 KB Output is correct
23 Correct 10 ms 444 KB Output is correct
24 Correct 9 ms 444 KB Output is correct
25 Correct 12 ms 420 KB Output is correct
26 Correct 8 ms 444 KB Output is correct
27 Correct 11 ms 440 KB Output is correct
28 Correct 9 ms 440 KB Output is correct
29 Correct 11 ms 444 KB Output is correct
30 Correct 8 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 340 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 2 ms 344 KB Output is correct
13 Correct 2 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 2 ms 436 KB Output is correct
17 Correct 5 ms 440 KB Output is correct
18 Correct 10 ms 436 KB Output is correct
19 Correct 10 ms 444 KB Output is correct
20 Correct 11 ms 444 KB Output is correct
21 Correct 7 ms 444 KB Output is correct
22 Correct 10 ms 428 KB Output is correct
23 Correct 10 ms 444 KB Output is correct
24 Correct 9 ms 444 KB Output is correct
25 Correct 12 ms 420 KB Output is correct
26 Correct 8 ms 444 KB Output is correct
27 Correct 11 ms 440 KB Output is correct
28 Correct 9 ms 440 KB Output is correct
29 Correct 11 ms 444 KB Output is correct
30 Correct 8 ms 444 KB Output is correct
31 Correct 23 ms 428 KB Output is correct
32 Correct 33 ms 440 KB Output is correct
33 Correct 36 ms 448 KB Output is correct
34 Correct 60 ms 508 KB Output is correct
35 Correct 64 ms 568 KB Output is correct
36 Correct 57 ms 452 KB Output is correct
37 Correct 92 ms 552 KB Output is correct
38 Correct 46 ms 520 KB Output is correct
39 Correct 44 ms 460 KB Output is correct
40 Correct 46 ms 460 KB Output is correct
41 Correct 50 ms 448 KB Output is correct
42 Correct 42 ms 468 KB Output is correct
43 Correct 94 ms 556 KB Output is correct
44 Correct 43 ms 520 KB Output is correct
45 Correct 54 ms 508 KB Output is correct
46 Correct 42 ms 552 KB Output is correct
47 Correct 48 ms 508 KB Output is correct
48 Correct 49 ms 452 KB Output is correct
49 Correct 67 ms 564 KB Output is correct
50 Correct 47 ms 472 KB Output is correct
51 Correct 40 ms 452 KB Output is correct
52 Correct 54 ms 452 KB Output is correct
53 Correct 49 ms 556 KB Output is correct
54 Correct 62 ms 556 KB Output is correct
55 Correct 53 ms 456 KB Output is correct
56 Correct 52 ms 568 KB Output is correct
57 Correct 50 ms 564 KB Output is correct
58 Correct 51 ms 768 KB Output is correct
59 Correct 45 ms 452 KB Output is correct
60 Correct 74 ms 472 KB Output is correct