链接:https://ac.nowcoder.com/acm/contest/11221/A
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
潜蛟舞,蜉蝣动,深渊水妖涟漪现。
你进行了 nn 次考试,第 ii 次考试的分数是 a_ia
i
。
你想知道你最大进步的幅度是多少,定义最大进步的幅度为:
-
选定一段 极长 的区间 [l,r][l,r],满足 a_l\le a_{l+1}\le\cdots\le a_ra
l
≤a
l+1
≤⋯≤a
r
。 -
满足条件一的情况下,使得 a_r-a_la
r
−a
l
的值最大。
如果你有多段最大进步,你需要输出所有的最大进步段,每一段用两个数 l,rl,r 表示,按照区间的左端点升序输出。
一句话题意:找到所有极长的不严格上升段,并找出它们当中右端点权值 - 左端点权值最大的那些个段,输出端点坐标。
输入描述:
全文第一行输入一个正整数 T(1\le T\le10^5)T(1≤T≤10
5
),表示数据组数。
每组数据第一行输入一个正整数 n(2\le n\le10^5)n(2≤n≤10
5
)。
第二行输入 nn 个正整数,第 ii 个正整数是 a_i(1\le a_i\le n)a
i
(1≤a
i
≤n)。
数据保证 \sum n\le3\times10^6∑n≤3×10
6
,保证至少存在一个 i\in[2,n]i∈[2,n] 满足 a_i\ge a_{i-1}a
i
≥a
i−1
。
输出描述:
对每组询问输出一行,表示你所得到的所有答案。
示例1
输入
复制
1
7
1 3 5 2 4 6 3
输出
复制
1 3 4 6
备注:
区间 [1,3][1,3] 是 1,3,51,3,5,满足 1\le3\le51≤3≤5 并且 5-1=45−1=4 差值最大。
区间 [4,6][4,6] 同理,6-2=46−2=4 同样为最大的差值。
思路 :
- 先找出所有的 极长 不严格上升 区间
- 然后在这些区间中选出 右端点权值-左端点权值最大 的那些区间
#include <iostream>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int a[N];
void solve()
{
int n; cin >> n;
for (int i = 1; i <= n && cin >> a[i]; i ++ );
vector<PII> ve;
int l = 1;
for (int i = 2; i <= n; i ++ )
{
if (a[i] < a[i - 1])
{
ve.push_back({l, i - 1});
l = i;
}
}
ve.push_back({l, n});
vector<PII> ans;
int mx = 0;
for (auto it : ve)
{
if (a[it.y] - a[it.x] > mx)
{
ans.clear();
mx = a[it.y] - a[it.x];
ans.push_back(it);
}
else if (a[it.y] - a[it.x] == mx)
ans.push_back(it);
}
for (auto it : ans) cout << it.x << ' ' << it.y << ' ';
cout << endl;
}
int main()
{
int _; cin >> _;
while (_ -- )
solve();
}