[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中的所有字 ...
随机推荐
- uoj279题目交流通道(dp)
题目大意: 神犇星球有 \(n\) 座小城.对于任意两座小城 \(v,u\)\((v≠u)\),吉米多出题斯基想在 \(v,u\) 之间建立一个传送时间为 \(w(v,u)\)的无向传送通道,其中 \ ...
- MySQL灵魂拷问:36题带你面试通关!
大家好,我是大彬~ 今天给大家分享MySQL常考的面试题,看看你们能答对多少. 本期MySQL面试题的目录如下: 事务的四大特性? 事务隔离级别有哪些? 索引 什么是索引? 索引的优缺点? 索引的作用 ...
- hadoop学习笔记:运行wordcount对文件字符串进行统计案例
文/朱季谦 我最近使用四台Centos虚拟机搭建了一套分布式hadoop环境,简单模拟了线上上的hadoop真实分布式集群,主要用于业余学习大数据相关体系. 其中,一台服务器作为NameNode,一台 ...
- 前端HTML元素的类型名称
<div id="content" class="para" style="width:800px;"> <p>He ...
- Beta阶段第四次会议
Beta阶段第四次会议 时间:2020.5.20 完成工作 姓名 工作 难度 完成度 ltx 1.对小程序进行修改2.提出相关api修改要求 轻 85% xyq 1.设计所需api文档2.编写相关技术 ...
- Mac 系统如何利用软链接在根目录创建文件夹?
作者:泥瓦匠 出处:https://www.bysocket.com/2021-10-26/mac-create-files-from-the-root-directory.html Mac 操作系统 ...
- MOSFET管驱动电路的设计
https://wenku.baidu.com/view/ae727da5caaedd3382c4d3b9.html?mark_pay_doc=2&mark_rec_page=1&ma ...
- Xpath语法学习记录
高级参考:https://blog.csdn.net/wudaoshihun/article/details/82226122 举例: 1 <!DOCTYPE html> 2 <ht ...
- GDI+图形图像技术1
System.Drawing命名空间提供了对GDI+基本图形功能的访问,其中一些子命名空间中提供了更高级的功能. GDI+由GDI发展而来,是Windows图形显示程序与实际物理设备之间的桥梁. GD ...
- # Host xx.xxx.x.xxx found: line 1 /root/.ssh/known_hosts updated. Original contents retained as /root/.ssh/known_hosts.old
一直可以ssh登录远程服务器,突然不行了. 原因:远程服务器最近打过安全补丁,安全标识已经更新. 清理本机的安全密匙即可 解决办法: #ssh-keygen -R "需要远程服务器ip地址& ...