pat甲级1044

原题链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264

晴神算法笔记题解

(第一次ac)

#include 
#include
#include
using namespace std;
const int N =100010;
int n,s,nears=100000010;
int sum[N];
int upper_bound(int left,int right,int s){
    int l=left,r=right,mid;
    while(ls){
            r=mid;
        }
        else    l=mid+1;;
    }
    return l;
}
int main()
{
    int n,s;
    scanf("%d%d",&n,&s);
    sum[0]=0;
    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]

你可能感兴趣的:(pat甲级,算法笔记)