首页 > 其他分享 >CF ROUND946 (DIV. 3)D:构造

CF ROUND946 (DIV. 3)D:构造

时间:2024-05-29 16:01:25浏览次数:21  
标签:rover 10 point ROUND946 CF meter helicopter DIV instructions

Ingenuity-2

题面翻译

你有两个机器人,分别是 Rover (R)Helicopter (H)。它们初始都在同一平面直角坐标系 \(xOy\) 的 \((0, 0)\) 处。定义北为 \(y\) 轴正方向,东为 \(x\) 轴正方向。

现有一串由以下四个字符指令组成的指令串 \(s\):

  • N 向北移动一步,即 \((x, y) \to (x, y + 1)\);
  • S 向南移动一步,即 \((x, y) \to (x, y - 1)\);
  • E 向东移动一步,即 \((x, y) \to (x + 1, y)\);
  • W 向西移动一步,即 \((x, y) \to (x - 1, y)\);

问是否存在一种将每个指令都分配给两个机器人的方法,使得它们最终停在同一个位置?如果存在,给出一组构造;如果不存在,报告无解。

题目描述

Let's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates $ (0, 0) $ . A set of instructions $ s $ consisting of $ n $ instructions of the following types was specially developed for them:

  • N: move one meter north (from point $ (x, y) $ to $ (x, y + 1) $ );
  • S: move one meter south (from point $ (x, y) $ to $ (x, y - 1) $ );
  • E: move one meter east (from point $ (x, y) $ to $ (x + 1, y) $ );
  • W: move one meter west (from point $ (x, y) $ to $ (x - 1, y) $ ).

Each instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all $ n $ instructions, the helicopter and the rover end up at the same point, or determine that this is impossible.

输入格式

The first line of input contains $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the number of instructions.

The second line of each test case contains a string $ s $ of length $ n $ consisting of the characters 'N', 'S', 'E', 'W' — the sequence of instructions.

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10 ^ 5 $ .

输出格式

For each test case, if the required distribution of instructions exists, output a string $ p $ of length $ n $ consisting of the characters 'R', 'H'. If the $ i $ -th operation should be executed by the rover, then $ p_i=\text{R} $ , if the $ i $ -th operation should be executed by the helicopter, then $ p_i=\text{H} $ . If there are multiple solutions, output any of them.

Otherwise, output NO.

样例 #1

样例输入 #1

10
6
NENSNE
3
WWW
6
NESSWS
2
SN
2
WE
4
SSNN
4
WESN
2
SS
4
EWNN
4
WEWE

样例输出 #1

RRHRRH
NO
HRRHRH
NO
NO
RHRH
RRHH
RH
RRRH
RRHH

提示

Let's consider the first example: the string $ S = \texttt{NENSNE} $ . One of the possible solutions, shown in the figure below, is $ p = \texttt{RRHRRH} $ , using which both the rover and the helicopter will end up one meter north and one meter east.

For WWW, the solution is impossible.

AC_code

这里的思路是当前给1,则后1个肯定给2(同理依次)
此时若第一个多一个N则应有多一个S抵消(N==S),由于每个机器人要至少给一条:则可以EW(相反与NS)先给2,再给1

#include <iostream>

using namespace std;

const int N = 2 * 1e5 + 10;

char ans[N], s[N];//公共串ans记录路径
int n, w[128];
void solve() 
{
    cin >> n >> s + 1;
    int ok = 0;

    //标记当前应分配给谁
    w['N'] = w['S'] = 0;
    w['W'] = w['E'] = 1;
    
    for(int i = 1; i <= n; ++ i) {
        ans[i] = w[s[i]] == 0 ? 'R' : 'H';
        // cout << i << ' '<< ans[i] << endl;
        //看是否有ok=3(至少12都接受过指令)
        if(ans[i] == 'R') ok |= 1;
        if(ans[i] == 'H') ok |= 2;
        w[s[i]] ^= 1;//0变1,1变0
    }
    //虽然新solve读入串会覆盖旧串,但下面puts输出ans所有的值(采用ans[n+1] = 0避免)
    ans[n + 1] = 0;
    if(ok == 3 && w['N'] == w['S'] && w['W'] == w['E'])
        puts(ans + 1);
    else 
        puts("No");
    return ;
}

int _;
int main()
{
    cin >> _;
    while(_ --) {
        solve();
    }
    return 0;
}

感觉发现性质和运用性质的能力一样重要
构造N == S,E == W即可,并且位运算在优化代码和构造中真的有很大的用处

本人思路

求除几种抵消操作后,是否可以构造出相同操作,抵消操作:NS上下,WE左右,以及NESW自转一圈

并错误的认为如果序列是奇数一定是"No"

