当前位置: 首页 > 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.zskr.cn/news/1499697.html

相关文章:

  • 青岛红色合伙人防水是什么?楼长修楼官方合作资质全解析 - 青岛防水品牌推荐
  • 深度实战:用MarkItDown构建你的文档转换流水线
  • Comparative-analysis-of-hourly-load-forecasting-using-PatchTST-TFT-NHiTS-and-CatBoost源代码详解:核心组件与实现原理
  • ChatMLX核心功能全解析:多模型支持、隐私保护与39种语言能力
  • 高效跨平台阅读体验:Awaken EPUB阅读器的四大核心优势与实战指南
  • 国际EMBA含金量高吗?2026五大高含金量国际EMBA项目解析 - 品牌2026推荐
  • pinche_xcx扩展功能开发:如何添加拼车费用计算与支付功能
  • 2026年6月最新版包头第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • CodeX Docs进阶开发:从用户到贡献者的成长之路
  • GolangBypassAV反沙箱技术:规避动态检测的关键策略
  • 2026澳洲本地留学移民机构排行 附选型避坑指南 - 互联网科技品牌测评
  • Strecs3D实战案例:悬臂梁模型的填充优化前后对比与效果分析
  • 3步解决老旧Mac蓝牙失效:OpenCore Legacy Patcher实用指南
  • 澳洲本地高成功率留学移民机构权威排行 - 互联网科技品牌测评
  • 人生第一双高跟鞋品牌排行 轻奢舒适兼具纪念意义 - 起跑123
  • statannotations API深度解析:Annotator类的完整使用指南与最佳实践
  • 如何在5分钟内上手Timeflake?Python开发者必备的高效UUID生成工具
  • OhMyREPL.jl与FZF集成:高效搜索REPL历史的完整教程
  • DuckDB-rs Parquet文件支持:大规模数据分析的完整解决方案
  • 2026年6月最新版丹东第三方CMACNAS甲醛检测治理机构口碑名单:万清CMA检测中心等5家公司深度测评万清CMA检测中心TOP1推荐 - 一修哥咨询
  • Enola Holmes:终极社交媒体用户名追踪工具,一键定位全网账号
  • MarkItDown终极指南:一键将Office文档转换为Markdown的完整教程
  • KiwiQ AI可观测性系统:实时进度监控与结构化日志分析教程
  • RealtimeMeshComponent深度解析:高性能动态网格渲染的架构设计与性能优化
  • 3步掌握OpenAI Python流式响应:告别等待,实时交互AI助手
  • 高端EMBA学员画像解析:人群特征、能力诉求与适配院校全维度分析 - 品牌2026推荐
  • 当Windows Defender突然“罢工“:从禁用状态恢复的完整指南
  • 终极指南:4步用OpenCore Legacy Patcher让旧Mac焕发新生
  • Quantum Katas深度剖析:Microsoft Quantum Development Kit中的交互式学习体验
  • NextUI Dashboard Template代码规范:ESLint与Prettier配置指南