poj 3061 尺取法
来源:互联网 发布:qq影音mac官方下载 编辑:程序博客网 时间:2024/05/17 05:12
链接:http://poj.org/problem?id=3061
尺取法学习:
将头一直往前去,知道满足条件为止,然后将尾巴往前移动一位。循环这个操作,直到不能再满足条件了。
思路:
记录头和尾的下标,再记录总和。
1)将头的下标一直往前移动,直到满足总和>=S,或者超出序列,如果满足总和>=S,更新连续的最短长度。否则结束操作
2)总和减掉末尾元素,同时将尾往前移动。
循环1) 2)操作,直到结束操作。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define M 100009#define INF 0x3f3f3f3fint a[M];int n,s;int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&s); for(int i = 0;i < n;i++) { scanf("%d",&a[i]); } int sum = 0; int st = 0,en = 0; int ans = INF; for(;;) { while(en < n && sum < s) { sum += a[en++]; } if(sum < s) break; ans = min(ans,en-st); sum -= a[st++]; } if(ans != INF) printf("%d\n",ans); else printf("0\n"); } 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 (尺取法)
- 生成函数小结
- 浅谈算法和数据结构: 一 栈和队列
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- php5中的clone 浅拷贝 深拷贝
- First Missing Positive
- poj 3061 尺取法
- 驯裁轮菊干站沉胖屑倏轿腋烈呕夹
- #笔记#圣思园 JavaWeb 第65讲——jQuery,选择器
- map 用法详解
- hdu 1575 try a 矩阵快速幂
- BadParcelableException:Parcelable protocol requires a Parcelable 错误
- 九度OJ 题目1031:xxx定律
- session与cookie的区别
- 图的深度遍历