SPOJ - AMR11D

原题链接:https://www.spoj.com/problems/AMR11D/en/

暑期训练VJ链接:https://vjudge.net/contest/237052#problem/D

AMR11D - Wizarding Duel

no tags 

 

 

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
Time Limit: 3 seconds
Memory Limit: 64 MB
Sample Input:
2
3
0 0 2
5
5 3 2 1 4
Sample Output:
1
5

 

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

题意:每个人都和其他人打“一场”。然后他给出每个人胜出的场次,问你这个场次是否合理,如果不合理要怎么样修改赢的场次让其合理。(最小修改多少分,修改可以是增可以是减)。首先可以知道这题是无序的,我们先帮他小-大排序。排序完成之后。按照多少个人来。i个人,则和一定是i*(i-1)/2 
1:如果和sum>=i*(i-1)/2 我们则不需要管,最后看多了多少直接减去就行了。
2:如果和sum<i*(i-1)/2我们则需要帮他添场次,添加的场次数为i*(i-1)/2-sum。
因为如果前面都因为小于不满足的话后面肯定也不满足。
 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 }

 

转载于:https://www.cnblogs.com/bendandedaima/p/9295368.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值