PAT 甲级 A1044 (2019/02/20)

Posted zjsaipplp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 甲级 A1044 (2019/02/20)相关的知识,希望对你有一定的参考价值。

#include<cstdio>
const int MAXN = 100010;
int sum[MAXN];
int n, S, nearS = 100000010;
int upper_bound(int L, int R, int x) {
    int left = L, right = R, mid;
    while(left < right) {
        mid = (left + right) / 2;
        if(sum[mid] > x) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}
int main(){
    //freopen("input.txt","r",stdin);
    scanf("%d %d", &n, &S);
    for(int i = 1; i <= n; i++){
        scanf("%d", &sum[i]);
        sum[i] += sum[i - 1];
    }
    for(int i = 1; i <= n; i++){
        int j = upper_bound(i, n + 1, sum[i - 1] + S);
        if(sum[j - 1] - sum[i - 1] == S) {
            nearS = S;
            break;
        } else if(j <= n && sum[j] - sum[i - 1] < nearS) {
            nearS = sum[j] - sum[i - 1];
        }
    }
    for(int i = 1; i <= n; i++) {
        int j = upper_bound(i, n + 1, sum[i - 1] + nearS);
        if(sum[j - 1] - sum[i - 1] == nearS) {
           printf("%d-%d
", i, j - 1);
        }
    }
    return 0;
} 

以上是关于PAT 甲级 A1044 (2019/02/20)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 甲级 A1067 (2019/02/20)

PAT 甲级 A1085 (2019/02/20)

PAT 甲级 A1037 (2019/02/20)

PAT 甲级 A1010 (2019/02/20)

PAT A1044 Shopping in Mars [二分]

pat甲级75分在啥水平