• 2015 HUAS Provincial Select Contest #1 C


    题目:

    Description

    Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.

    For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.

    Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.

    Input

    First line of input consists of one integer n (1 ≤ n ≤ 3000).

    Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.

    Output

    Output single integer — minimum amount of coins the colonel has to pay.

    题目大意:输入N个数,至少要加多少个1,才能使这N个数各不相同;

    解题思路:用数组存储这N个数,用头文件algorithm中的sort函数排列,再用

                           for(int j=1;j<n;j++)
    			{
    				if(a[j]<=a[j-1])
    				{
    					l+=a[j-1]-a[j]+1;
    					a[j]+=a[j-1]-a[j]+1;
    					
    				}
    			}
    求出答案。
    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,l;
     8     int* a; 
     9     while(scanf("%d",&n)==1)
    10     {    
    11         if(n>=1&&n<=3000)
    12         {
    13               l=0;
    14             a=new int[n];
    15             for(int i=0;i<n;i++)
    16             {
    17                 scanf("%d",&a[i]);
    18                 if(a[i]<1||a[i]>n)
    19                   scanf("%d",&a[i]);
    20             }
    21             sort(a,a+n);
    22             for(int j=1;j<n;j++)
    23             {
    24                 if(a[j]<=a[j-1])
    25                 {
    26                     l+=a[j-1]-a[j]+1;
    27                     a[j]+=a[j-1]-a[j]+1;
    28                     
    29                 }
    30             }
    31             printf("%d
    ",l);
    32         }
    33     }
    34     return 0;
    35 }
     
  • 相关阅读:
    Python+Flask使用蓝图
    Python+selenium实现自动登录
    Python+Flask做个简单的表单提交程序
    第一个Flask程序
    PHP读取IIS网站列表
    在IIS7上导出所有应用程序池的方法 批量域名绑定
    Delphi判断一个字符串是否全是相同的数字
    WeTest六周年 | 匠心不改 初心不变
    WeTest压测大师链路性能监控 | 一站式压测、监控解决方案,开放免费体验预约
    WeTest自助压测1折起,最低1分钱参与Q币抽奖
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4652308.html
Copyright © 2020-2023  润新知