codeforces 712B Memory and Trident

STM32F103移植到F407后OLED不亮?主频与延时函数是关键 在嵌入式系统开发中,STM32系列芯片凭借丰富的资源和性价比被广泛使用,而OLED显示屏因其高对比度、低功耗成为交互界面的主流选择。当开发者把基于STM32F103的OLED驱动代码移植到主频更高的F407平台时,常遇到屏幕不亮的故障。其本质在于两款芯片的Cortex-M3与Cortex-M4内核主频差异显著(72MHz vs 168MHz),导致依赖指令循环的软件延时函数严重失真,进而破坏I2C通信的时钟极性与建立保持时间,使OLED控制器无法正确采样数据。同时,F407的GPIO复用映射(AF)也与F1 阅读详情

B. Memory and Trident
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:

  • An 'L' indicates he should move one unit left.
  • An 'R' indicates he should move one unit right.
  • A 'U' indicates he should move one unit up.
  • A 'D' indicates he should move one unit down.

But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.

Input

The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.

Output

If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.

Examples
input
RRU
output
-1
input
UDUR
output
1
input
RUUR
output
2
Note

In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.

In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.



首先,字符串的长度len肯定为偶数,否则不可能同时形成配对的U - D 和 R - L

设字符串中U,D,R,L的数量分别为cu,cd,cr,cl

t1 = cu + cd         t2 = cr + cl

在len为偶数的情况下,有两种情况:

1. t1和t2同时为奇

2. t1和t2同时为偶


同时为奇数的时候,则cu和cd尽可能平摊,多的一个给cr和cl,然后cr和cl再平摊,平摊的过程中记录变换的个数

同时为偶数的时候cu和cd直接平摊,cr和cl直接平摊就可以了,同样过程中记录变换的个数


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>

using namespace std;

const int maxn = 100000;
char s[maxn + 5];
int len;

int main()
{
	int cu, cd, cr, cl, ans, t1, t2;
	scanf("%s", s);
	len = strlen(s);
	if (len % 2) {
		cout << -1 << endl;
		return 0;
	}
	map<char, int> m;
	for (int i = 0; i < len; i++) {
		m[s[i]]++;
	}
	cu = m['U'];
	cd = m['D'];
	t1 = cu + cd;
	cr = m['R'];
	cl = m['L'];
	t2 = cr + cl;
	if (t1 % 2 && t2 % 2) {
		ans = max(cu, cd) - t1 / 2;
		ans = ans + max(cl, cr) - (t2 + 1) / 2;
	} else if(t1 % 2 == 0 && t2 % 2 == 0) {
		ans = max(cu, cd) - t1 /2 + max(cl, cr) - t2 / 2;
	}
	cout << ans << endl; 
	return 0;
}



携程酒店数据爬取进阶:动态接口破解与反反爬策略实战 本文详细解析了携程酒店数据爬取的进阶技巧,包括动态接口参数破解、反爬虫策略对抗及企业级采集方案实现。重点介绍了eleven参数的生成逻辑、动态Cookie维护方法,以及如何应对携程的请求指纹检测和行为模式识别。通过实战案例,帮助开发者高效获取酒店价格数据,同时遵守行业规范。 阅读详情

相关推荐

CanOpen PDO映射实战:从原理到配置的完整指南

本文详细解析了CanOpen PDO映射的原理与配置流程,从基础通信机制到实战配置步骤,涵盖禁用PDO、清空映射、逐项映射和使能PDO等关键环节。特别针对西门子G120变频器、台达伺服和倍福CX控制器等不同厂商设备,提供了具体的配置技巧和常见问题排查指南,帮助工程师快速掌握多厂商设备间的PDO通信配置。

weixin_30718391的博客 515

Codeforces #370 (Div.2) B. Memory and Trident【思维】

传送门:B. Memory and Trident 描述: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output M

魔笛手的博客 515

Transformer模型详解(图解最完整版)

Transformer由论文提出,现在是谷歌云TPU推荐的参考模型。论文相关的Tensorflow的代码可以从GitHub获取,其作为Tensor2Tensor包的一部分。哈佛的NLP团队也实现了一个基于PyTorch的版本,并注释该论文。在本文中,我们将试图把模型简化一点,并逐一介绍里面的核心概念,希望让普通读者也能轻易理解。Transformer 与 RNN 不同,可以比较好地并行训练。

2501_91695094的博客 2699

Memory and Trident

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An ‘L’ indicates he should move one unit left. An ‘R’ indicates...

无限迭代中...... 556

拆开一个烧坏的IGBT模块,手把手教你识别过压、过流、过温的“案发现场”

