1、记不清楚了,大体意思就是给一个数组大小n,然后给n个数,组成一个离散序列,就是求极大值所在的族大区间,将这个最大区间的左侧和右侧坐标x值输出。
例如:
输入:
n
n长度的整数数组
输出:
区间的范围
样例:
10
1 3 1 2 5 4 3 1 9 10
样例输出:
2 7
代码:
#include<iostream>
#include<vector>
using namespace std;
struct MyStruct
{
int leftx;
int righty;
int length;
};
bool operator>(const MyStruct &a, const MyStruct &b)
{
return (a.length > b.length) ? true : false;
}
int main()
{
MyStruct data;
MyStruct datatemp;
int n = 0;
cin >> n;
int temp0,temp1;
bool flag0, flag1;
flag0 = flag1 = false;
cin >> temp0;
for (size_t i = 1; i < n; i++)
{
cin >> temp1;
if (temp1>temp0)
{
if (flag0 == false)
{
flag0 = true;
datatemp.leftx = i-1;
datatemp.righty = 0;
datatemp.length = 0;
}
else
{
if (flag1==true)
{
datatemp.righty = i-1;
datatemp.length = datatemp.righty - datatemp.leftx;
if (datatemp>data)
{
data = datatemp;
}
datatemp.leftx = i-1;
datatemp.righty = 0;
datatemp.length = 0;
flag0=true;
flag1 = false;
}
}
}
else
{
if (flag0==true)
{
flag1 = true;
}
}
temp0 = temp1;
}
cout << data.leftx << ' ' << data.righty;
return 0;
}