poj 3061 尺取法或二分

经典尺取法,复杂度O(n)。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5
 6 const int INF = 999999;
 7 const int N = 100000;
 8 int a[N];
 9
10 int main ()
11 {
12     int t;
13     scanf("%d", &t);
14     while ( t-- )
15     {
16         int n, s;
17         scanf("%d%d", &n, &s);
18         for ( int i = 0; i < n; i++ ) scanf("%d", a + i);
19         int p = 0, q = 0, sum = 0, len = INF;
20         while ( 1 )
21         {
22             while ( q < n && sum < s )
23             {
24                 sum += a[q++];
25             }
26             if ( sum < s ) break;
27             len = min( len, q - p );
28             sum -= a[p++];
29         }
30         if ( len == INF ) len = 0;
31         printf("%d\n", len);
32     }
33     return 0;
34 }

二分也可以做,复杂度多了个log。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6
 7 typedef long long ll;
 8 const int INF = 999999;
 9 const int N = 100001;
10 ll sum[N];
11
12 int main ()
13 {
14     int t;
15     scanf("%d", &t);
16     while ( t-- )
17     {
18         int n, s;
19         scanf("%d%d", &n, &s);
20         sum[0] = 0;
21         for ( int i = 1; i <= n; i++ )
22         {
23             scanf("%lld", sum + i);
24             sum[i] += sum[i - 1];
25         }
26         int len = INF;
27         for ( int i = n; i > 0; i-- )
28         {
29             if ( sum[i] < s ) break;
30             int pos = upper_bound( sum, sum + i, sum[i] - s ) - sum;
31             if ( pos != i )
32             {
33                 len = min( len, i - pos + 1 );
34             }
35         }
36         if ( len == INF ) len = 0;
37         printf("%d\n", len);
38     }
39     return 0;
40 }
时间: 2024-10-28 20:25:21

poj 3061 尺取法或二分的相关文章

poj 2100 尺取法(尺度法)

poj 2100 尺取法(尺度法) 题意 给你一个数N,然后找到一个连续的序列,使得这个序列中的数的平方和等于N. 输出需要注意的是第一行为解的个数,剩下的每行先输出序列的长度,然后输出序列.按照序列的长度进行降序输出. 解题思路 没啥好说的,使用尺度法,进行枚举各个区间上的数. 需要注意的是数字1的答案为: 1 1 1 代码实现 #include<cmath> #include<cstdio> #include<cstring> #include<algorit

POJ——3061Subsequence(尺取法或二分查找)

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11224   Accepted: 4660 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) ar

POJ 3320 尺取法,Hash,map标记

1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识点 必须说,STL够屌.. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio>

POJ 2566(尺取法

自己看了半天并没有看出这题怎么用尺取法(虽然一看就觉得肯定是尺取法..),由于是绝对值,那么在计算的时候头和尾的实际位置并不重要,而应用尺取法这个数列肯定得是单调,那么我们把前缀和处理出来排序就可以直接应用尺取法了 #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<utility> #include<vector> #inc

poj 2100(尺取法)

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m

poj 3320 尺取法

容易联想到尺取法,因为假设从第s页开始阅读至少需要读到t页才能覆盖所有知识点的话,那么如果从s+1页开始阅读,至少要读到t'>=t的位置.于是可以考虑用map维护一下. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <map> 5 using namespace std; 6 7 const int INF = 1111111; 8 const int

poj 2100 尺取法 一个数字拆成连续数字平方和

题意:将一个数拆成若干个连续数字的平方和. 用尺取法枚举区间,复杂度为O(n),时限10s,3s多ac. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 7 const int N = 100; 8 9 struct Node 10 { 11 int from, to; 12 } node[N]

POJ 3320 (尺取法+Hash)

题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的,因此需要统计不重复元素个数,而且需要记录重复个数. 最好能及时O(1)反馈不重复的个数.那么毫无疑问,得使用Hash. 推荐使用map,既能Hash,也能记录对于每个key的个数. 尺取的思路: ①不停扩展R,并把扫过知识点丢到map里,直到map的size符合要求. ②更新结果. ②L++,map

POJ 2566 尺取法(进阶题)

Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4297   Accepted: 1351   Special Judge Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration