题目 | Max Min
AtCoder Beginner Contest 247
E - Max Min
https://atcoder.jp/contests/abc247/tasks/abc247_e
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
We have a number sequence
- The maximum value of
is , and the minimum is .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4 3 1
1 2 3 1
Sample Output 1
4
Sample Input 2
5 2 1
1 3 2 4 1
Sample Output 2
0
No pair
Sample Input 3
5 1 1
1 1 1 1 1
Sample Output 3
15
It may hold that
Sample Input 4
10 8 1
2 7 1 8 2 8 1 8 2 8
Sample Output 4
36
我的笔记
数列分段
要满足
例如
时间复杂度:
计算每段数列的情况
我们将数列分段后,易得
使用双指针,初始化
如果该区间不符合条件,即
如果该区间符合条件,即
时间复杂度:
这样就可以得到该段数列的情况,题目答案
总时间复杂度:
源码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int N, X, Y;
int A[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> X >> Y;
for (int i = 0; i < N; i++)
cin >> A[i];
int i = 0;
long long ans = 0;
while (i < N)
{
vector<int> subseq;
while (i < N && A[i] >= Y && A[i] <= X)
{
subseq.push_back(A[i]);
i++;
}
if (!subseq.empty())
{
int l = 0, r = 0;
int cnt_X = 0, cnt_Y = 0;
if (subseq[0] == X)
cnt_X++;
if (subseq[0] == Y)
cnt_Y++;
while (l < subseq.size() && r < subseq.size())
{
if (!cnt_X || !cnt_Y)
{
r++;
if (subseq[r] == X)
cnt_X++;
if (subseq[r] == Y)
cnt_Y++;
}
else
{
ans += subseq.size() - r;
if (subseq[l] == X)
cnt_X--;
if (subseq[l] == Y)
cnt_Y--;
l++;
}
}
}
else
{
i++;
}
}
cout << ans << endl;
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi