思路:真绝了,开了longlong从80分到了100分。因为一个特判st值影响我AC,那个单独的特判竟然有问题。我想的是如果有a[i] + a[i+1]则将状态值st标为true。最后在输出结果之前先看st的值,如果他为false,则说明没有两盒子的和大于x,就输出0。
但是为什么不可以呢。
代码1:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
int n,x;
int a[100010];
bool st;//特判所有相邻糖果加和小于x
int cnt;
int main()
{
//优先吃后面盒子的糖果
scanf("%d%d",&n,&x);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i] > x){
cnt += abs(x - a[i]);
a[i] = a[i] - abs(x - a[i]);
}
if(a[i]+a[i-1] > x && i > 1){
if(a[i-1] <= x){
cnt += (a[i] + a[i-1] - x);
a[i] = a[i] - (a[i] + a[i-1] - x);
}
if(a[i-1] > x){
cnt += a[i-1] - x;
a[i] = a[i] - (a[i] + a[i-1] - x);
}
st = true;
}
}
// for(int i=1;i<=n;i++) cout<<a[i]<<endl;
if(st==false){
printf("0");
return 0;
}
printf("%d\n",cnt);
return 0;
}
结果1:
代码2:开了longlong
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
int n,x;
int a[100010];
bool st;//特判所有相邻糖果加和小于x
LL cnt;
int main()
{
//优先吃后面盒子的糖果
scanf("%d%d",&n,&x);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i] > x){
cnt += abs(x - a[i]);
a[i] = a[i] - abs(x - a[i]);
}
if(a[i]+a[i-1] > x && i > 1){
if(a[i-1] <= x){
cnt += (a[i] + a[i-1] - x);
a[i] = a[i] - (a[i] + a[i-1] - x);
}
if(a[i-1] > x){
cnt += a[i-1] - x;
a[i] = a[i] - (a[i] + a[i-1] - x);
}
st = true;
}
}
// for(int i=1;i<=n;i++) cout<<a[i]<<endl;
if(st==false){
printf("0");
return 0;
}
printf("%lld\n",cnt);
return 0;
}
结果2:
代码3:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
int n,x;
int a[100010];
bool st;//特判所有相邻糖果加和小于x
LL cnt;
int main()
{
//优先吃后面盒子的糖果
scanf("%d%d",&n,&x);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
if(a[i] > x){
cnt += abs(x - a[i]);
a[i] = a[i] - abs(x - a[i]);
}
if(a[i]+a[i-1] > x && i > 1){
if(a[i-1] <= x){
cnt += (a[i] + a[i-1] - x);
a[i] = a[i] - (a[i] + a[i-1] - x);
}
if(a[i-1] > x){
cnt += a[i-1] - x;
a[i-1] = x;
}
st = true;
}
}
// for(int i=1;i<=n;i++) cout<<a[i]<<endl;
printf("%lld\n",cnt);
return 0;
}
结果3: