算法刷题记录:P1563 [NOIP2016 提高组] 玩具谜题

题目链接

https://www.luogu.com.cn/problem/P1563

题目分析

既然是环形问题,那么直接取模来进行模拟即可,注意顺时针逆时针
顺时针的箭头是向左拐,是+,逆时针的箭头是向右拐,是-

AC代码

// Problem: P1563 [NOIP2016 提高组] 玩具谜题
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1563
// Memory Limit: 500 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#define fs first
#define sc second
typedef std::pair<std::string, int> psi;
const int N = 100010;
int n, m;
psi w[N];
// 朝内:左->顺->+,右->逆->-, 0
// 朝外:相反, 1
int main()
{
std::cin >> n >> m;
for (int i = 0; i < n; ++ i)
std::cin >> w[i].sc >> w[i].fs;
// p=0:左数, p=1右数
int p = 0, q = 0, idx = 0;
while (m -- ){
std::cin >> p >> q;
if (!w[idx].sc) // 朝内
if (p) idx = (idx + q) % n;
else idx = (idx + n - q) % n;
else // 朝外
if (p) idx = (idx + n - q) % n;
else idx = (idx + q) % n;
}
std::cout << w[idx].fs;
}
posted @   想个昵称好难ABCD  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示