Submission #497976

# Submission time Handle Problem Language Result Execution time Memory
497976 2021-12-24T07:31:26 Z Nalrimet Nice sequence (IZhO18_sequence) C++17
0 / 100
2000 ms 47276 KB
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
using namespace std;

const int N = 2 * 1e6 + 5;
const long long inf = 1000000000;

#define F first
#define S second
#define pb push_back

int n, t, a, b, lastans, k, id, m;
bool used[N];
pair<int, int> pref[N];
vector<int> top, g[N];

void create(int v){
    for(int i = 0; i <= v; ++i){
        g[i].clear();
        used[i] = 0;
    }
    for(int i = 0; i <= v; ++i){
        if(i + n <= v) g[i + n].pb(i);
        if(i - m >= 0) g[i - m].pb(i);
    }
}
/*
0 -> 3 -> 2 1

2 3 0 1
0 1 2 3

2 3 0 1
*/
bool check(int v){
    used[v] = 1;
    for(auto to : g[v]){
        if(used[to] == 1){
            return 1;
        }
        else if(used[to] == 0){
            if(check(to)) return 1;
        }
    }
    used[v] = 2;
    return 0;
}

void topsort(int v){
    used[v] = 1;
    for(auto to : g[v]){
        if(used[to] == 0){
            topsort(to);
        }
    }
    top.pb(v);
}

 main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> t;

    while(t--){
        cin >> n >> m;
        int l = 0, r = n + m , mid;
        while(l < r){
            mid = (l + r) / 2;
            create(mid);
            if(!check(mid)) l = mid;
            else r = mid - 1;
        }

        //l = n + m -    1 - __gcd(n, m);
        cout << l << '\n';
        create(l);
        top.clear();
        for(int i = 0; i <= l; ++i){
            if(!used[i]) topsort(i);
        }
        reverse(top.begin(), top.end());
        for(int i = 0; i < top.size(); ++i){
//            cout << top[i] << ' ' << i << '\n';
            pref[i] = {top[i], i};
        }
//        cout << '\n';
        sort(pref, pref + top.size());
        for(int i = 1; i < top.size(); i++){
            cout << (pref[i].S - pref[i - 1].S) << ' ';
        }
        cout << '\n';
    }

    return 0;

}

// merge sort tree
// l <= b <= r
// r < b

Compilation message

sequence.cpp:65:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   65 |  main() {
      |  ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:91:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         for(int i = 0; i < top.size(); ++i){
      |                        ~~^~~~~~~~~~~~
sequence.cpp:97:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for(int i = 1; i < top.size(); i++){
      |                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 47276 KB Ok
2 Execution timed out 2102 ms 47180 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2090 ms 47172 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 47180 KB Ok
2 Correct 24 ms 47188 KB Ok
3 Execution timed out 2091 ms 47196 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2098 ms 47180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 47276 KB Ok
2 Execution timed out 2102 ms 47180 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 47276 KB Ok
2 Execution timed out 2102 ms 47180 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 47276 KB Ok
2 Execution timed out 2102 ms 47180 KB Time limit exceeded
3 Halted 0 ms 0 KB -