2017百度实习生笔试题

        Peter给围栏刷油漆,围栏由N块木板组成,有K种颜料,编号从1K,最初围栏是棕色,用数字0表示,重新油漆分为M个步骤,在每一步中,Peter选择数字LR,且L<=R,并用K种颜料其中之一重新涂第LR块(包括两者)木板

编写一个算法,求出每步之后相同颜色最多木板数

输入:

5                numOfPlanks 1,10^5

5               numOfColors 1,10^5

4               numOfSteps 1,10^5

2 3 5

4 5 2

4 5 1

1 5 4

输出

3 2 2 5

vector<int> fence(int numOfPlanks, int numOfColors, int numOfSteps, int **steps )   //主函数定义的数组,需用int steps[][3] 
   
{
vector<int> res;
if(numOfPlanks <= 0 || numOfColors <= 0 || numOfSteps <= 0)
return res;
vector<int> temp(numOfPlanks,0);


for(int i=0; i<numOfSteps;i++)
{
vector<int> num(numOfColors+1,0);
int start=steps[i][0];
int end=steps[i][1];
int clo=steps[i][2];

for(auto it = temp.begin()+start-1;it!=temp.begin()+end;it++)
*it=clo;
for(auto e:temp)
{
num[e]++;
}
int t= *max_element(num.begin(),num.end());
res.push_back(t);
}
return res;
}

猜你喜欢

转载自blog.csdn.net/u012878503/article/details/80036489