zoukankan      html  css  js  c++  java
  • CF 546 B Soldier and Badges(贪心)

    原题链接:http://codeforces.com/problemset/problem/546/B

    原题描述:

    Soldier and Badges

    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.

    Example

    Input
    4
    1 3 1 4
    Output
    1
    Input
    5
    1 2 3 2 5
    Output
    2
     题目感觉很水啊,数量最多才3000个,就算3000个每个都是3000增加后的最大值也不会超过6000,所以我开了个10005的数组;先将每个读入,然后统计1-10000之间每个数字的个数,比如说3000这个数字有2个那么b[3000]值为3,然后让b[3001]增加2,b[3000]变为1,同时需要增加的总量增加2以此类推统计增加的总量即可,也不知道别人看不看得懂。。。。看看代码应该能理解
    AC代码如下:
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    int a[10005],b[10005];
    
    int main()
    {
        int n,sum=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            b[a[i]]++;
        }
        for(int i=1;i<=10005;i++)
        {
            if(b[i]>1)b[i+1]+=b[i]-1,sum+=b[i]-1,b[i]=1;
        }
        cout<<sum<<endl;
        return 0;
    }

      

  • 相关阅读:
    滑动窗口法与剑指offer:和为S的连续正数数列 与 和为S的两个数字
    数组中的逆序对与归并中的分治思想
    重读STL源码剖析:迭代器
    重读深度探索C++对象模型:函数
    FreeMarker笔记 前言&第1章 入门
    分享我的PL/SQL的优化设置,为开发全面提速
    迅影QQ视频查看v2.0 源码
    Invalid encoding name "UTF8". 报错 XML
    [HNOI2003]消防局的设立
    We need water!
  • 原文地址:https://www.cnblogs.com/teble/p/7202686.html
Copyright © 2011-2022 走看看