[at4631]Jewels
如果要选某颜色,必然会选该颜色最大的两个,那么不妨将这两个宝石权值修改为两者的平均数,显然不影响两者的和,也即不影响答案
接下来,将所有宝石按权值从大到小排序,并在权值相同时按颜色编号从小到大(使颜色相同的在一起)
此时,如果可以选前$i$个宝石,那么显然直接选即可
否则,注由于每种颜色权值最大的两个宝石权值(和颜色)都相同,那么必然是相邻的,因此不能选的原因必然是第$i$个宝石的颜色在之前没有出现过
接下来,考虑先选前$i-1$个宝石,然后进行调整:
1.选择$i$之后(包括$i$)权值最大且该颜色之前选过的宝石
2.选择$i$之后(包括$i$)权值最大且颜色相同的两个宝石,并删除$i-1$之前(包括$i-1$)权值最小且该颜色选过不小于3次的宝石
3.选择$i$之后(包括$i$)权值最大且颜色相同的三个宝石,并删除$i-1$之前(包括$i-1$)权值最小且颜色相同的两个宝石
由于前面的宝石权值不小于后面,不难证明不会删更多的宝石,因此只有这三种情况,用set维护即可

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define ll long long
5 vector<int>a[N];
6 pair<int,int>b[N];
7 multiset<int>S01,S11;
8 multiset<ll>S02,S12,S13;
9 int n,m,x,y,nn,vis[N];
10 ll sum,ans;
11 bool cmp(int x,int y){
12 return x>y;
13 }
14 int main(){
15 scanf("%d%d",&n,&m);
16 for(int i=1;i<=n;i++){
17 scanf("%d%d",&x,&y);
18 a[x].push_back((y<<1));
19 }
20 for(int i=1;i<=m;i++){
21 sort(a[i].begin(),a[i].end(),cmp);
22 if (a[i].size()){
23 y=(a[i][0]>>1)+(a[i][1]>>1);
24 a[i][0]=a[i][1]=y;
25 S12.insert(0LL+y+y);
26 if (a[i].size()>=3)S13.insert(0LL+y+y+a[i][2]);
27 }
28 for(int j=0;j<a[i].size();j++)b[++nn]=make_pair(-a[i][j],i);
29 }
30 sort(b+1,b+nn+1);
31 for(int i=1;i<=n;i++){
32 x=b[i].second;
33 if (vis[x]){
34 if (vis[x]>=2)S01.insert(a[x][vis[x]]);
35 if (vis[x]==1)S02.insert(0LL+a[x][vis[x]-1]+a[x][vis[x]]);
36 printf("%lld\n",(sum+a[x][vis[x]])/2);
37 }
38 else{
39 ans=-1e18;
40 if (!S11.empty())ans=(*--S11.end());
41 if ((!S01.empty())&&(!S12.empty()))ans=max(ans,(*--S12.end())-(*S01.begin()));
42 if ((!S02.empty())&&(!S13.empty()))ans=max(ans,(*--S13.end())-(*S02.begin()));
43 if (ans+sum<0)printf("-1\n");
44 else printf("%lld\n",(ans+sum)/2);
45 }
46 if (vis[x])S11.erase(S11.find(a[x][vis[x]]));
47 if (vis[x]+1<a[x].size())S12.erase(S12.find(0LL+a[x][vis[x]]+a[x][vis[x]+1]));
48 if (vis[x]+2<a[x].size())S13.erase(S13.find(0LL+a[x][vis[x]]+a[x][vis[x]+1]+a[x][vis[x]+2]));
49 sum+=a[x][vis[x]++];
50 if (vis[x]<a[x].size())S11.insert(a[x][vis[x]]);
51 if (vis[x]+1<a[x].size())S12.insert(0LL+a[x][vis[x]]+a[x][vis[x]+1]);
52 if (vis[x]+2<a[x].size())S13.insert(0LL+a[x][vis[x]]+a[x][vis[x]+1]+a[x][vis[x]+2]);
53 }
54 }
[at4631]Jewels的更多相关文章
- hdu.1044.Collect More Jewels(bfs + 状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1044 Collect More Jewels(bfs+状态压缩)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- 771. Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
随机推荐
- 微信小程序_快速入门02
01我们学习了环境的准备和简单的demo,现在是时候来学习简单的页面编写了,首先我们来学习一些常用的基础标签: 一.view盒子,就是类似于div的盒子,可以用来存其他元素的容器. 二.text 文本 ...
- CVE-2017-11882 漏洞分析总结 新手漏洞分析详细教程
CVE-2017-11882分析总结 注: 这篇随笔记录了CVE-2017-11882漏洞分析的整个过程,并介绍了相关调试软件的使用 漏洞信息 CVE-2017-11882属于缓冲区溢出类型漏洞,产生 ...
- PAT (Basic Level) Practice (中文)1076 Wifi密码 (15分)
1076 Wifi密码 (15分) 下面是微博上流传的一张照片:"各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B- ...
- Scrum Meeting 15
第15次例会报告 日期:2021年06月09日 会议主要内容概述: 开发工作接近尾声,接下来两天重点放在单元测试.调CSS和增加数据集数量上. 一.进度情况 我们采用日报的形式记录每个人的具体进度,链 ...
- 5.27日Scrum Metting
日期:2021年5月27日 会议主要内容概述:确定账单数据格式,确定需要添加新的图表,确定模板分享功能任务量. 一.进度情况# 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作中遇到的困难 ...
- 对mongo文档的增删改操作
在mongo db 中增加.删除.修改文档有好多方法,这里简单记录一下我所知道的一些方法. 前置条件: 1.创建study数据库 use study; 2.创建persons集合,当第一次向pers ...
- Noip模拟8 2021.6.17
T1 星际旅行 仔细一看,发现像一个欧拉路(简称一笔画). 满足"可以一笔画"的条件是: 1.所有点都有偶数条连边; 2.有偶数个点连奇数条边; 满足以上两个条件的任意一个即可一笔 ...
- boost编译中的细节问题
原文链接 http://www.cppblog.com/Robertxiao/archive/2013/01/06/197022.html 生成文件命名规则:boost中有许多库,有的库需要编译.而有 ...
- Python:Ubuntu上使用pip安装opencv-python出现错误
Ubuntu 18.04 上 使用 pip 安装 opencv-python,出现的错误如下: 1 ~$: pip install opencv-python -i https://pypi.tuna ...
- 奔跑吧linux-第三章实验
基于树莓派+openeuler平台 实验 3-2:汇编语言练习--查找最大数 1.实验目的 通过本实验了解和熟悉 ARM64 汇编语言. 2.实验要求 使用 ARM64 汇编语言来实现如下功能:在给定 ...