soj:2876: Antimonotonicity
http://acm.scu.edu.cn/soj/problem.action?id=2876
简介题意,给test组测试,每组是n个数串,求满足a<=b>=c<=d>=e...这样的最长子串长度多少
#include<cstdio>
using namespace std;
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
int n;
scanf("%d",&n);
int length = 1;
int num;
scanf("%d",&num);
int last = num;
int flag = true;
for(int i = 1; i < n; i++)
{
scanf("%d",&num);
if(flag && (num<=last))
{
length++;
flag = !flag;
}
else if(!flag&&(num>=last))
{
length++;
flag = !flag;
}
last = num;
}
printf("%d\n",length);
}
return 0;
}
直接写就行了吧。。。
int flag好像写的不对,应该写bool flag
这个就是该大就看大不大,该小就看小不小,不论满不满足,last都等于刚才比较过的数。
这就有个问题,有人会问,这一定包含第一个数吗?
不一定!
但是举个例子
7 5 4 6 3 8
虽然7>5但是5不<4所以last = num = 4
然后7 4 6 3 8满足(你要是非得不喜欢7,5 4 6 3 8也一样五位数啊,只是5和4都比7小,算7不是更简单一点吗)
另举例:
7 9 4 6 3 8
在last=7的时候没有满足情况的,last=num=9;后面9 4 6 3 8也是一样的
第一个数字包括在内or第一个数字不包括在内两种情况都有了呀~
好久没用flag,别忘了它的厉害
本文介绍了一种用于寻找给定数串中最长抗单调子串的算法实现。通过迭代输入序列并使用布尔标志来跟踪序列变化趋势,算法能够高效地找出满足条件的最长子串长度。
4119

被折叠的 条评论
为什么被折叠?



