贪心+蓝桥杯

原题路径

题目思路 : 思路很简单,肯定是贪心做法,要使总代价最小,需用那些出现次数比avg多的数来替换那些没有出现或者是出现次数少于avg的数, 所以我们存当前数每次出现的代价是多少 ,枚举每一个 0 - 9 之间的数 ,如果当前数出现的次数多于avg,那么说明需要减少,每次用最小的代价替换即可
由于我们并不知道替换的数是哪一个,但由于题目中说明了每个数都会出现 n / 10 次,所以证明一旦多出来的数必定会替换为另一个数,故我们只需要从小到大替换,将 avg 个当前数中价值最大的保存即可。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

int n;
int a[N] , b[N];
vector<int> h[N];

int main()
{
    cin >> n;
    for(int i = 1 ; i <= n ; i ++)
    {
        int a , b;
        cin >> a >> b;
        h[a].push_back(b);
    }
    
    int avg = n / 10;
    long long res = 0;
    for(int i = 0 ; i < 10 ; i ++)
    {
        int x = h[i].size();
        if(x > avg)
        {
            sort(h[i].begin() , h[i].end());
            for(int j = 0 ; j < h[i].size() - avg ; j ++)
            {
                res += h[i][j];
            }
        }
    }
    
    printf("%lld" , res);
    
    return 0;
}

相关推荐

  1. 贪心+

    2024-01-17 06:38:05       65 阅读
  2. C组-填充-贪心

    2024-01-17 06:38:05       53 阅读
  3. 2023年-三国游戏(贪心

    2024-01-17 06:38:05       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 06:38:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 06:38:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 06:38:05       82 阅读
  4. Python语言-面向对象

    2024-01-17 06:38:05       91 阅读

热门阅读

  1. python3的几个大坑

    2024-01-17 06:38:05       47 阅读
  2. qt绘制生成PDF文件

    2024-01-17 06:38:05       46 阅读
  3. 开源世界许可证Copyleft GPL LGPL MIT BSD Apache

    2024-01-17 06:38:05       65 阅读
  4. PTA——7-4 奇葩楼层 (15 分)

    2024-01-17 06:38:05       59 阅读
  5. [leetcode~数位动态规划] 2719. 统计整数数目 hard

    2024-01-17 06:38:05       53 阅读
  6. Vue和小程序的区别

    2024-01-17 06:38:05       55 阅读