本文详细解析了IGBT模块失效分析的实战技巧,通过观察铝层熔化纹路、焊锡溢出形态等物理痕迹,手把手教你识别过压、过流、过温的典型失效模式。文章提供了从基础工具包到现场快速诊断的完整流程,帮助工程师精准定位IGBT模块失效的根本原因。

weixin_30655569的博客 423

CMakeTestCCompiler.cmake错误解决

CMakeTestCCompiler.cmake错误解决欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 最近编译项目的时候,遇到一个cmake的错误,具体错误信息如下: /path-to-pr

昨夜星辰的博客 2万+

Codeforces 370D - Memory and Trident(模拟)

B. Memory and Trident time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Memory is performing a walk on the two-dimensional plane, starting a

Snakeflute的博客 512

团结引擎+OpenHarmony 记录 (持续更新中)

获取不到的原因是因为二者不在同一个线程中,所以数据没有同步。而 globalThis.context 数据进行了同步。(TuanjieMainWorkerHandler.ts中可以看到)在进行绘制 UI 的时候 ,如果组件有 Native API 就可能不会显示出来,例如 web 控件就不能展示出来,刚加上权限的时候 会报错 因为缺少 reason ,添加上就不会报错了。只有纯 UI 控件是可以展示出来的,注释掉 web控件后就可以。

m_cainiaokuaifei的博客 1490

Codeforces Round #370 (Div. 2)B. Memory and Trident

地址:http://codeforces.com/problemset/problem/712/B 题目:    B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp...

weixin_30338497的博客 153

一根网线,让整个公司断网,二层环路真实事故复盘

那天晚上 23:47,监控告警突然爆了。核心交换机 CPU 100%。ARP 表暴涨。广播流量飙升。业务系统集体超时。5 分钟后,全公司断网。运维群里一句话:“是不是黑客攻击?但老网工都知道,这种现象只有一种可能:二层环路。

网络技术联盟站 346

华为OD机试双机位C卷-魔法收积木 (C/C++/Py/Java/Js/Go)

华为OD机试(机考)双机位C卷 【魔法收积木】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为od - 魔法收积木。

qq_45776114的博客 1939

Codeforces 712B. Memory and Trident

题目链接:http://codeforces.com/problemset/problem/712/B 题意:   一个人站在坐标原点处,他可以往上(U), 下(D), 左(L), 右(R), 四个方向移动,现给你一个移动序列,为了使他最后仍然能回到原点,你需要对这个序列做一些改变,每次可以改变其中一个字母,问最少的改变次数. 思路:   如果这个序列的长度是奇数,那么肯定不可能...

dimaishu7553的博客 155

34、婴儿监视器应用开发指南

本文详细介绍了基于 ESP32 和 Edge Impulse 开发婴儿监视器应用的全过程,涵盖音频处理、机器学习推理、ESP RainMaker 平台集成以及内存监控等功能。通过实现 AppAudio、AppRmaker 和 AppMem 三个核心类,完成了婴儿哭声的实时检测与远程通知,并通过合理的内存管理优化,有效节省了 ESP32 的内部 RAM 资源。文章还提供了完整的代码示例和配置说明,适用于物联网和嵌入式 AI 应用开发爱好者参考学习。

python9snake的博客 93

Codeforces 712B Memory and Trident

B. Memory and Trident time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Memory is performing a walk on the two-...

weixin_30594001的博客 114

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

codeforces 712B. Memory and Trident

题目链接:http://codeforces.com/problemset/problem/712/B 题目大意:   给出一个字符串(由'U''D''L''R'),分别是向上、向下、向左、向右一个单位,问修改若干字符,可以回到原点。回不到原点输出 -1,可以则输出最少修改的步数。 解题思路:   如果是奇数步,则不可以回到原点 直接输出-1;   否则:分别统计出向各个方向的步数,求出...

weixin_30484247的博客 125

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Codeforces Problem 712B Memory and Trident(implementation)

Codeforces Problem 712B Memory and Trident(implementation)

queuelovestack的专栏 1050

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Codeforces 712B Memory and Trident 简单贪心

B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Memory is performing a walk on the tw

你那么爱笑,家里人知道吗? 591

CodeForces 712B Memory and Trident

Description Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An 'L' indicates he should move one unit lef

弹剑听潮 334

B - Memory and Trident CodeForces - 712B(回到原点,水题)

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An ‘L’ indicates he should move one unit left. An ‘R’ indicates...

tomjobs的博客 385

CodeForces-712B. Memory and Trident(模拟)

B. Memory and Trident time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Memory is performing a walk on the two-dimensional plane, starting a

Prim 410

codeforces 712B B. Memory and Trident(水题)

题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Memory is performing a walk on the two...

weixin_30556161的博客 120
上一篇: codeforces706C Hard problem 动态规划
下一篇: hdu5902 GCD is Funny
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值