poj--3061--Subsequence(技巧)
来源:互联网 发布:mcs51单片机数据总线 编辑:程序博客网 时间:2024/06/03 16:28
Subsequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11099 Accepted: 4602
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
以前做过的一道题,,,现在再刷一遍
题意:给出了n个数,然后求最短的子串,这个字串和大于s,然后输出长度
思路:肯定是不能暴力的,所以我们从头开始加,加到和大于s,然后求该字串的最优,然后再接着加
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int num[100000 + 10];int main(){int t;cin >> t;while (t--){int n, s;cin >> n >> s;memset(num, 0, sizeof(num));for (int i = 0; i < n; i++)cin >> num[i];int l, r, minn = 0x3f3f3f3f, sum = 0;l = r = 0;//子串通过l--r来限定 bool f = false;while (r < n){sum += num[r++];while (sum >= s)//找到一个之后就取最优 {f = true;//标记有答案 minn = min(minn, r - l);sum -= num[l++];}}if (f)cout << minn << endl;elsecout << 0 << endl;}return 0;}
0 0
- poj--3061--Subsequence(技巧)
- 【算法技巧】尺取法 POJ 3061 Subsequence
- poj--3061--Subsequence(贪心)
- 【POJ 3061】Subsequence(二分法)
- 【POJ】3061---Subsequence(二分)
- POJ 3061 Subsequence (尺取法)
- [ACM] POJ 3061 Subsequence (尺取法)
- poj 3061 Subsequence(尺取法)
- POJ 题目3061 Subsequence(水题)
- POJ 3061 Subsequence(二分/尺取法)
- POJ-3061Subsequence(尺取法)
- POJ 3061 Subsequence (尺取)
- POJ 3061 Subsequence(尺取法)
- POJ 3061 Subsequence(尺取法)
- POJ 3061 Subsequence(尺取法)
- POJ:3061 Subsequence(尺取法)
- poj 3061 Subsequence(尺取)
- POJ 3061 Subsequence(尺取法)
- 在eclipse中使用run as->maven build报错 -Dmaven.multiModuleProjectDirectory system propery is not set. Chec
- (4.6.7)Android性能调优工具TraceView介绍
- maven的setting.xml文件配置详情
- Dynamics CRM2016 Web API之Expand related entities & $ref & $count
- (4.6.8)Android性能调优
- poj--3061--Subsequence(技巧)
- 游戏角色
- EF架构D层中公共方法使用文档说明书-张连海-2014年12月7日
- CF_5D_FollowTrafficRules
- Codeforces 540C Ice Cave (BFS)
- C语言实现文件复制
- 93. Restore IP Addresses
- Accelerated C++:通过示例进行编程实践——习题解答(第1章)
- http请求参数综合浅记