codeforces 279 B books

http://codeforces.com/problemset/problem/279/B 题意:给定一个序列,问一段连续的序列的和小于等于t的最长的序列的长度。 思路:尺取法。三个月前学习的了。

/* ***********************************************
Author :111qqz
Created Time :2015年12月15日 星期二 20时08分16秒
File Name :code/cf/problem/279B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E5+7;
7int n;
8int t;
9int a[N];
 1void solve()
 2{
 3    int head = 1;
 4    int tail = 1;
 5    int sum = 0 ;
 6    int ans = 0 ;
 7    while (tail<=n)
 8    {
 9//	cout<<"aaaaa?"<<endl;
10	sum = sum + a[tail];
11//	cout<<"head:"<<head<<" tail:"<<tail<<" sum:"<<sum<<endl;
12	if (sum>t)
13	{
14	    while (sum>t)
15	    {
16		sum = sum - a[head];
17//		cout<<"sum:"<<sum<<endl;
18		head++;
19	    }
20	}
21	if (sum<=t)
22	    ans = max(ans,tail-head+1);
23	tail++;
1    }
2    printf("%d\n",ans);
3}
4int main()
5{
6	#ifndef  ONLINE_JUDGE 
7	freopen("code/in.txt","r",stdin);
8  #endif
1	scanf("%d%d",&n,&t);
2	for ( int i =1 ; i  <= n ; i++) scanf("%d",&a[i]);
3	solve();
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}