美团秋招笔试第三题(剪彩带)求助帖
题目描述及代码如下。
- 我使用模拟打表法,示例通过了,但是提交通过率为0。
- 诚心求教。
- 欢迎补充题目,或者有原题链接更好~。
- 我觉得可能出错的点: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;
}