首页 > 其他分享 >P6105 [Ynoi2010] y-fast trie

P6105 [Ynoi2010] y-fast trie

时间:2024-01-18 16:25:19浏览次数:41  
标签:q1 oth q2 Ynoi2010 st trie int P6105 配对

更好的阅读体验

P6105 [Ynoi2010] y-fast trie

首先把所有数对 \(C\) 取模,分类讨论:

  1. \(x+y\geq C\)

因为只会取模一次,这时显然取最大值和次大值。

  1. \(x+y<C\)

一开始的想法是对于每一个数 \(a\) 找到另一个数满足 \(a+b<C\) 的最大的 \(b\),这样是一棵外向树(环长一定 \(=2\)),修改如果修改到入度比较大的节点复杂度不对。

接下来是神奇套路:\((a,b)\) 需要被维护当且仅当 \((a,b)\) 互为最优配对。

对于加数,加进去的数 \(a\) 寻找一个最优配对 \(b\),如果 \(b\) 没有被配对直接把 \((a,b)\) 扔进堆里。否则比较 \(b\) 的配对 \(c\) 和 \(a\) 的大小,取较大者和 \(b\) 配对,较小者不与任何数配对(一定不会与任何数配对,因为较小数找到的配对数一定是 \(b\))。

对于删数,如果 \(a\) 没有配对直接删掉,否则把 \((a,b)\) 都删掉,然后插入 \(b\)。

显然上述操作一定不会遗漏互为最优的配对。

开一个堆维护所有配对,开一个 set 维护所有数,复杂度 \(\mathcal O(n\log n)\)。注意插入的数中可能有 \(0\)。

	struct Node{int v,oth;Node(int V,int Oth){v=V,oth=Oth;};};
	bool operator <(const Node nd1,const Node nd2){return nd1.v<nd2.v||(nd1.v==nd2.v&&nd1.oth<nd2.oth);}
	multiset<Node> st;
	struct Delq
	{
		priority_queue<int> q1,q2;
		inline void ins(int x){q1.e(x);}
		inline void del(int x){q2.e(x);}
		inline void update(){while(q1.size()&&q2.size()&&q1.top()==q2.top())q1.pop(),q2.pop();}
		inline int top(){return update(),q1.size()?q1.top():-inf;}
	}q;
	int n,m;
	inline void ins(int x)
	{
		auto it=st.lower_bound(Node(m-x,-1));int oth=-1;
		if(it!=st.begin())
		{
			--it;Node nd=*it;
			if(nd.oth==-1)st.erase(it),st.insert(Node(oth=nd.v,x)),q.ins(nd.v+x);
			else
			{
				if(nd.oth<x)
				{
					st.erase(it),st.erase(st.find(Node(nd.oth,nd.v)));
					st.insert(Node(nd.oth,-1)),st.insert(Node(nd.v,x));
					q.del(nd.v+nd.oth),q.ins(nd.v+x),oth=nd.v;
				}
			}
		}
		st.insert(Node(x,oth));
	}
	inline void mian()
	{
		read(n,m);int opt,x,ans=0;
		while(n--)
		{
			read(opt,x),x=(x^ans)%m;
			if(opt==1)ins(x);
			else
			{
				auto it=st.lower_bound(Node(x,-1));Node nd=*it;
				if(nd.oth==-1)st.erase(it);
				else q.del(nd.oth+nd.v),st.erase(it),st.erase(st.find(Node(nd.oth,nd.v))),ins(nd.oth);
			}
			ans=q.top();
			if(st.size()>=2)Mmax(ans,((--st.end())->v+(--(--st.end()))->v)%m);
			if(ans<0)puts("EE"),ans=0;else write(ans,'\n');
		}
	}

标签:q1,oth,q2,Ynoi2010,st,trie,int,P6105,配对
From: https://www.cnblogs.com/WrongAnswer90-home/p/17972741

相关文章

  • 浅谈 Trie 树
    浅谈Trie树什么是Trie树?Trie树,又称字典树,可用于存储单词。Trie树的根节点不表示任何字母,但是除了根节点的所有字母都表示一个字母。任何一个单词,都可以用一条从根节点出发的路径表示。在路径的终点做一个“结束”标记,对应一个单词的结尾。举个例子:要存储work,word,wo......
  • 【字典树/trie树】实现高效插入和查询字符串的数据结构
    本文是https://www.acwing.com/problem/content/description/837/的总结,有兴趣可以做做字典树的实现依赖于树结构,有两种操作,1是插入字符串,2是查找字符串。使用idx维护最新的结点下标。如下图,假设我们维护一个 可以看到,我们维护了一个树形结构储存了左边的字符串,但......
  • Trie字符串统计题解
    维护一个字符串集合,支持两种操作:"Ix"向集合中插入一个字符串x;"Q×"询问一个字符串在集合中出现了多少次。共有N个操作,输入的字符串总长度不超过105,字符串仅包含小写英文字母。输入格式第一行包含整数N,表示操作数。接下来N行,每行包含一个操作指令,指令为"l×"或"Q×"中的一种。......
  • 记一次执行yum命令报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/
    执行yum安装命令时报如下错误:root@docker-test101~]#vi/etc/hosts[root@docker-test101~]#yuminstallopenssl*-yLoadedplugins:fastestmirror,langpacksCouldnotretrievemirrorlisthttp://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=sto......
  • pnpm切换源后报错ERR_PNPM_REGISTRIES_MISMATCH
    工具都是有利有弊,使用pnpm过程中经常会出现一个错误:Thismodulesdirectorywascreatedusingthefollowingregistriesconfiguration:{"default":"https://registry.npmjs.org/"}.Thecurrentconfigurationis{"default":"https://registry.npm.taob......
  • pnpm切换源后报错ERR_PNPM_REGISTRIES_MISMATCH
    工具都是有利有弊,使用pnpm过程中经常会出现一个错误:Thismodulesdirectorywascreatedusingthefollowingregistriesconfiguration:{"default":"https://registry.npmjs.org/"}.Thecurrentconfigurationis{"default":"https://registry.npm.ta......
  • Why the developed country choose the countries of southeast Asia to build proces
    ThedevelopedcountrieschoosecountriesinSoutheastAsiatobuildprocessingfactoriesandutilizetheirlaborforceforvariousreasons.Someofthekeyfactorsthatcontributetothisdecisionincludethelowcostoflabor,favorablegovernmentpolici......
  • 【Nacos】启动报错 failed to req API:/nacos/v1/ns/instance after all servers([xxx
    1  com.alibaba.nacos.api.exception.NacosException:failedtoreqAPI:/nacos/v1/ns/instanceafterallservers([xxx])tried:ErrCode:403,ErrMsg:<html><body><h1>Whitelab#我的配置spring.application.name=virtuous-base-servicespring.profiles.......
  • k8s1.26部署etcd集群挂载nfs failed to save Raft hard state and entries","error":"
    背景:使用helm部署apisix时会把etcd也一起部署了,etcd数据需要持久化的,这边因为测试环境使用nfs,挂载nfs时发现只有一个etcd节点启动正常其他两个均报错如下:failedtosaveRafthardstateandentries","error":"input/outputerror截图:排错过程:1查看节点是否都可以挂载nfs  ---......
  • Trie树
    Trie树(字典树)Trie树,是使用树形结构来存储字符串的一种方式,由于使用了树形结构,大大加快了字符串的存储以及多次查询的速度。Trie树一般用于多字符串存储,以及查询一个字符串的出现次数时使用,或者查询以某段字符为前缀的字符串也可。关于trie树的构造以及树形图像,请看这篇博......