Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14927 | Accepted: 6312 |
Description
Input
Output
Sample Input
<span style="font-size: 18px"><strong>2 10 15 5 1 3 5 10 7 4 9 2 8 5 11 1 2 3 4 5</strong></span>
Sample Output
<span style="font-size: 18px"><strong>2 3</strong></span>
Source
代码
<span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Scanner; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> Main{ </span><span style="color: #008000">/**</span><span style="color: #008000"> * </span><span style="color: #808080">@param</span><span style="color: #008000"> args </span><span style="color: #008000">*/</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span><span style="color: #000000"> n,s; </span><span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> map[]=<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[100000+3<span style="color: #000000">]; </span><span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> sum[]=<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[100000+3<span style="color: #000000">]; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args) { </span><span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated method stub</span> Scanner scan=<span style="color: #0000ff">new</span><span style="color: #000000"> Scanner(System.in); </span><span style="color: #0000ff">int</span> t=<span style="color: #000000">scan.nextInt(); </span><span style="color: #0000ff">while</span>(t>0<span style="color: #000000">){ n</span>=<span style="color: #000000">scan.nextInt(); s</span>=<span style="color: #000000">scan.nextInt(); </span><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<n;i++<span style="color: #000000">){ map[i]</span>=<span style="color: #000000">scan.nextInt(); } sovle(); t</span>--<span style="color: #000000">; } } </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> sovle() { </span><span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated method stub</span> <span style="color: #0000ff">int</span> res=n+1<span style="color: #000000">; </span><span style="color: #0000ff">int</span> ss=0,t=0,sum=0<span style="color: #000000">; </span><span style="color: #0000ff">for</span><span style="color: #000000">(;;){ </span><span style="color: #0000ff">while</span>(t<n&&sum<<span style="color: #000000">s){ sum</span>+=map[t++<span style="color: #000000">]; } </span><span style="color: #0000ff">if</span>(sum<<span style="color: #000000">s){ </span><span style="color: #0000ff">break</span><span style="color: #000000">; } res</span>=Math.min(res, t-<span style="color: #000000">ss); sum</span>-=map[ss++<span style="color: #000000">]; } </span><span style="color: #0000ff">if</span>(res><span style="color: #000000">n){ res</span>=0<span style="color: #000000">; } System.out.println(res); } }</span>
以上是poj3061(尺取法)的详细内容。更多信息请关注PHP中文网其他相关文章!