POJ 3061 (尺取法)
来源:互联网 发布:笨笨鸟网络 资源下载 编辑:程序博客网 时间:2024/04/28 10:16
Subsequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10639 Accepted: 4408
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
210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5
Sample Output
23
Source
Southeastern Europe 2006
原来这种做法叫尺取法,印象中用过好多次了原来还有个这样高大上的名字~
题意是给你一个正数序列,问你最短的连续子序列长度l使得这段子序列的和大于等于S。
先找到一个大于等于S的从1开始的子序列,然后不断地减掉左边的,加上右边的,每次贪心,取最小值。
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <algorithm>#include <vector>#include <cstdio>using namespace std;typedef unsigned long long ull;#define maxn 111111int n;long long a[maxn], m;int main () {int t;scanf ("%d", &t);while (t--) {scanf ("%d%lld", &n, &m);for (int i = 1; i <= n; i++) {scanf ("%lld", &a[i]);}long long sum = 0;int l = 1, r = 1;while (sum < m && r <= n) {sum += a[r++];} if (sum < m) {printf ("0\n");continue;}int ans = n;for (r = r; r <= n+1; r++) {while (sum-a[l] >= m) {sum -= a[l++];}ans = min (ans, r-l);if (r == n+1)break;sum += a[r];}printf ("%d\n", ans);}return 0;}
0 0
- poj 3061 尺取法
- poj 3061 尺取法
- poj 3061 尺取法
- poj 3061 尺取法
- POJ 3061 (尺取法)
- poj 3061 尺取法
- POJ 3061 尺取法
- 尺取法 +POJ 3061
- poj 3061 尺取法
- poj 3061 尺取法
- 【poj 3061】尺取法
- 尺取法 poj 3061
- poj 3061--Subsequence(尺取法)
- poj 3061 Subsequence(尺取法)
- POJ 3061 Subsequence ( 尺取法 )
- POJ 3061 Subsequence(尺取法)
- Poj 3061 Subsequence【尺取法】
- poj 3061 Subsequence (尺取法)
- Dialog 形式的 Activity
- DL,DT,DD,比传统table更语义,解析更快的table列表方式
- ios收集到的完整app
- BZOJ2820: YY的GCD
- 用cmd的FC命令 对比两个文件夹内容不同并将文件名输出到文件中
- POJ 3061 (尺取法)
- java基础
- Oracle中基本sql语句
- Python基础01 Hello World!
- 解决android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState
- mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
- 2.0.1 客户端需求分析实例
- 油漆费才十几刀-而人工费加起来就要400多刀
- Google GFS文件系统深入分析