[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中的所有字 ...
随机推荐
- oracle基础安全配置
1.oracle中用户密码复杂度配置 1)查看参数 select limit from dba_profiles where resource_name='PASSWORD_VERIFY_FUNCTI ...
- Java基础之(八):顺序结构与选择结构
顺序结构 Java的基本结构就是顺序结构 顺序结构是最简单的算法结构 它是任何一个算法都离不开的一种基本算法结构. 例子: public static void main(String[] args) ...
- 项目问题记录------Mabatis动态sql语句
现在在做一个模糊查询功能,使用两个查询条件: 条件1:下拉框选择的产品名 条件2:输入框输入的用户名 需求1:下拉框的选项是从数据库里导出来的产品名,此外,添加一个选项"全部产品" ...
- SignalR 在React/GO技术栈的生产应用
哼哧哼哧半年,优化改进了一个运维开发web平台. 本文记录SignalR在react/golang 技术栈的生产小实践. 1. 背景 有个前后端分离的运维开发web平台, 后端会间隔5分钟同步一次数据 ...
- 【UE4 C++】DateTime、Timespan 相关函数
基于UKismetMathLibrary DateTime 相关函数 Timespan 运算操作相关函数见尾部附录 /** Returns the date component of A */ UFU ...
- 【UE4 C++】Tick的三种方式、异步蓝图节点
Tick的三种方式 包括 默认 Tick (Actor.Component.UMG) TimerManager 定时器 FTickableGameObject 可以写原生 Object 也可以继承UO ...
- 微信小程序的支付流程
一.前言 微信小程序为电商类小程序,提供了非常完善.优秀.安全的支付功能 在小程序内可调用微信的API完成支付功能,方便.快捷 场景如下图所示: 用户通过分享或扫描二维码进入商户小程序,用户选择购买, ...
- 全连接层dense作用
参考来源
- Noip模拟47 2021.8.25
期望得分:55+24+53 实际得分:0+0+3 乐死 累加变量清零了吗? 打出更高的部分分暴力删了吗? 样例解释换行你看见了吗? T1 Prime 打出55分做法没删原来的暴力,结果就轻松挂55分 ...
- 热身训练2 The All-purpose Zero
The All-purpose Zero 简要题意: 长度为n的数组,每个数字为S[i],$0$是一种很神奇的数字,你想要的,它都可以变! 问这个序列的最长上升子序列长度为多少? 分析: 我们将除了 ...