题解 CF1090M 【The Pleasant Walk】
STL大法好,数组不也能用吗……
模拟题目,我使用的是vector,跑的还挺快的,772ms,目前最优解!
代码里有注释
#include <cstdio>
#include <vector>//vector头文件
using namespace std;
vector <int> vec;//定义vector
int main()
{
int n, ans = 0, x = 0, k;
scanf("%d %d", &n, &k);//读入
vec.resize(n);//将vec的元素大小设为n
for(vector <int>::iterator it = vec.begin(); it != vec.end(); ++it)//迭代器访问
{
scanf("%d", &*it);//通过*迭代器名访问
if(*it == *(it - 1))//如果相邻两个元素一样
{
x = 1;//x变为1,不是0
}
else
{
x++;//否则x自增1
}
ans = x > ans ? x : ans;//三目运算符,如果x比ans大,ans等于x,否则ans不变,相当于打擂台
}
printf("%d\n", ans);
return 0;//养成好习惯哦
}
求过

浙公网安备 33010602011771号