尺取法模板 【pascal】(附poj 3061&poj 2739)
来源:互联网 发布:惠普427网络打印机驱动 编辑:程序博客网 时间:2024/06/10 19:40
尺取法,详细的介绍以及图片网上有很多,这里不再啰嗦,
个人更喜欢叫它毛虫算法,以至于在程序中的名字都叫caterpillar(毛虫),
因为它的移动和毛毛虫真的是太像了=w=
时间复杂度:O(n)
直接贴代码(模板)
poj 3061
var t,n,m,ans :longint; i :longint; a :array[0..100010] of longint;procedure caterpillar;var l,r,sum:longint;begin l:=1;r:=1;sum:=a[1]; while true do begin while (r<n) and (sum<m) do begin inc(r); inc(sum,a[r]); end; // if (sum<m) then exit; // if r-l+1<ans then ans:=r-l+1; // dec(sum,a[l]); inc(l); end;end;begin read(t); while (t>0) do begin dec(t); read(n,m); ans:=n+1; for i:=1 to n do read(a[i]); caterpillar; if ans=n+1 then writeln(0) else writeln(ans); end;end.
poj 2739
var n,ans,m :longint; i :longint; a :array[0..10010] of longint;procedure find(x:longint);var j:longint;begin for j:=2 to trunc(sqrt(x)) do if (x mod j=0) then exit; inc(n); a[n]:=x;end;procedure caterpillar;var l,r,sum:longint;begin l:=1;r:=1;sum:=2; while true do begin while (r<n) and (sum<m) do begin inc(r); inc(sum,a[r]); end; // if (sum<m) then exit; if (sum=m) then inc(ans); // dec(sum,a[l]); inc(l); end;end;begin n:=1; a[1]:=2; for i:=3 to 10000 do find(i); // read(m); while (m<>0) do begin ans:=0; caterpillar; writeln(ans); read(m); end;end.
——by Eirlys
0 0
- 尺取法模板 【pascal】(附poj 3061&poj 2739)
- POJ 3061 Subsequence 尺取法附基础模板
- 尺取法模板题 poj 3061 Subsequence
- 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
- POJ 3061 Subsequence (尺取法)
- [ACM] POJ 3061 Subsequence (尺取法)
- 可靠数据传输
- 分析一段H264视频数据
- 计算机知识
- ajax的使用
- NOIP 提高组 初赛 四、阅读程序写结果 习题集(四)NOIP2004-NOIP2005
- 尺取法模板 【pascal】(附poj 3061&poj 2739)
- PIC32单片机harmony开发环境 - i2c例程和代码分析
- springmvc的简单配置
- 锋利的jQuery读书笔记-第2章 jQuery选择器
- lintcode,二叉树的路径和
- bzoj 2802: [Poi2012]Warehouse Store 贪心+优先队列
- QT之CheckBox单项选择与多项选择
- 20. Valid Parentheses*
- 汇编学习float与double速率问题