当前位置: 首页 > news >正文

AtCoder abc461_c Variety

AT_abc461_c [ABC461C] Variety

题目描述

There are $ N $ gems. The color (represented as an integer) of the $ i $ -th gem is $ C_i $ and its value is $ V_i $ .

Choose $ K $ gems from these $ N $ gems. Here, the chosen gems must have at least $ M $ distinct colors.

Find the maximum possible total value of the chosen gems. (Such a choice is always possible in the given input.)

输入格式

The input is given from Standard Input in the following format:

$ N $ $ K $ $ M $
$ C_1 $ $ V_1 $
$ C_2 $ $ V_2 $
$ \vdots $
$ C_N $ $ V_N $

输出格式

Output the maximum possible total value of the chosen gems as an integer.

输入输出样例 #1

输入 #1

5 3 2
1 30
1 40
1 50
2 10
3 20

输出 #1

110

输入输出样例 #2

输入 #2

5 3 3
1 30
1 40
1 50
2 10
3 20

输出 #2

80

输入输出样例 #3

输入 #3

5 5 1
4 1000000000
5 1000000000
4 1000000000
5 1000000000
4 1000000000

输出 #3

5000000000

说明/提示

Sample Explanation 1

In this sample, choose three gems from five gems. The chosen gems must have at least two distinct colors.

Choosing gems $ 2, 3, 5 $ gives colors $ 1, 1, 3 $ , which are two distinct colors. Their total value is $ 40 + 50 + 20 = 110 $ , and this is the maximum possible value.

Sample Explanation 2

The gems and the number to choose are the same as in Sample Input 1, but the chosen gems must have at least three distinct colors.

Choosing gems $ 3, 4, 5 $ gives colors $ 1, 2, 3 $ , which are three distinct colors. Their total value is $ 50 + 10 + 20 = 80 $ , and this is the maximum possible value.

Sample Explanation 3

Beware of overflow.

Constraints

  • $ 1 \leq M \leq K \leq N \leq 2 \times 10^5 $
  • $ 1 \leq C_i \leq N $
  • $ 1 \leq V_i \leq 10^9 $
  • There exist gems of at least $ M $ distinct colors.
  • All input values are integers.

思路描述

比较容易想到的一题。

由于要求最大的价值,所以按价值从大到小排序。

因为至少要有 $ M $ 种宝石,所以先选出排完序后的、选出最大的、颜色互不相同的 $ M $ 颗宝石。最后再在没选过的宝石中选出 $ K-M $ 颗没选过的宝石就行了。

代码示例

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200010;
int n,m,k,c,v,cnt;
ll ans;
pair<int,int> a[N];
set<int> st;
bool f[N];
int main() {scanf("%d %d %d",&n,&k,&m);for(int i=1;i<=n;i++) {scanf("%d %d",&c,&v);a[i].first=v;a[i].second=c;}sort(a+1,a+1+n);for(int i=n;i>=1;i--) {if(st.size()==m) break;if(st.find(a[i].second)==st.end()) {st.insert(a[i].second);f[i]=true;ans+=(ll)a[i].first;cnt++;}}for(int i=n;i>=1;i--) {if(f[i]) continue;if(cnt==k) break;ans+=(ll)a[i].first;cnt++;}printf("%lld",ans);return 0;
} 
http://www.jsqmd.com/news/987955/

相关文章:

  • BRFlabbyTable与FlabbyListView对比:iOS与Android弹性列表实现差异终极指南
  • JBrowserDriver vs 传统浏览器驱动:为什么纯Java无头方案更适合自动化测试?
  • Apache 虚拟主机配置指南:从单站点到多站点
  • 3个秘诀让Continue成为你的终极AI代码审查搭档:如何实现源码可控的智能检查
  • OpenAI最强编程助手Codex:下载安装、使用指南(含使用方式、提示技巧、趋势)
  • RollToolsApi架构深度解析:构建稳定聚合API接口源的技术实践
  • 2026年6月最新版东营第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • 青岛红色合伙人防水是什么?楼长修楼官方合作资质全解析 - 青岛防水品牌推荐
  • sublime-phpcs与版本控制集成:提交代码前自动检查的实现方法
  • Polyglot-Ko-1.3B应用场景探索:客服机器人、内容创作与教育辅助
  • TanStack Ranger:打造现代化滑块组件的终极无头UI解决方案
  • 深度实战:用MarkItDown构建你的文档转换流水线
  • CAD如何修改快捷键?CAD如何自定义快捷键。
  • 2026年6月最新版固原第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • 2026年6月最新版大庆第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • 5个Claudian插件使用技巧:快速提升AI交互效率的完整指南
  • PVC 橡胶阻燃剂应用分类解析 优质生产厂家甄选指南 - 变量人生001
  • 从3D Tiles到I3S:使用loaders.gl实现不同瓦片格式的转换
  • ChatMLX核心功能全解析:多模型支持、隐私保护与39种语言能力
  • Progenitor客户端高级配置:自定义请求头、超时和认证的实用技巧
  • 批量改图片DPI的Python脚本
  • 2026深圳拆装搬家服务专业服务商推荐:家具/空调/热水器专业拆移搬迁一站式服务 - 从来都是英雄出少年
  • 3个核心场景:从零开始配置yuzu Switch模拟器,让电脑流畅运行任天堂游戏
  • Comparative-analysis-of-hourly-load-forecasting-using-PatchTST-TFT-NHiTS-and-CatBoost源代码详解:核心组件与实现原理
  • 2026年6月最新版大同第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • 高效跨平台阅读体验:Awaken EPUB阅读器的四大核心优势与实战指南
  • 人生第一双高跟鞋品牌排行:轻奢舒适兼具纪念意义 - 起跑123
  • 扎根青岛24年!本土老牌防水楼长修楼真实测评 - 青岛防水品牌推荐
  • 青岛海边小区漏水频发?盐雾气候对防水层的致命影响 - 青岛防水品牌推荐
  • 3步掌握LLPlayer:从零开始的语言学习终极指南