Submission #45038

# Submission time Handle Problem Language Result Execution time Memory
45038 2018-04-11T04:47:26 Z RayaBurong25_1 Library (JOI18_library) C++17
100 / 100
280 ms 848 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include "library.h"
typedef struct cc cc;
struct cc
{
	std::vector<int> v;
};
std::vector<cc> All;
std::vector<int> M;
int test(int N, int s, int e, int exclude, int i)
{
	int j, k;
	for (j = 0; j < N; j++)
		M[j] = 0;
	for (j = s; j <= e; j++)
	{
		if (j == exclude) continue;
		for (k = 0; k < All[j].v.size(); k++)
			M[All[j].v[k]] = 1;
	}
	if (exclude < s || exclude > e)
		exclude = -1;
	// int cnt = Query(M);
	// printf("cnt %d should be %d\n", cnt, (e - s + 1) - (exclude != -1));
	M[i] = 1;
	int cnt = Query(M);
	// printf("s %d e %d cnt %d\n", s, e, cnt);
	if (cnt <= (e - s + 1) - (exclude != -1))
		return 1;
	else
		return 0;
}
int checkend(int N, int p, int i)
{
	int j, k;
	for (j = 0; j < N; j++)
		M[j] = 0;
	M[All[p].v.back()] = 1;
	M[i] = 1;
	int cnt = Query(M);
	if (cnt == 1)
		return 1;
	else
		return 0;
}
void Solve(int N)
{
	int CCo = 0, CCn;
	int i, j, k;
	for (i = 0; i < N; i++)
		M.push_back(0);
	cc nothing;
	int mn, md, mx, t;
	for (i = 0; i < N; i++)
	{
		// printf("i%d\n", i);
		for (j = 0; j <= i; j++)
			M[j] = 1;
		for (j = i + 1; j < N; j++)
			M[j] = 0;
		// M[i] = 1;
		CCn = Query(M);
		// printf("CCn %d\n", CCn);
		if (CCn > CCo)
		{
			All.push_back(nothing);
			All.back().v.push_back(i);
		}
		else if (CCn == CCo)
		{
			mn = 0;
			mx = All.size() - 1;
			while (mn != mx)
			{
				md = (mn + mx)/2;
				if (test(N, mn, md, -1, i))
				{
					mx = md;
				}
				else
				{
					mn = md + 1;
				}
			}
			if (All[mn].v.size() == 1)
				All[mn].v.push_back(i);
			else
			{
				if (!checkend(N, mn, i))
					std::reverse(All[mn].v.begin(), All[mn].v.end());
				All[mn].v.push_back(i);
			}
		}
		else
		{
			mn = 0;
			mx = All.size() - 1;
			while (mn != mx)
			{
				md = (mn + mx)/2;
				if (test(N, mn, md, -1, i))
				{
					mx = md;
				}
				else
				{
					mn = md + 1;
				}
			}
			if (All[mn].v.size() == 1)
				All[mn].v.push_back(i);
			else
			{
				if (!checkend(N, mn, i))
					std::reverse(All[mn].v.begin(), All[mn].v.end());
				All[mn].v.push_back(i);
			}
			t = mn;
			// printf("t %d\n", t);

			mn = 0;
			mx = All.size() - 1;
			while (mn != mx)
			{
				md = (mn + mx)/2;
				if (test(N, mn, md, t, i))
				{
					mx = md;
				}
				else
				{
					mn = md + 1;
				}
			}
			if (All[mn].v.size() == 1)
			{
				for (j = 0; j < All[mn].v.size(); j++)
					All[t].v.push_back(All[mn].v[j]);
				// printf("ok?\n");
				All[mn].v.clear();
				All.erase(All.begin() + mn);
				// printf("ok?\n");
			}
			else
			{
				if (checkend(N, mn, i))
					std::reverse(All[mn].v.begin(), All[mn].v.end());
				for (j = 0; j < All[mn].v.size(); j++)
					All[t].v.push_back(All[mn].v[j]);
				// printf("ok?\n");
				All[mn].v.clear();
				All.erase(All.begin() + mn);
				// printf("ok?\n");
			}
		}
		// printf("CC %d\n", CCn);
		CCo = CCn;
		// printf("All.size() %d\n", All.size());
		// for (j = 0; j < All.size(); j++)
		// {
		// 	printf("j%d : ", j);
		// 	for (k = 0; k < All[j].v.size(); k++)
		// 		printf("%d ", All[j].v[k]);
		// 	printf("\n");
		// }
	}

	std::vector<int> res(N);
	for (i = 0; i < N; i++)
	{
		res[i] = All[0].v[i] + 1;
	}
	Answer(res);
}

