1.题目描述:点击打开链接
2.解题思路:本题是典型的二分搜索题,二分答案后验证是否满足和大于等于S即可。
3.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<functional> using namespace std; typedef long long LL; #define N 100000+10 int a[N]; LL sum[N]; int n; LL s; bool ok(int len) { for (int L = 0; L <= n - len;L++) if (sum[L + len] - sum[L] >= s) return true; return false; } int main() { //freopen("t.txt", "r", stdin); while (~scanf("%d%d", &n, &s)) { memset(a, 0, sizeof(a)); memset(sum, 0, sizeof(sum)); for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } if (sum[n] < s)printf("0\n"); else { int L = 0, R = n; while (L < R) { int M = L + (R - L) / 2; if (ok(M))R = M; else L = M + 1; } cout << L << endl; } } return 0; }
时间: 2024-10-09 04:09:17