NC252836. Evil Capitalist
描述
输入描述
The first line contains one integer, representing the number of workers.
The second line containsintegers
;
represents the current salary of the
-th worker.
输出描述
A single integer, represents the minimum amount of money you need to spend.
示例1
输入:
4 100 101 1 101
输出:
1
说明:
C++(clang++ 11.0.1) 解法, 执行用时: 112ms, 内存消耗: 2032K, 提交时间: 2023-07-31 23:49:43
#include <bits/stdc++.h> using namespace std; long n,sum,mx,cnt,now,a[200001],i; int main() { cin>>n; while(i<n) cin>>a[i++]; sort(a,a+n); a[n]=2e9; for(i=0;i<n;i++){ if(a[i]==a[i+1]) cnt++; else{ now=cnt%2?0:a[i+1]-a[i]; sum+=now; mx=max(mx,now); cnt=0; } } cout<<sum-mx; return 0; }
C++(g++ 7.5.0) 解法, 执行用时: 106ms, 内存消耗: 2112K, 提交时间: 2023-07-14 00:03:18
#include <bits/stdc++.h> using namespace std; long n,sum,mx,cnt,now,a[200001],i,j; int main() { cin>>n; while(j<n) cin>>a[j++]; sort(a,a+n); a[n]=2e9; for(;i<n;i++){ if(a[i]==a[i+1]) cnt++; else{ now=cnt%2?0:a[i+1]-a[i]; sum+=now; mx=max(mx,now); cnt=0; } } cout<<sum-mx; return 0; }