[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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值