zoukankan      html  css  js  c++  java
  • POJ3061

    我也不知道为什么二分跑得比尺取法还快,这不科学

    #include<iostream>
    #include<cstdio>
    #define rep(i,j,k) for(register int i(j);i<=k;++i)
    using std::cin;
    using std::cout;
    const int N = 1e5 + 79;
    int a[N], sum[N], S, n;
    inline bool check(int len) {
    	rep(i, len, n) {
    		if(sum[i] - sum[i - len] >= S) return true;
    	}
    	return false;
    }
    
    inline void solve1() {
    	int ans(0);
    	int l = 0, r = n;
    	while(l <= r) {
    		int mid(l + r >> 1);
    		if(check(mid)) {
    			ans = mid;
    			r = mid - 1;
    		} else l = mid + 1;
    	}
    	cout << ans << '
    ';
    }
    
    inline void solve2() {
    	int ans(N);
    	int L(0), R(0),tot(0);
        while(1){
        	while(R<n&&tot<S){
        		tot+=a[++R];
    		}
    		if(tot<S) break;//ºóÃæÈ«¼ÓÉÏҲûÓÃ
    		ans=std::min(ans,R-L+1);
    		tot-=a[L++]; 
    	}
    	cout << (ans == N ? 0 : ans) << '
    ';
    }
    
    int main() {
    	std::ios::sync_with_stdio(false);
    	int T;
    	cin >> T;
    	while(T--) {
    		cin >> n >> S;
    		sum[0] = 0;
    		rep(i, 1, n) {
    			cin >> a[i];
    			sum[i] = sum[i - 1] + a[i];
    		}
    //		solve1();
    		solve2();
    	}
    	return 0;
    }
    

    本文来自博客园,作者:{2519},转载请注明原文链接:https://www.cnblogs.com/QQ2519/p/15398873.html

  • 相关阅读:
    config https in nginx(free)
    js hex string to unicode string
    alter character set
    es6
    音乐播放器
    JS模块化-requireJS
    PHP中的封装和继承
    JavaScriptOOP
    mui框架移动开发初体验
    走进AngularJS
  • 原文地址:https://www.cnblogs.com/QQ2519/p/15398873.html
Copyright © 2011-2022 走看看