3 solutions

  • 1
    @ 2026-8-7 9:48:24
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    long long a[N],b[N];
    int n;
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr);
    	cin>>n;
    	for(int i=1;i<=n;i++) cin>>a[i];
    	b[1]=a[1];
    	long long tmp1=0,tmp2=0;
    	//tmp1表示b2~bn中所有正数的和
    	//tmp2表示b2~bn中所有负数取绝对值之后的总和
    	for(int i=2;i<=n;i++){
    		b[i]=a[i]-a[i-1];
    		if(b[i]>0) tmp1=tmp1+b[i];
    		else tmp2=tmp2+abs(b[i]);
    	}
    	cout<<max(tmp1,tmp2)<<"\n";
    	cout<<abs(tmp1-tmp2)+1;
    	return 0;
    }
    

    Information

    ID
    172
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    6
    Tags
    # Submissions
    53
    Accepted
    18
    Uploaded By