日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3061 -- Subsequence(二分)

發布時間:2024/4/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3061 -- Subsequence(二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Description

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

Sample Input

2 10 15 5 1 3 5 10 7 4 9 2 8 5 11 1 2 3 4 5

Sample Output

2 3

AC

  • 記錄一個sum數組,二分找所有可能的區間,記錄最小的區間
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <algorithm> #define N 100005 #define ll long long using namespace std; int a[N], sum[N]; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint t;scanf("%d", &t);while (t--) {int n, s;scanf("%d%d", &n, &s);for (int i = 1; i <= n; ++i) {scanf("%d", &a[i]);sum[i] = sum[i - 1] + a[i];}// 特判無解 if (sum[n] < s) {printf("0\n");continue;}int len = 1e9;for (int i = 0; i < n; ++i) {int l = i + 1, r = n;while (l < r) {int mid = (l + r) >> 1;if (sum[mid] - sum[i] >= s)r = mid;elsel = mid + 1;}// 如果找到一個合適的序列 if (sum[r] - sum[i] >= s)len = min(len, r - i);}printf("%d\n", len);}return 0; }

總結

以上是生活随笔為你收集整理的POJ 3061 -- Subsequence(二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。