一种奇怪の思路(理论上应该行得通,但是只WA2个)
  • 板块P6510 奶牛排队
  • 楼主gin23
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/7/29 23:50
  • 上次更新2024/7/30 09:45:49
查看原帖
一种奇怪の思路(理论上应该行得通,但是只WA2个)
1266200
gin23楼主2024/7/29 23:50

我的想法是定义一个head指针和一个tail指针,tail指针从左到右遍历,遇到非递增的就把head值重新赋值为tail,然后计算一次这个递增序列的长度,并且比较和max的大小,如果更大,就更新max值;最后当tail遍历到最后一位时退出循环。这样理论上可以得到答案(应该)。

我测试了几个想到的例子都没发现问题,想看看有没有我没想到的数据

代码如下:

#include<stdio.h>
#include<math.h>
#define MAX 100000
int main()
{
	int n;
	scanf("%d",&n);
	int h[MAX];
	for(int i=0;i<n;i++)
	{
		scanf("%d",&h[i]);
	}
	h[n]=0;
	//int que[n+1];
	int head=0,tail=0;
	int max=0;
	int num;
	int cnt=0;
	for(tail=1;tail<=n;tail++)
	{
		if(h[tail]<h[tail-1])
		{
			num=tail-head-cnt;
			head=tail;
			if(num>max)
			{
				max=num;
			}
		}
		else if(h[tail]==h[tail-1])
		{
			cnt++;
		}else
		{
			cnt=0;
		}
		if(h[head]==h[tail])
		{
			head=tail;
		}
	}
	printf("%d",max);		
	return 0;
}
2024/7/29 23:50
加载中...