Submission #518708

#TimeUsernameProblemLanguageResultExecution timeMemory
518708spike1236Nice sequence (IZhO18_sequence)C++14
100 / 100
1199 ms57588 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define f first #define s second #define ll long long #define ld long double #define all(_v) _v.begin(), _v.end() #define sz(_v) (int)_v.size() #define pii pair <int, int> #define pll pair <ll, ll> #define veci vector <int> #define vecll vector <ll> const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const double PI = 3.1415926535897932384626433832795; const double eps = 1e-9; const int MOD1 = 1e9 + 7; const int MOD2 = 998244353; const int MAXN = 4e5 + 10; int n, m; int pref[MAXN]; char used[MAXN]; bool cyc; int cur; veci ts; void dfs(int v) { if(cyc) return; used[v] = 1; for(auto to : {v - n, v + m}) { if(to < 0 || to > cur) continue; if(!used[to]) dfs(to); else if(used[to] == 1) { cyc = 1; return; } } used[v] = 2; } void dfs2(int v) { used[v] = 1; for(auto to : {v - n, v + m}) { if(to < 0 || to > cur) continue; if(!used[to]) dfs2(to); } ts.pb(v); } void solve() { cin >> n >> m; int l = 1, r = n + m, rs = 0; while(l <= r) { int mid = l + r >> 1; cur = mid; for(int i = 0; i <= mid; ++i) used[i] = 0; cyc = 0; for(int i = 0; i <= mid; ++i) { if(!used[i]) dfs(i); if(cyc) break; } if(cyc) r = mid - 1; else rs = mid, l = mid + 1; } cout << rs << '\n'; ts.clear(); for(int i = 0; i <= rs; ++i) used[i] = 0; for(int i = 0; i <= rs; ++i) if(!used[i]) dfs2(i); reverse(all(ts)); for(int i = 0; i < sz(ts); ++i) pref[ts[i]] = i; for(int i = 1; i <= rs; ++i) cout << pref[i] - pref[i - 1] << ' '; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int CNT_TESTS = 1; cin >> CNT_TESTS; for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) { solve(); if(NUMCASE != CNT_TESTS) cout << '\n'; } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'void solve()':
sequence.cpp:58:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |         int mid = l + r >> 1;
      |                   ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...