[Codeforces Global Round 5]D. Balanced Playlist(线段树)

首先考虑有一个点-1的情况,即走了一圈还能走一圈,说明min×2>=max,所以每个点都是-1
其他情况:把环破链,上个点i到点j停下,那么下个点i+1肯定也能到j(少了a[i]),需要重新计算i+1到j的最大值。可以用线段树维护。注意j可能走到第三圈
题目

import java.io.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException{
        Scanner cin=new Scanner(System.in);
        PrintWriter cout = new PrintWriter(System.out);
        int n = cin.nextInt(),minn = (int)1e9,maxx = 1;
        int []a = new int[311101];
        for (int i = 1; i <= n; i++) {
            a[i] = cin.nextInt();
            a[i+n] = a[i];
            a[i+n+n] = a[i];
            if (a[i] < minn) minn = a[i];
            if (a[i] > maxx) maxx = a[i];
        }
        if (maxx <= minn*2) {
            for (int i = 1; i <= n; i++) cout.print("-1 ");
            cin.close(); cout.close();
            return;
        }
        SegTree t = new SegTree(1,n*2,a);
        int j = 1,ans,Max;
        for (int i = 1; i <= n; i++) {
            if (j < i) j = i;
            Max = t.Max(1,2*n,i,j);
            while (a[j]*2 >= Max) {
                Max = Integer.max(Max, a[j]);
                j++;
            }
            cout.print((j-i)+" ");
        }
        cin.close(); cout.close();
    }
}

class SegTree {
    int val;
    SegTree lch,rch;
    SegTree(){}
    SegTree(int l,int r,int []a) {
        if (l == r) {
            this.val = a[l];
            this.lch = this.rch = null;
            return;
        }
        int mid = (l+r)>>1;
        this.lch = new SegTree(l,mid,a);
        this.rch = new SegTree(mid+1,r,a);
        this.val = Integer.max(lch.val,rch.val);
    }
    int Max(int l,int r,int x,int y) {
        if (x<=l && r<=y) return this.val;
        if (x>r || y<l) return 0;
        int mid = (l+r)>>1;
        return Integer.max(lch.Max(l,mid,x,y),rch.Max(mid+1,r,x,y));
    }
}

Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of tracks numbered from to . The playlist is automatic and cyclic: whenever track finishes playing, track starts playing automatically; after track goes track
For each track
, you have estimated its coolness . The higher is, the cooler track
is.
Every morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness
of already played tracks. Once you hear that a track with coolness strictly less than
(no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood.
For each track
, find out how many tracks you will listen to before turning off the music if you start your morning with track , or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted.

你可能感兴趣的:(OI/ACM,Solution,cf,线段树,rmq)