HDU-2795 线段树

由于题目给的公告数最多是200000,所以最多也就用200000行。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main_HDU2795{
	static int[] Tr = new int[200005<<2];  //用行做数组长度
	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[] args) {
		InputReader2795 sc = new InputReader2795();
		while(sc.hasNext()) {
			int h = sc.nextInt();
			int w = sc.nextInt();
			int n = sc.nextInt();
			if(h>n) h = n;  //因为最多的公告数只有200000 所以只能用到200000行
			build(1,h,1);
			for(int i=1;i<=n;i++) {
				int wi = sc.nextInt();
				if(Tr[1]+wi>w) {  //用的最短的行都超过了
					out.println(-1);
				}else {
					query(wi,w,1,h,1);
				}
			}
			out.flush();
		}
	}
	public static void build(int l,int r,int root) {
		Tr[root] = 0;
		if(l==r) return;
		int m = (l+r)>>1;
		build(l,m,root<<1);
		build(m+1,r,root<<1|1);
	}
	public static void query(int wi,int w,int l,int r,int root) {
		if(l==r) {
			out.println(l);
			Tr[root] += wi;
			return;
		}
		int m = (l+r)>>1;
		if(Tr[root<<1]+wi<=w) {     
			query(wi,w,l,m,root<<1);
		}else {
			query(wi,w,m+1,r,root<<1|1);
		}
		Tr[root] = Math.min(Tr[root<<1], Tr[root<<1|1]);
	}
}
class InputReader2795
{
    BufferedReader buf;
    StringTokenizer tok;
    InputReader2795()
    {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext()
    {
        while(tok == null || !tok.hasMoreElements()) 
        {
            try
            {
                tok = new StringTokenizer(buf.readLine());
            } 
            catch(Exception e) 
            {
                return false;
            }
        }
        return true;
    }
    String next()
    {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt()
    {
        return Integer.parseInt(next());
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值