This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Om Namah Shivaya
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
https://codeforces.com/blog/entry/63991?#comment-478025
https://oj.uz/submission/682671
p(i) = pref sum until i
p(i) - p(i-n) < 0 => p(i) < p(i-n)
p(i) - p(i-m) > 0 => p(i) > p(i-m)
find a valid p and the answer can be restored from this
how to find a valid p
the only condition on p is the inequalities that we have
create an array that satisfies given inequalities:
in such cases, think toposort
add edge u-->v if p(u) < p(v)
add edges: i-->i-n and i-m-->i
find toposort, and assign p(i) values in the order every index appears in the toposort
how to find max len seq?
=> if we can make a seq of length k, then we can also make a seq of len k-1
so we can b.s on max len
*/
const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
vector<bool> vis(N), recstack(N);
vector<int> topo;
bool cyc;
int n, m, k;
void dfs(int u) {
vis[u] = 1;
recstack[u] = 1;
for (auto v : {u - n, u + m}) {
if (v < 0 or v > k) conts;
if (vis[v]) {
if (recstack[v]) cyc = true;
conts;
}
dfs(v);
}
topo.pb(u);
recstack[u] = 0;
}
void solve(int test_case)
{
cin >> n >> m;
auto ok = [&](int mid) {
k = mid;
rep(i, k + 1) vis[i] = 0;
topo.clear();
cyc = false;
rep(i, k + 1) {
if (vis[i]) conts;
dfs(i);
}
reverse(all(topo));
if (cyc) return false;
return true;
};
int l = 1, r = n + m;
int ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (ok(mid)) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
ok(ans);
vector<int> p(ans + 5);
rep(i, sz(topo)) {
p[topo[i]] = i;
}
cout << ans << endl;
rep1(i, ans) {
int val = p[i] - p[i - 1];
cout << val << " ";
}
cout << endl;
}
int main()
{
fastio;
int t = 1;
cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void solve(int)':
sequence.cpp:30:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | #define rep(i, n) for(int i = 0; i < n; ++i)
| ^
sequence.cpp:151:5: note: in expansion of macro 'rep'
151 | rep(i, sz(topo)) {
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |