首页 > 其他分享 >P5513 [CEOI2013] Board

P5513 [CEOI2013] Board

时间:2023-12-19 12:13:01浏览次数:22  
标签:int CEOI2013 P5513 pos long 节点 depth Board id

NOIP 模拟赛原题,赛时没切。

我们可以先考虑 \(30\) 分的部分分怎么打,\(n \le 50\)。对于每一个点去维护两个信息 \(pos\) 和 \(depth\) 分别表示当前这个点所在位置的编号是多少以及它在第几层,我们从两个点最后的状态往回考虑。然后用一个贪心的思想,深度大的点一定会先一直沿着父亲节点走,直到两个点深度相同,然后既可以选择横着直接到达也可以选择两个点一起继续往上走,把所有情况取 \(\min\) 即可。因为深度不超过 \(50\),所以点的编号不超过 \(2^{50}\),不会爆 long long。

(这里点的编号顺序是从上到下,从左到右依次升序编号)

维护 \(pos\) 值的方法:

  • 走到左儿子:\(pos \times 2\)。

  • 走到右儿子:\(pos \times 2 +1\)。

  • 走到左边节点:\(pos-1\)。

  • 走到右边边节点:\(pos+1\)。

  • 走到父亲节点:$\left \lfloor \frac{pos}{2} \right \rfloor $。

维护 \(depth\) 值的方法:

  • 走到左儿子:\(depth+1\)。

  • 走到右儿子:\(depth+1\)。

  • 走到左边节点:\(depth\) 不变。

  • 走到右边节点:\(depth\) 不变。

  • 走到父亲节点:\(depth-1\)。

然后考虑正解。我们会发现其实 \(30\) 分代码的时间复杂度是 \(O(n)\) 的,没有问题,关键的问题是存编号的 \(pos\) 会爆 long long,需要把这一部分优化。又因为这是一棵完全二叉树,操作无非就是乘二除二加一减一,所以可以想到用一个二进制串来维护操作,这个串的长度就是深度,值就是点的编号,相当于把上述两种操作合并到了一起,并且还不会爆 long long。

维护二进制串的方法:

  • 走到左儿子:\(pos_{++depth} = 0\)。

  • 走到右儿子:\(pos_{++depth} = 1\)。

  • 走到左边节点:\(pos_{depth}--\)。

  • 走到右边节点:\(pos_{depth}++\)。

  • 走到父亲节点:把 \(pos\) 的最后一位删掉。

注意有可能中间会有进位的情况,可以到最后一起进位即可。

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair <int,int> pii;
const int MAXN = 1e5 + 10;
char a[MAXN],b[MAXN];
int p[MAXN],q[MAXN],ans = 1e18,n,m,cnt1,cnt2,cnt,sum;
inline void solve1(int id){p[id - 1] += (p[id] - (p[id] & 1)) >> 1;p[id] &= 1;}
inline void solve2(int id){q[id - 1] += (q[id] - (q[id] & 1)) >> 1;q[id] &= 1;}
signed main()
{
	scanf("%s%s",(a + 1),(b + 1));
	n = strlen(a + 1),m = strlen(b + 1);
	for(int i = 1;i <= n;i++) 
	{
		if(a[i] == '1') p[++cnt1] = 0;
		if(a[i] == '2') p[++cnt1] = 1;
		if(a[i] == 'L') p[cnt1]--;
		if(a[i] == 'R') p[cnt1]++;
		if(a[i] == 'U') solve1(cnt1),cnt1--;
	}
	for(int i = cnt1;i > 1;i--) solve1(i);
	for(int i = 1;i <= m;i++) 
	{
		if(b[i] == '1') q[++cnt2] = 0;
		if(b[i] == '2') q[++cnt2] = 1;
		if(b[i] == 'L') q[cnt2]--;
		if(b[i] == 'R') q[cnt2]++;
		if(b[i] == 'U') solve2(cnt2),cnt2--;
	}
	for(int i = cnt2;i > 1;i--) solve2(i);
	cnt = min(cnt1,cnt2);
	for(int i = 0;i <= cnt && sum < 1e6;i++) sum = sum * 2 + (p[i] - q[i]),ans = min(ans,abs(sum) + 2 * (cnt - i));
	printf("%lld",ans + abs(cnt1 - cnt2));
	return 0;
}

