zoukankan      html  css  js  c++  java
  • 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 }
     
  • 相关阅读:
    正则表达式
    c#文件流
    css
    Flash笔记 快捷键
    .Net截取指定长度的汉字,其余用...表示的方法
    远程修改最新消息管理系统(JQuery版)
    远程修改最新消息管理系统
    kubernetes 1.6 集群实践 (一)
    创建kafak集群
    创建zookeeper集群
  • 原文地址:https://www.cnblogs.com/huaxiangdehenji/p/4652308.html
Copyright © 2011-2022 走看看