답안 #952710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952710 2024-03-24T15:51:07 Z kilkuwu 도서관 (JOI18_library) C++17
100 / 100
104 ms 1476 KB
#include <bits/stdc++.h>


#ifndef LOCAL
#include "library.h"
#else
#include <vector>
int Query(const std::vector<int>& M);
void Answer(const std::vector<int>& res);
#endif

void Solve(int N) {
  if (N == 1) {
    Answer({1});
    return;
  }
  std::vector<int> dp(N + 1, -1);

  auto get_pref = [&](int x) {
    if (dp[x] != -1) return dp[x];
    std::vector<int> a(N);
    for (int i = 0; i < x; i++) {
      a[i] = 1;
    }
    return dp[x] = Query(a);
  };

  auto get_pref_more = [&](int x, int y) {
    std::vector<int> a(N);
    a[y - 1] = 1;
    for (int i = 0; i < x; i++) {
      a[i] = 1;
    }
    return Query(a);
  };

  std::vector<std::vector<int>> adj(N + 1);

  dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (get_pref(i) - get_pref(i - 1) == 1) {
      continue;
    }

    if (get_pref(i) - get_pref(i - 1) == 0) {
      int lb = 1, rb = i - 1; 
      while (lb < rb) {
        int mid = (lb + rb) / 2;
        int bef = get_pref(mid);
        int aft = get_pref_more(mid, i);
        if (aft - bef == 0) {
          rb = mid;
        } else {
          lb = mid + 1;
        }
      }
      adj[rb].push_back(i);
      adj[i].push_back(rb);
      continue;
    }

    assert(get_pref(i) - get_pref(i - 1) == -1);

    int lb = 1, rb = i - 1;
    while (lb < rb) {
      int mid = (lb + rb) / 2;
      int bef = get_pref(mid);
      int aft = get_pref_more(mid, i);

      if (aft - bef == -1) { // now we can be sure
        rb = mid;
      } else {
        lb = mid + 1;
      }
    }

    int z = rb;

    adj[z].push_back(i);
    adj[i].push_back(z);

    lb = 1, rb = z - 1; // z - 1 
    while (lb < rb) {
      int mid = (lb + rb) / 2;
      int bef = get_pref(mid);
      int aft = get_pref_more(mid, i);

      if (aft - bef == 0) {
        rb = mid;
      } else {
        lb = mid + 1;
      }
    }

    adj[rb].push_back(i);
    adj[i].push_back(rb);
  }

  std::vector<int> res;

  std::function<void(int, int)> dfs = [&](int u, int p) {
    res.push_back(u);
    for (int v : adj[u]) {
      if (v == p) continue;
      dfs(v, u);
    }
  };


  for (int i = 1; i <= N; i++) {
    if ((int) adj[i].size() == 1) {
      dfs(i, i);
      break;
    }
  }

  assert(res.size() == N);
  Answer(res);
}

#ifdef LOCAL
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
using namespace std;
void Solve(int N);

namespace {
struct Judge
{
	int N;
	int A[1002];
	int pos[1002];
	bool f[1002];
	int query_c;
	bool answered;
	void init()
	{
		query_c=0;
		int ret=scanf("%d",&N); ret++;
		answered=false;
		for(int i=0;i<N;i++)ret=scanf("%d",&A[i]),pos[A[i]]=i;
	}
	int query(const vector<int>& M)
	{
		if(query_c==20000)
		{
			puts("Wrong Answer [3]");
			exit(0);
		}
		if(int(M.size())!=N)
		{
			puts("Wrong Answer [1]");
			exit(0);
		}
		bool all_zero=true;
		for(int i=0;i<N;i++)
		{
			if(M[i]!=0&&M[i]!=1)
			{
				puts("Wrong Answer [2]");
				exit(0);
			}
			if(M[i]==1)all_zero=false;
		}
		if(all_zero)
		{
			puts("Wrong Answer [2]");
			exit(0);
		}
		memset(f,0,sizeof(f));
		for(int i=0;i<N;i++)if(M[i])f[pos[i+1]]=true;
		bool las=false;
		int r=0;
		for(int i=0;i<N;i++)
		{
			if(las==false&&f[i]==true)r++;
			las=f[i];
		}
		query_c++;
		return r;
	}
	void answer(const vector<int>& res)
	{
		bool f1=true,f2=true;
		if(int(res.size())!=N)
		{
			puts("Wrong Answer [4]");
			exit(0);
		}
		if(answered)
		{
			puts("Wrong Answer [7]");
			exit(0);
		}
		answered=true;
		memset(f,0,sizeof(f));
		for(int i=0;i<N;i++)
		{
			if(res[i]<=0||res[i]>N)
			{
				puts("Wrong Answer [5]");
				exit(0);
			}
			if(f[res[i]])
			{
				puts("Wrong Answer [6]");
				exit(0);
			}
			f[res[i]]=true;
		}
		for(int i=0;i<N;i++)
		{
			f1&=A[i]==res[i];
			f2&=A[i]==res[N-i-1];
		}
		if(!f1&&!f2)
		{
			puts("Wrong Answer [8]");
			exit(0);
		}
	}
	void end()
	{
		if(!answered)puts("Wrong Answer [7]");
		else printf("Accepted : %d\n",query_c);
	}
}judge;
}

