Submission #497034

# Submission time Handle Problem Language Result Execution time Memory
497034 2021-12-22T08:54:14 Z abc864197532 Hidden Sequence (info1cup18_hidden) C++17
100 / 100
5 ms 312 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
	cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
	for (; l != r; ++l) 
		cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x);
#include "grader.cpp"
#else
#define test(x...) void(0);
#endif
const int N = 1001;

bool isSubsequence (vector <int> S);

pair<vector <int>, pii> reduce(vector <int> a) {
	vector <int> cur;
	int now = -1, cnt = 0;
	for (int i : a) {
		if (now == -1) {
			now = i;
			cnt++;
		} else if (now != i) {
			now = -1;
			cnt = 0;
			cur.pb(i);
		} else {
			cnt++;
		}
	}
	return mp(cur, mp(now, cnt));
}

vector <int> findSequence(int n) {
	int bound = n + 1 >> 1;
	int len = 0, d = 0;
	if (isSubsequence(vector <int>(bound, 0))) {
		d = 1;
	}
	for (int i = bound; i > 0; --i) {
		if (isSubsequence(vector <int>(i, d))) {
			len = i;
			break;
		}
	}
	int cnt[2];
	if (d)
		cnt[1] = len, cnt[0] = n - len;
	else
		cnt[0] = len, cnt[1] = n - len;
	vector <int> ans;
	for (int i = 0; i < n; ++i) {
		if (cnt[0] == 0) {
			ans.pb(1);
			cnt[1]--;
			continue;
		}
		if (cnt[1] == 0) {
			ans.pb(0);
			cnt[0]--;
			continue;
		}
		int now, ct;
		auto A = reduce(ans);
		vector <int> res = A.X;
		tie(now, ct) = A.Y;
		bool is = false;
		if (now == -1 || 1 + cnt[now] > ct + 1 + min(cnt[0], cnt[1]))
			is = true;
		if (is) {
			for (int j = 0; j < ct; ++j)
				res.pb(now);
			int d = 1;
			if (cnt[0] < cnt[1])
				d = 0;
			res.pb(d ^ 1);
			for (int j = 0; j < cnt[d]; ++j)
				res.pb(d);
			if (isSubsequence(res))
				ans.pb(d ^ 1), cnt[d ^ 1]--;
			else
				ans.pb(d), cnt[d]--;
		} else {
			res.pb(now ^ 1);
			for (int j = 0; j < cnt[now]; ++j)
				res.pb(now);
			if (isSubsequence(res))
				ans.pb(now ^ 1), cnt[now ^ 1]--;
			else
				ans.pb(now), cnt[now]--;
		}
	}
	return ans;
}

Compilation message

hidden.cpp: In function 'std::vector<int> findSequence(int)':
hidden.cpp:49:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |  int bound = n + 1 >> 1;
      |              ~~^~~
grader.cpp: In function 'int main()':
grader.cpp:28:26: warning: format '%d' expects argument of type 'int', but argument 3 has type 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wformat=]
   28 |     fprintf (fifo_out, "%d\n", ans.size ());
      |                         ~^     ~~~~~~~~~~~
      |                          |              |
      |                          int            std::vector<int>::size_type {aka long unsigned int}
      |                         %ld
grader.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i=0; i<ans.size () && i < N; i++)
      |                   ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct: Maximum length of a query = 5
2 Correct 0 ms 200 KB Output is correct: Maximum length of a query = 6
3 Correct 0 ms 200 KB Output is correct: Maximum length of a query = 5
4 Correct 0 ms 200 KB Output is correct: Maximum length of a query = 5
5 Correct 0 ms 200 KB Output is correct: Maximum length of a query = 4
# Verdict Execution time Memory Grader output
1 Correct 3 ms 200 KB Output is correct: Maximum length of a query = 83
2 Correct 3 ms 296 KB Output is correct: Maximum length of a query = 90
3 Correct 4 ms 312 KB Output is correct: Maximum length of a query = 96
4 Correct 4 ms 304 KB Output is correct: Maximum length of a query = 77
5 Correct 4 ms 304 KB Output is correct: Maximum length of a query = 95
6 Correct 3 ms 200 KB Output is correct: Maximum length of a query = 87
7 Correct 3 ms 200 KB Output is correct: Maximum length of a query = 96
8 Correct 3 ms 304 KB Output is correct: Maximum length of a query = 83
9 Correct 4 ms 308 KB Output is correct: Maximum length of a query = 101
10 Correct 4 ms 200 KB Output is correct: Maximum length of a query = 100
11 Correct 4 ms 312 KB Output is correct: Maximum length of a query = 96
12 Correct 2 ms 296 KB Output is correct: Maximum length of a query = 100
13 Correct 5 ms 304 KB Output is correct: Maximum length of a query = 101