发现有路径问题,这时采用的是枚举主要R的做法,先减去NESW转圈,再减去NS,EW抵消问题,想着后续处理路径,眼瞎忽略了至少HR各走一步限制,导致后续问题,并且后续处理路径也很容易夭折

标签:rover,10,point,ROUND946,CF,meter,helicopter,DIV,instructions
From: https://www.cnblogs.com/OVSolitario-io/p/18218431

相关文章

  • CF Div. 2 C
    CodeforcesRound948(Div.2)C.NikitaandLCM标签暴力枚举数据结构[[数学]]题目大意有长度为n的数组a,求a中最长子序列的长度,子序列要满足\(lcm(subArray(a))\notina\)\(1\len\le2000\),\(1\lea_i\le10^9\),对于t个测试案例,\(sum(n)\le2000\)思路1.......
  • CF1843E Tracking Segments
    题目链接:https://www.luogu.com.cn/problem/CF1843E思路:题目要求至少第几次修改后满足给定的一个区间是美丽区间.我们发现修改操作是有单调性的,随着修改次数的增加,那么满足的美丽区间数量一定会保持不变或增多.因此我们选择二分答案,二分修改次数.二分答案的check函数就根......
  • 大学生看过来:CCF-智谱清言-全国智能体开发比赛
    前言Al界的新话题——基于大模型(LLM)的智能体正在成为大学生日常工具,它们正式让人人都是产品经理变得有可能。它们模仿人类决策过程,拥有无限的应用潜力和强大功能。想要提升学习效率、丰富自己的课余生活、实现人机协同?智能体让你轻松搞定!只要能看到需求有痛点,就可以创作智......
  • CCF-CSP真题《202403-1 词频统计》思路+python满分题解
    哇q(≧▽≦q),第一次写博客,请大家多多关照○| ̄|_ 看到没啥人提供202403的第一题解题思路及python代码,刚好写完,心血来潮想分享解题思路,就写下了这篇博客,有其他的编码版本,欢迎大家一起探讨呀(虽然我是算法菜鸟┗(T﹏T)┛,但有问题,我会尽力回答的!!!)好了废话不多说,上解题思路!大概想了......
  • 「杂题乱刷」CF1977B
    题目链接CF1977B(luogu)CF1977B(codeforces)解题思路考虑通用做法。我们发现如果直接用二进制来表示的话这个数会只包含\(0,1\)这两个数字。发现这时阻碍我们构造的是连续的数字\(1\)。考虑消除连续的数字\(1\)。容易发现连续的数字\(1\)可以转化成将这一段最高位......
  • 「杂题乱刷」CF1977C
    题目链接CF1977C(luogu)CF1977C(codeforces)解题思路首先这题有一个简单的思路,就是当这个序列的LCM大于\(10^9\)时,显然取所有数字数字是合法的。然后我们接下来考虑LCM小于等于\(10^9\)的情况。发现这种情况LCM很小,且有一个简单的结论,就是你在序列中任选一个子......
  • codeforces round 948(Div2)
    A题目过简单,略B.构造+二进制点击查看代码#include<bits/stdc++.h>#defineLLlonglongLLx,ans[40];boolyes[40];intmain(){std::ios::sync_with_stdio(0),std::cin.tie(0);intT;std::cin>>T;while(T--){std::cin>>x;for(LLi......
  • 从CF1660F2看同余分组
    https://codeforces.com/contest/1660/problem/F2同余分组,树状数组维护逆序对先承继F1的做法,维护一个前缀和数组,让s[i]=='+'为\(1\),s[i]=='-'为\(-1\)。那么要满足两个条件:\(pre_r-pre_l\leq0\)要么加减号相同,要么减号更多(只有减号能减少)\(pre_r-pre_l......
  • 【最新区块链论文录用资讯】CCF A—INFOCOM 2024 共17篇
    Conference:IEEEInternationalConferenceonComputerCommunicationsCCFlevel:CCFACategories:计算机网络Year:2024Num:17AGenericBlockchain-basedSteganographyFrameworkwithHighCapacityviaReversibleGAN通过可逆GAN实现高容量的基于区块链的通用隐......
  • Codeforces Round 948 (Div. 2)
    A.LittleNikita题意:\(n\)步操作,\(+1\)或\(-1\),最终结果是否等于\(m\)思路:设\(+1\)的操作次数为\(x\),\(-1\)的操作次数为\(y\)\[x+y=n\\x-y=m\]\[x=(n+m)/2\\y=(n-m)/2\]\((n-m)\)和\((n+m)\)均为偶数,即\(n\)和\(m\)均为偶数或同为奇数,且\(n>=m\)代码:voidsolve()......