博客
关于我
Codeforces Round #639 (Div. 2) C. Hilbert's Hotel
阅读量:745 次
发布时间:2019-03-22

本文共 971 字,大约阅读时间需要 3 分钟。

要解决这个问题,我们需要检查每个数移动到指定位置后是否有重复的位置。如果有任何一个位置被多个数占据,则返回“NO”,否则返回“YES”。

思路

  • 问题分析:我们需要确保每个位置(i + a[i]) % n 之后都是唯一的。
  • 直接验证:计算每个数移动后的位置,记录到哈希表中,检查是否存在重复。
  • 步骤
    • 遍历数组中的每个元素。
    • 计算该元素移动后的新位置。
    • 使用哈希表记录位置出现次数。
    • 如果有位置出现次数超过一次,立即返回“NO”。
  • 优化考虑:直接访问和记录位置,避免复杂的计算,确保代码简洁高效。
  • 解决代码

    #include 
    #include
    using namespace std;int main() { int t; cin >> t; while (t--) { int n; cin >> n; map
    posMap; bool flag = false; for (int i = 0; i < n; ++i) { int x; cin >> x; int new_pos = (x + i) % n; if (new_pos < 0) new_pos += n; // 处理负数情况 posMap[new_pos]++; if (posMap[new_pos] > 1) { flag = true; break; } } puts(flag ? "NO" : "YES"); } return 0;}

    代码解释

  • 输入读取:首先读取测试用例的数量t。
  • 循环处理每个用例:读取n的值,初始化位置记录哈希表posMap。
  • 遍历数组元素:对于每个元素x,计算其移动后的位置new_pos。
  • 位置记录和重复检查:将new_pos记录到哈希表,检查是否有重复。如果有,设置标志并跳出循环。
  • 输出结果:根据标志值输出“NO”或“YES”。
  • 转载地址:http://pkhwk.baihongyu.com/

    你可能感兴趣的文章
    Prometheus 采集器使用详解
    查看>>
    Prometheus 黑盒监控实战
    查看>>
    prometheus+alertmanager+grafana监控部署教程
    查看>>
    Prometheus+Grafana构建智能化Kubernetes监控系统实战
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>
    prometheus安装
    查看>>
    Prometheus实战教程:监控Kafka消息
    查看>>
    Prometheus实战教程:监控mysql数据库
    查看>>
    Prometheus实战教程:监控Nginx状态
    查看>>
    prometheus常用exporter下载地址大全
    查看>>
    Prometheus快速搭建与监控Linux系统实战
    查看>>
    prometheus报警与恢复告警的格式
    查看>>
    Pytorch中安装 torch_geometric 详细图文操作(全)
    查看>>
    prometheus监控docker容器实战
    查看>>
    Prometheus监控k8s集群使用邮箱和微信告警!
    查看>>
    Prometheus监控mysq数据库实战
    查看>>
    prometheus监控nginx实战
    查看>>
    Prometheus监控redis数据库实战
    查看>>
    Prometheus监控教程:使用Grafana展示主机基本信息
    查看>>