이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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<int> adj[N];
vector<bool> vis(N), recstack(N);
vector<int> topo;
bool cyc;
void dfs(int u) {
vis[u] = 1;
recstack[u] = 1;
trav(v, adj[u]) {
if (vis[v]) {
if (recstack[v]) cyc = true;
conts;
}
dfs(v);
}
topo.pb(u);
recstack[u] = 0;
}
void solve(int test_case)
{
int n, m; cin >> n >> m;
auto ok = [&](int mid) {
rep(i, mid + 1) adj[i].clear(), vis[i] = 0;
rep1(i, mid) {
if (i - n >= 0) {
adj[i].pb(i - n);
}
if (i - m >= 0) {
adj[i - m].pb(i);
}
}
topo.clear();
cyc = false;
rep(i, mid + 1) {
if (vis[i]) conts;
dfs(i);
}
reverse(all(topo));
if (cyc) return false;
return true;
};
int l = 1, r = N - 1;
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;
}
컴파일 시 표준 에러 (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:158:5: note: in expansion of macro 'rep'
158 | 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... |