int Query(const vector<int>& M)
{
	return judge.query(M);
}
void Answer(const vector<int>& res)
{
	judge.answer(res);
}

int main()
{
	judge.init();
	Solve(judge.N);
	judge.end();
}
#endif

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from library.cpp:1:
library.cpp: In function 'void Solve(int)':
library.cpp:117:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  117 |   assert(res.size() == N);
      |          ~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 696 KB # of queries: 1453
2 Correct 12 ms 448 KB # of queries: 1464
3 Correct 14 ms 696 KB # of queries: 1538
4 Correct 15 ms 444 KB # of queries: 1534
5 Correct 13 ms 448 KB # of queries: 1536
6 Correct 13 ms 676 KB # of queries: 1527
7 Correct 12 ms 444 KB # of queries: 1541
8 Correct 11 ms 444 KB # of queries: 1474
9 Correct 11 ms 700 KB # of queries: 1544
10 Correct 10 ms 444 KB # of queries: 903
11 Correct 0 ms 344 KB # of queries: 0
12 Correct 0 ms 344 KB # of queries: 1
13 Correct 0 ms 344 KB # of queries: 3
14 Correct 0 ms 344 KB # of queries: 6
15 Correct 1 ms 344 KB # of queries: 54
16 Correct 1 ms 436 KB # of queries: 124
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 696 KB # of queries: 1453
2 Correct 12 ms 448 KB # of queries: 1464
3 Correct 14 ms 696 KB # of queries: 1538
4 Correct 15 ms 444 KB # of queries: 1534
5 Correct 13 ms 448 KB # of queries: 1536
6 Correct 13 ms 676 KB # of queries: 1527
7 Correct 12 ms 444 KB # of queries: 1541
8 Correct 11 ms 444 KB # of queries: 1474
9 Correct 11 ms 700 KB # of queries: 1544
10 Correct 10 ms 444 KB # of queries: 903
11 Correct 0 ms 344 KB # of queries: 0
12 Correct 0 ms 344 KB # of queries: 1
13 Correct 0 ms 344 KB # of queries: 3
14 Correct 0 ms 344 KB # of queries: 6
15 Correct 1 ms 344 KB # of queries: 54
16 Correct 1 ms 436 KB # of queries: 124
17 Correct 98 ms 1476 KB # of queries: 10059
18 Correct 95 ms 728 KB # of queries: 9932
19 Correct 95 ms 984 KB # of queries: 10056
20 Correct 96 ms 720 KB # of queries: 9366
21 Correct 88 ms 720 KB # of queries: 8825
22 Correct 104 ms 1244 KB # of queries: 10052
23 Correct 101 ms 720 KB # of queries: 10041
24 Correct 44 ms 944 KB # of queries: 4650
25 Correct 99 ms 728 KB # of queries: 9822
26 Correct 89 ms 960 KB # of queries: 9199
27 Correct 52 ms 692 KB # of queries: 4622
28 Correct 83 ms 972 KB # of queries: 8977
29 Correct 102 ms 468 KB # of queries: 8967
30 Correct 86 ms 980 KB # of queries: 8977