原题链接:https://www.spoj.com/problems/AMR11D/en/
暑期训练VJ链接:https://vjudge.net/contest/237052#problem/D
AMR11D - Wizarding Duel
You are the organizer of a Wizarding Duel tournament at Hogwarts. N players participated in the tournament and each player played with every other player exactly once, and each such game resulted in a winner and a loser (no drawn games). It is now time to declare the results. Each player's score is the number of games the player won. However, you realize that something possibly went wrong with the scoring system, and the scores that you have noted down might be wrong. In fact, the scores you have noted down could simply not be possible. For example, suppose there are 3 players and the scores that you noted are 0,0 and 2. Clearly this is not possible as the game between the first two players must have had a winner and thus both the players cannot have score 0.
While you have no way to figure out the correct scores of each player, you've decided to set the scores right by adjusting the scores of some players. The new set of scores should be possible to have arisen in the tournament, otherwise there would be ample proof that the scoring is wrong. However, making drastic changes might cause suspicion. So you've decided to adjust the scores so that the sum of the absolute differences between the old and the new scores of each player is minimized. In other words, if the original scores which you noted are a1,..,aN, you must change them to the series of possible scores b1,...bN such that the sum |ai - bi| is minimized.
Input (STDIN):
The first line contains the number of test cases T. T test cases follow. Each case contains an integer N on the first line, followed by the set of scores which you have noted down: a1..aN.
Output (STDOUT):
Output T lines, one for each test case, containing the minimum sum of absolute values in order to make the scorecard a valid one.
Constraints:
1 <= T <= 20
2 <= N <= 50
0 <= ai <= 100
Sample Input:
2
3
0 0 2
5
5 3 2 1 4
Sample Output:
1
5
1 #include<stdio.h> 2 #include<algorithm> 3 #include<stdlib.h> 4 #include<math.h> 5 using namespace std; 6 int DP[55]; 7 int a[55]; 8 int main() 9 { 10 int T; 11 scanf("%d",&T); 12 while (T--) 13 { 14 int N;long long ans=0; 15 long long sum1=0; 16 scanf("%d",&N); 17 for (int i=0;i<N;i++) 18 scanf("%d",&a[i]); 19 sort(a,a+N); 20 sum1=a[0]; 21 for (int i=2;i<=N;i++)//前i个人 22 { 23 sum1+=a[i-1];//前i个人的和 24 if(sum1>=i*(i-1)/2)//如果大于直接继续,最后在统一减 25 { 26 continue; 27 } 28 else//如果小于则需要添加场次 29 { 30 ans+=abs(sum1-(i*(i-1)/2)); 31 sum1=i*(i-1)/2; 32 } 33 } 34 //N个人的时候 35 if(sum1>=N*(N-1)/2)//统一减 36 { 37 ans+=sum1-(N*(N-1)/2); 38 } 39 else//如果小于则添加场次 40 ans+=(N*(N-1)/2)-sum1; 41 printf("%lld\n",ans); 42 } 43 return 0; 44 }