# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
317535 | Kevin_Zhang_TW | Hidden Sequence (info1cup18_hidden) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] : ", args)
void kout() { cerr << endl; }
template<class T, class ...U>
void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
void debug(auto L, auto R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
vector<int> ans{1,1,1,1,1,1,1,1,1,1,1};
int mxq;
bool isSubsequence(vector<int> a) {
for (int i = 0, j = 0;i < a.size();++i) {
while (j < ans.size() && ans[j] != a[i])
++j;
if (j == ans.size()) return false;
++j;
}
mxq = max(mxq, (int)a.size());
return true;
}
#else
#include "grader.h"
#define debug(...) 0
#define DE(...) 0
#endif
const int MAX_N = 300010, inf = 1e9;
const vector<int> zero{0}, one{1};
int n;
bool query(vector<int> a) {
if (a.size() > n) return false;
return isSubsequence(a);
}
vector<int> operator + (vector<int> a, vector<int> b) {
a.insert(end(a), AI(b));
return a;
}
vector<pair<int,int>> edge[MAX_N];
int dp[MAX_N];
vector<int> findSequence(int _n) {
n = _n;
int flip = 0;
auto makev = [&](int a, int v1, int b, int v2, int c, int v3) {
return vector<int>(a, v1^flip) + vector<int>(b, v2^flip) + vector<int>(c, v3^flip);
};
auto con = [&](int l, int r, int sum) {
DE(l, r, sum);
edge[r].pb(l-1, -sum);
};
DE(n);
vector<int> Z, O, ans;
while (Z.size() == O.size() && Z.size() + O.size() < n) {
if (query(Z + zero))
Z.pb(0);
if (query(O + one))
O.pb(1);
}
if (Z.size() > O.size())
swap(Z, O), flip ^= 1;
while (Z.size () + O.size () < n)
O.pb(0);
int m = Z.size();
//con(1, m+1, O.size());
auto bellmand = [&]() {
dp[m+1] = O.size();
fill(dp+1, dp+m+1, inf);
queue<pair<int,int>> q;
q.push(m+1);
while (q.size()) {
auto [x, len] = q.front(); q.pop();
if (len > dp[x]) continue;
for (auto [u, w] : edge[x])
if (dp[x] + w < dp[u]) {
dp[u] = dp[x] + w;
q.push(u);
}
}
};
for (int l0 = 0;l0 <= m;++l0)
for (int r0 = 0;r0 + l0 <= m;++r0) {
int mx1 = 0;
for (int m1 = 0;m1 + l0 + r0 <= n;++m1) {
if (query(makev(l0, 0, m1, 1, r0, 0)))
mx1 = m1;
else break;
}
con(l0+1, m+1 - r0, mx1);
}
bellmand();
for (int i = m+1;i >= 1;--i)
dp[i] -= dp[i-1];
for (int i = 1;i <= m+1;++i) {
while (dp[i]--)
ans.pb( 1 ^ flip );
if (i <= m)
ans.pb( 0 ^ flip );
}
return ans;
}
#ifdef KEV
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
vector<int> res = findSequence(ans.size());
if (res == ans) {
cerr << "AC!\n";
DE(mxq);
}
else {
cerr << "WA\n";
debug(AI(res));
cerr << "Should be\n";
debug(AI(ans));
}
}
#endif