1484D - Playlist - CodeForces Solution


data structures dsu implementation *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;

const int N = (1 << 17);
const int INF = 1e9 + 13;

int gcd(int a, int b) {
    if(!b) return a;
    return gcd(b, a % b);
}
int t[2 * N], p[2 * N];
void update(int pos, int val, int l = 0, int r = N - 1, int v = 1) {
    if(l == r) {
        t[v] = val; 
        p[v] = pos;
        return;
    }
    int m = (l + r) / 2;
    if(pos <= m) update(pos, val, l, m, 2 * v);
    else update(pos, val, m + 1, r, 2 * v + 1);
    t[v] = min(t[2 * v], t[2 * v + 1]);
    p[v] = (t[2 * v] <= t[2 * v + 1] ? p[2 * v] : p[2 * v + 1]);
}
pair<int, int> get(int ql, int qr, int l = 0, int r = N - 1, int v = 1) {
    if(r < ql || l > qr || ql > qr) return {INF, N};
    if(ql <= l && r <= qr) {
        return {t[v], p[v]};
    }
    int m = (l + r) / 2;
    pair<int, int> t1 = get(ql, qr, l, m, 2 * v);
    pair<int, int> t2 = get(ql, qr, m + 1, r, 2 * v + 1);
    return {min(t1.first, t2.first), (t1.first <= t2.first ? t1.second : t2.second)};
}   
set<int> elems;
set<int>::iterator prev(int x) {
    if(elems.empty()) return elems.end();
    return (*elems.begin() < x ? --elems.lower_bound(x) : --elems.end());
}
set<int>::iterator next(int x) {
    if(elems.empty()) return elems.end();
    return (*(--elems.end()) > x ? elems.upper_bound(x) : elems.begin());
}
void solve() {
    vector<int> ans;

    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++) 
        cin >> a[i];
    for(int i = 0; i < n; i++) {
        elems.insert(i);
        update(i, gcd(a[i], a[(i + n - 1) % n]));
    }
    int lb = 0;
    while(!elems.empty()) {
        pair<int, int> vi;
        vi = get(*next(lb), n - 1);
        if(vi.first != 1) {
            vi = get(0, n - 1);
            if(vi.first != 1) 
                break;
        }
        update(vi.second, INF);
        elems.erase(vi.second);
        int nx = *next(vi.second), pr = *prev(vi.second);
        ans.push_back(vi.second);
        update(nx, gcd(a[nx], a[pr]));
        lb = nx;
    }
    cout << (int)ans.size() << ' ';
    for(int c : ans) cout << c+1 << ' ';
    elems.clear();
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while(t--) {
        solve();
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST