关闭

POJ3061 尺取法

121人阅读 评论(0) 收藏 举报
分类:

n个数字,求最短的连续的数字长度,满足和>=s

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		new POJ3061().solve();
	}
}

class POJ3061 {

	InputReader in = new InputReader(System.in);
	PrintWriter out = new PrintWriter(System.out);
	final int N = 100008;
	int[] a = new int[N];

	void solve() {
		int t = in.nextInt();
		while (t-- > 0) {
			int n = in.nextInt();
			int s = in.nextInt();
			for (int i = 0; i < n; i++)
				a[i] = in.nextInt();

			int start = 0, end = 0, sum = 0, res = n + 1;
			for (;;) {
				while (end < n && sum < s) {
					sum += a[end++];
				}
				if (sum < s)
					break;
				res = Math.min(res, end - start);
				sum -= a[start++];
			}
			out.println(res == n + 1 ? 0 : res);
		}
		out.flush();
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = new StringTokenizer("");
	}

	private void eat(String s) {
		tokenizer = new StringTokenizer(s);
	}

	public String nextLine() {
		try {
			return reader.readLine();
		} catch (Exception e) {
			return null;
		}
	}

	public boolean hasNext() {
		while (!tokenizer.hasMoreTokens()) {
			String s = nextLine();
			if (s == null)
				return false;
			eat(s);
		}
		return true;
	}

	public String next() {
		hasNext();
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}

}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:214108次
    • 积分:9516
    • 等级:
    • 排名:第1880名
    • 原创:668篇
    • 转载:0篇
    • 译文:0篇
    • 评论:55条
    文章分类
    最新评论