当前位置: 首页 > news >正文

美团秋招笔试第三题(剪彩带)求助帖

题目描述及代码如下。

  1. 我使用模拟打表法,示例通过了,但是提交通过率为0。
  2. 诚心求教。
  3. 欢迎补充题目,或者有原题链接更好~。
  4. 我觉得可能出错的点:int -> long long ?或者一些临界条件。
/*
美团25毕业秋招第三题,做题时间2024 8 10有一根无限长度、颜色循环的彩带,彩带的颜色为N,对彩带进行 k次剪裁。
求每次剪裁下来的彩带中有多少种颜色。6 3(彩带长为6,进行3次剪裁)
1 2 3 4 1 4 (彩带颜色)
L 2 第一次剪裁:从左边开始,剪裁长度为2 因此颜色数是 2(从1剪到2,1&2两种)
L 3 第二次剪裁:从左边开始,剪裁长度为3 因此颜色数是 3(从3剪到1,3&4&1两种)
R 12 第三次剪裁:从右边开始,剪裁长度为12,颜色是整个band的颜色(共4种)
(这个实例我修改过,只是为了解释题意)印象中 N k 都是1e9
别的条件不记得了,欢迎补充
*/#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <set>
using namespace std;
int main()
{int BanLength, cutTimes;cin >> BanLength >> cutTimes;vector<int> BanColor(BanLength);for (int i = 0; i < BanLength; i++)cin >> BanColor[i];vector<vector<int>> rec(BanLength, vector<int>(2 * BanLength, 1));for (int i = 0; i < BanLength - 1; i++){for (int j = i + 1; j < 2 * BanLength; j++){if (j - i >= BanLength){rec[i][j] = rec[0][BanLength - 1];continue;}int colorNum = 0;set<int> ColorApps;for (int k = i; k <= j; k++){if (ColorApps.find(BanColor[k % BanLength]) == ColorApps.end()){colorNum++;ColorApps.insert(BanColor[k % BanLength]);}}rec[i][j] = colorNum;}}// cout << "REC 0 1" << rec[0][1] << endl;char direc;int lengthCut;vector<pair<char, int>> everyCut(cutTimes);for (int i = 0; i < cutTimes; i++){cin >> direc >> lengthCut;everyCut[i] = pair<char, int>(direc, lengthCut);}int left = 0, right = BanLength - 1;int start, end;for (int i = 0; i < cutTimes; i++){if (everyCut[i].second >= BanLength){cout << rec[0][BanLength - 1] << endl;continue;}if (everyCut[i].first == 'L'){start = left;end = left + everyCut[i].second - 1;// cout << "out:rec[" << start << "," << end << "]" << endl;cout << rec[start][end] << endl;left = (end + 1) % BanLength;}else{end = right;start = right - everyCut[i].second + 1;while (start < 0){start += BanLength;end += BanLength;}// cout << "out:rec[" << start << "," << end << "]" << endl;cout << rec[start][end] << endl;right = start - 1;while (right < 0)right += BanLength;}}return 0;
}

http://www.lryc.cn/news/419521.html

相关文章:

  • LeetCode 算法:最小栈 c++
  • 【解压既玩】PS3模拟器v0.0.32+战神3+战神升天+各存档 整合包 ,完美不死机,没有BUG,旷世神作,强力推荐
  • bootstrap- X-editable 行内编辑
  • 【LabVIEW学习篇 - 12】:通知器
  • Oracle一对多(一主多备)的DG环境如何进行switchover切换?
  • 【浏览器插件】Chrome扩展V3版本
  • 编码器信号干扰问题、编码器选型
  • Unity入门5——材质
  • C的温故而知新:存储类别、链接和内存管理(C Primer Plus第十二章)
  • SpringBoot统一功能处理——统一数据返回格式
  • Milvus 实践(2) --- 2.4.x 安装,脚本分析,数据存储解析
  • 【蛋疼c++】千万别用std::wifstream读取Unicode UTF16文件
  • [算法] 第二集 二叉树中的深度搜索
  • 放弃使用外键时,sequelize 应该怎么使用?
  • Microsoft GraphRAG 输出的配置信息
  • 怎么判断张量的维度(形状(shape)),即如何定义行数、列数和深度的?
  • AI入门指南(二):算法、训练、模型、大模型是什么?
  • CSS已访问链接的隐私保护
  • 代码练习12-排序链表
  • Linux 内核源码分析---套接字
  • vscode配置xdebug断点调试详细教程
  • 【人工智能】Transformers之Pipeline(八):文生图/图生图(text-to-image/image-to-image)
  • AI Agent 工程师认证-学习笔记(1)——【单Agent】ModelScope-Agent
  • 【Python机器学习】树回归——将CART算法用于回归
  • 前端(HTML + CSS)小兔鲜儿项目(仿)
  • 【Rust光年纪】构建高效终端用户界面:Rust库全面解析
  • 鼠标滑动选中表格部分数据列(vue指令)
  • “5G+Windows”推动全场景数字化升级:美格智能5G智能模组SRM930成功运行Windows 11系统
  • c语言学习,isupper()函数分析
  • Adnroid 数据存储:SharedPreferences详解【SharedPreferencesUtils,SharedPreferences的ANR】