2 solutions

  • 1
    @ 2024-11-16 18:45:55

    本蒟蒻的代码有一点长,大佬们写的代码短多了

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll n,a[500005];
    ll t1=0,t2=0;
    int main(){
    	cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        sort(a+1,a+1+n);
        for(int i=n;i>=1;i--){
            if(n%2==0){
                if(i%2==0) t1+=a[i];
                else if(i%2!=0) t2+=a[i];
            }
            if(n%2!=0){
                if(i%2==0) t2+=a[i];
                else if(i%2!=0) t1+=a[i];
            }
        }
        cout<<t1-t2;
        return 0; 
    }
    

    Information

    ID
    606
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    # Submissions
    62
    Accepted
    13
    Uploaded By