dd爱框框

dd爱框框

https://ac.nowcoder.com/acm/contest/11211/F

求个前缀和sum
原始等价于sum[r]-sum[l-1]>=x
即sum[r]-x>=sum[l-1]
枚举r下标二分l(l<=r)更新答案即可
#include<bits/stdc++.h>
using namespace std;
#define il inline
long long n, x;
il void read(long long& x)
{
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) { if (ch == '-')f = -1; ch = getchar(); }
while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
x *=f;
}
const int N = 1e7 + 10;
long long sum[N];
int main()
{
read(n); read(x);
for (int i = 1,val; i <= n; ++i)
{
read(sum[i]);
sum[i] = sum[i] + sum[i - 1];
}
int len = 0x3f3f3f3f,l,r;
for (int i = 1; i <= n; ++i)
{
int c = sum[i] - x;
int j = upper_bound(sum + 1, sum + i + 1, c) - sum;
if (c >= sum[j - 1])
{
if (i - j + 1 < len) {
l = j, r = i;
len = i - j + 1;
}
}
}
cout << l << " " << r << endl;
return 0;
}

全部评论

相关推荐

头像
04-26 15:00
已编辑
算法工程师
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务