Compilation message

library.cpp: In function 'int test(int, int, int, int, int)':
library.cpp:20:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (k = 0; k < All[j].v.size(); k++)
               ~~^~~~~~~~~~~~~~~~~
library.cpp: In function 'int checkend(int, int, int)':
library.cpp:37:9: warning: unused variable 'k' [-Wunused-variable]
  int j, k;
         ^
library.cpp: In function 'void Solve(int)':
library.cpp:139:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (j = 0; j < All[mn].v.size(); j++)
                 ~~^~~~~~~~~~~~~~~~~~
library.cpp:150:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (j = 0; j < All[mn].v.size(); j++)
                 ~~^~~~~~~~~~~~~~~~~~
library.cpp:51:12: warning: unused variable 'k' [-Wunused-variable]
  int i, j, k;
            ^
# Verdict Execution time Memory Grader output
1 Correct 18 ms 248 KB Output is correct
2 Correct 26 ms 436 KB Output is correct
3 Correct 16 ms 516 KB Output is correct
4 Correct 20 ms 516 KB Output is correct
5 Correct 15 ms 592 KB Output is correct
6 Correct 20 ms 592 KB Output is correct
7 Correct 21 ms 656 KB Output is correct
8 Correct 20 ms 656 KB Output is correct
9 Correct 20 ms 656 KB Output is correct
10 Correct 9 ms 656 KB Output is correct
11 Correct 2 ms 656 KB Output is correct
12 Correct 2 ms 656 KB Output is correct
13 Correct 1 ms 656 KB Output is correct
14 Correct 2 ms 656 KB Output is correct
15 Correct 2 ms 656 KB Output is correct
16 Correct 7 ms 656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 248 KB Output is correct
2 Correct 26 ms 436 KB Output is correct
3 Correct 16 ms 516 KB Output is correct
4 Correct 20 ms 516 KB Output is correct
5 Correct 15 ms 592 KB Output is correct
6 Correct 20 ms 592 KB Output is correct
7 Correct 21 ms 656 KB Output is correct
8 Correct 20 ms 656 KB Output is correct
9 Correct 20 ms 656 KB Output is correct
10 Correct 9 ms 656 KB Output is correct
11 Correct 2 ms 656 KB Output is correct
12 Correct 2 ms 656 KB Output is correct
13 Correct 1 ms 656 KB Output is correct
14 Correct 2 ms 656 KB Output is correct
15 Correct 2 ms 656 KB Output is correct
16 Correct 7 ms 656 KB Output is correct
17 Correct 217 ms 656 KB Output is correct
18 Correct 205 ms 716 KB Output is correct
19 Correct 280 ms 716 KB Output is correct
20 Correct 177 ms 716 KB Output is correct
21 Correct 186 ms 716 KB Output is correct
22 Correct 244 ms 716 KB Output is correct
23 Correct 229 ms 716 KB Output is correct
24 Correct 80 ms 716 KB Output is correct
25 Correct 215 ms 716 KB Output is correct
26 Correct 201 ms 848 KB Output is correct
27 Correct 75 ms 848 KB Output is correct
28 Correct 51 ms 848 KB Output is correct
29 Correct 44 ms 848 KB Output is correct
30 Correct 52 ms 848 KB Output is correct