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; 
    }
    
    • 0
      @ 2024-11-15 14:39:27

      本题签到题,因为取数没有限制,两个人都是取剩余当中最大的,因此我们从大到小排序轮流取数即可。

      #include <bits/stdc++.h>
      using namespace std;
      
      long long a[500005];
      
      bool cmp(long long x, long long y) { return x > y; }
      
      int main() {
      int n;
      cin >> n;
      for (int i = 1; i <= n; i++) {
      cin >> a[i];
      }
      sort(a + 1, a + n + 1, cmp);
      
      int res = 0, f = 1;
      for (int i = 1; i <= n; i++) {
      res += f * a[i];
      f *= -1;
      }
      
      cout << res;
      return 0;
      
      }
      
      • 1

      Information

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