AcWing 1528. 火星购物( 前缀和 + 双指针 )
原题链接
中等
作者:
Value
,
2020-06-01 08:52:38
,
所有人可见
,
阅读 888
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1E5 + 10;
int s[N];
int main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) scanf("%d", &s[i]);
for(int i = 1; i <= n; i ++ ) s[i] += s[i - 1];
int i, j;
i = j = 1;;
int vmin = 0x3f3f3f;
while(i <= j && j <= n){
int value = s[j] - s[i - 1];
if(value < m || j == i) j ++ ;
else{
vmin = min(vmin, value - m);
i ++ ;
}
}
i = j = 1;
while(i <= j && j <= n){
int value = s[j] - s[i - 1];
if(value - m == vmin) cout << i << "-" << j << endl;
if(value <= m || j == i) j ++ ;
else i ++ ;
}
return 0;
}
你这个写错了
你倒是说哪里写错了啊!
大佬果然是大佬哇,代码精简,为什么我写的好长,