标签:int,CEOI2013,P5513,pos,long,节点,depth,Board,id
From: https://www.cnblogs.com/Creeperl/p/17913412.html

相关文章

  • 1 K8S for Prometheus Dashboard 20211010 EN
    *[PrometheusTimeSeriesCollectionandProcessingServer](http://localhost:9090/targets?search=#pool-prometheus)*[Dashboards|GrafanaLabs](https://grafana.com/grafana/dashboards/?search=prometheus)*[Alertmanager|GrafanaLabs](https://grafana.com/g......
  • 45、Flink 的指标体系介绍及验证(2)-指标的scope、报告、系统指标以及追踪、api集成示例
    文章目录Flink系列文章一、Flink指标体系2、Scope范围1)、用户范围2)、系统范围SystemScope3)、所有变量列表4)、用户变量3、Reporter4、Systemmetrics1)、CPU2)、Memory3)、Threads4)、GarbageCollection5)、ClassLoader6)、Network7)、Defaultshuffleservice8)、Cluster9)、Availabili......
  • 修改kubernetes-dashboard默认token认证时间
    详解:k8s默认dashboardtoken时间是900s,15分钟,到期后会自动退出登陆。解决办法:修改默认时间找到部署dashboard的yaml文件增加其中这一行[root@master1~]#catrecommended.yaml#Copyright2017TheKubernetesAuthors.##LicensedundertheApacheLicense,Version2.0(th......
  • C-Kermit 连接 Microchip WBZ451 Curiosity Board实例
    TheKermitProject|NowhostedbyPanix.comNewYorkCityUSA•[email protected]…since1981~/.kermrc文件:;ConnecttoWBZ451USB-USARTVirtualCOMdefconnWBZ451{SETPORT\%1;SpecifydevicenameSE......
  • CF1500F Cupboards Jumps 题解
    题目链接点击打开链接题目解法感觉是一个融合了许多技巧的题,很巧妙题目要求\(\max(h_i,h_{i+1},h_{i+2})-\min(h_i,h_{i+1},h_{i+2})=w_i\),这可以转化成另一个只和两项有关的形式为:\(\max(|h_i-h_{i+1}|,|h_i-h_{i+2}|,|h_{i+1}-h_{i+2}|)=w_i\)令\(d_i=h_{i+1}-h_i\),所以......
  • ThingsBoard 前端项目轮播图部件开发
    前言ThingsBoard是目前Github上最流行的开源物联网平台(14.6kStar),可以实现物联网项目的快速开发、管理和扩展,是中小微企业物联网平台的不二之选。本文介绍如何在ThingsBoard前端项目中开发轮播图部件。产品需求最近接到产品经理一个需求,在TB仪表板中添加轮播图部件,......
  • 计分牌Scoreboarding代码实现(Python)
    代码地址:Scoreboarding:计算机体系结构作业——计分板模拟(gitee.com)简介此代码为高级计算机体系结构作业——计分板模拟器,使用python实现;模拟的CPU只有四个阶段,分别是发出指令(Issue)、读操作数(ReadOperator,RO)、执行计算(ExecuteComputation,EC)、写结果(WriteResult,WR)......
  • .NET 6 使用 LogDashboard 可视化日志
    在上一篇中我使用Nlog记录日志到了数据库,接下来我们进行日志的可视化展示1.关于LogDashboardlogdashboard是在github上开源的aspnetcore项目,它旨在帮助开发人员排查项目运行中出现错误时快速查看日志排查问题Tips:项目已经有两年没有更新了官网地址https://logdashboard.......
  • Prometheus Dashboard for elasticsearch exporter
    prometheus-community/elasticsearch_exporter:ElasticsearchstatsexporterforPrometheusReleases·prometheus-community/elasticsearch_exporterDashboards|GrafanaLabsElasticsearchExporterQuickstartandDashboard|GrafanaLabsPrometheusOSS|Elast......
  • 钡铼技术助力Thingsboard平台国内发展,特价网关为用户带来超值体验
    钡铼技术作为Thingsboard官方合作伙伴,致力于推动TB平台在国内的推广和应用。为了让国内用户更好的体验Thingsboard平台,钡铼技术联合Thingsboard中文网推出一批特价网关,不为赚钱只为交个朋友!钡铼技术Thingsboard硬网关南向可以采集PLC、Modbus、楼宇BACnet、电力IEC103、104、DL/......