PAT (Advanced Level) 1044. Shopping in Mars (25)

双指针。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std; const int maxn=+;
long long a[maxn];
int n; long long val,Min;
struct Ans
{
int L,R;
Ans(int a,int b)
{
L=a;
R=b;
}
};
vector<Ans>v; int main()
{
Min=;
scanf("%d%lld",&n,&val);
for(int i=;i<=n;i++) scanf("%lld",&a[i]); long long sum=a[];
int p1=,p2=;
while()
{
int fail=;
if(sum>=val)
{
if(sum-val<Min)
{
Min=sum-val;
v.clear();
v.push_back(Ans(p1,p2));
}
else if(sum-val==Min)
v.push_back(Ans(p1,p2));
if(p1<=n)
{
sum=sum-a[p1];
p1++;
fail=;
}
}
else
{
if(p2+<=n){
p2++;
sum=sum+a[p2];
fail=;
}
}
if(fail==) break;
} for(int i=;i<v.size();i++)
{
printf("%d-%d\n",v[i].L,v[i].R);
}
return ;
}
上一篇:HttpClient + Testng实现接口测试


下一篇:微信支付----PC扫码支付(查询微信支付订单判断是否支付成功)