题解归档 - cf888B
本文最后由方少年更新于2026 年 6 月 28 日,已超过0天没有更新。如果文章内容或图片资源失效,请留言反馈,将会及时处理,谢谢!
题解归档 - cf888B
本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。
- 本地编号:
cf888B - 本地来源:
problems/cf888B/idea.md - 题目链接:https://codeforces.com/contest/888/problem/B
- 原始标题:cf888B — Buggy Robot
思路
cf888B — Buggy Robot
题意
机器人按序执行 U/D/L/R,部分指令可能被忽略。求仍能在结束时回到 (0,0) 的前提下,最多正确执行多少条指令。
做法
U 与 D、L 与 R 必须成对抵消,互不影响。答案为 2*(min(U,D)+min(L,R))。
验证
三组样例手测;RRRUU 得 0,LDUR 得 4。
代码
来源:problems/cf888B/solution.cpp
/* Author: likely
* Time: 2026-06-08 03:40:00
**/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s;
ll n,i,j,k,zc,pd,cnt,ans;
int main(){
cin>>n>>s;
zc=pd=cnt=0;
for(i=0;i<n;i++){
if(s[i]=='U') zc++;
if(s[i]=='D') pd++;
if(s[i]=='L') cnt++;
if(s[i]=='R') k++;
}
ans=2*(min(zc,pd)+min(cnt,k));
cout<<ans<<"\n";
return 0;
}
~ ~ The End ~ ~
文章标题:题解归档 - cf888B
文章链接:https://www.fangshaonian.cn/archives/388/
最后编辑:2026 年 6 月 28 日 19:08 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)