Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
2015-12-06 15:25
471 查看
B. Approximating a Constant Range
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data
points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?
You're given a sequence of n data points a1, ..., an.
There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.
A range [l, r] of data points is said to be almost constant if
the difference between the largest and the smallest value in that range is at most 1. Formally, let M be
the maximum and m the minimum value of ai for l ≤ i ≤ r;
the range [l, r] is almost constant if M - m ≤ 1.
Find the length of the longest almost constant range.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) —
the number of data points.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
Output
Print a single number — the maximum length of an almost constant range of the given sequence.
Sample test(s)
input
output
input
output
Note
In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.
In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10];
the only almost constant range of the maximum length 5 is [6, 10].
求最长的连续恒定序列长度,要求序列的最大值与最小值的差不大于1
因为要求差值不大于1而且是连续的,所以就方便多了,对于每个元素,记录在以它结尾的恒定序列的长度、最大值和最小值,另外还有在它之前和它相等的元素的个数(必须是和它连续的)。这样,遍历数组,遇到接不上恒定序列的元素,看是否能以它前一个为起点从新开始一个新的恒定序列,如果可以,把它前一个元素所记录的same(前面提到的在此元素前和此元素相等的元素的个数,它们必须是连续的)加上。这样不断更新最长长度,最后得出结果。
#include <stdio.h>
#include <algorithm>
#define maxn 100005struct node
{
int x, min, max;
int len, same;
}a[maxn];
int Max(int a, int b) {return a>b?a:b;}
int main()
{
int n, i, res = 1;
scanf("%d", &n);
for(i = 0;i < n;i++)
{
scanf("%d", &a[i].x);
a[i].len = a[i].same = 1;
a[i].min = a[i].max = a[i].x;
}
for(i = 1;i < n;i++)
{
if(a[i].x == a[i - 1].x) a[i].same = a[i - 1].same + 1;
if(abs(a[i].x - a[i - 1].min) <= 1&&abs(a[i].x - a[i - 1].max) <= 1)
{
a[i].len = a[i - 1].len + 1;
a[i].min = a[i].x<a[i - 1].min?a[i].x:a[i - 1].min;
a[i].max = a[i].x>a[i - 1].max?a[i].x:a[i - 1].max;
if(a[i].len > res) res = a[i].len;
}
else if(abs(a[i].x - a[i - 1].x) <= 1)
{
a[i].len = a[i - 1].same + 1;
a[i].min = a[i].x<a[i - 1].x?a[i].x:a[i - 1].x;
a[i].max = a[i].x>a[i - 1].x?a[i].x:a[i - 1].x;
if(a[i].len > res) res = a[i].len;
}
}
printf("%d\n", res);
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data
points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?
You're given a sequence of n data points a1, ..., an.
There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.
A range [l, r] of data points is said to be almost constant if
the difference between the largest and the smallest value in that range is at most 1. Formally, let M be
the maximum and m the minimum value of ai for l ≤ i ≤ r;
the range [l, r] is almost constant if M - m ≤ 1.
Find the length of the longest almost constant range.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) —
the number of data points.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).
Output
Print a single number — the maximum length of an almost constant range of the given sequence.
Sample test(s)
input
5 1 2 3 3 2
output
4
input
11 5 4 5 5 6 7 8 8 8 7 6
output
5
Note
In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.
In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10];
the only almost constant range of the maximum length 5 is [6, 10].
求最长的连续恒定序列长度,要求序列的最大值与最小值的差不大于1
因为要求差值不大于1而且是连续的,所以就方便多了,对于每个元素,记录在以它结尾的恒定序列的长度、最大值和最小值,另外还有在它之前和它相等的元素的个数(必须是和它连续的)。这样,遍历数组,遇到接不上恒定序列的元素,看是否能以它前一个为起点从新开始一个新的恒定序列,如果可以,把它前一个元素所记录的same(前面提到的在此元素前和此元素相等的元素的个数,它们必须是连续的)加上。这样不断更新最长长度,最后得出结果。
#include <stdio.h>
#include <algorithm>
#define maxn 100005struct node
{
int x, min, max;
int len, same;
}a[maxn];
int Max(int a, int b) {return a>b?a:b;}
int main()
{
int n, i, res = 1;
scanf("%d", &n);
for(i = 0;i < n;i++)
{
scanf("%d", &a[i].x);
a[i].len = a[i].same = 1;
a[i].min = a[i].max = a[i].x;
}
for(i = 1;i < n;i++)
{
if(a[i].x == a[i - 1].x) a[i].same = a[i - 1].same + 1;
if(abs(a[i].x - a[i - 1].min) <= 1&&abs(a[i].x - a[i - 1].max) <= 1)
{
a[i].len = a[i - 1].len + 1;
a[i].min = a[i].x<a[i - 1].min?a[i].x:a[i - 1].min;
a[i].max = a[i].x>a[i - 1].max?a[i].x:a[i - 1].max;
if(a[i].len > res) res = a[i].len;
}
else if(abs(a[i].x - a[i - 1].x) <= 1)
{
a[i].len = a[i - 1].same + 1;
a[i].min = a[i].x<a[i - 1].x?a[i].x:a[i - 1].x;
a[i].max = a[i].x>a[i - 1].x?a[i].x:a[i - 1].x;
if(a[i].len > res) res = a[i].len;
}
}
printf("%d\n", res);
}
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima
- Codeforces Gym100342J Triatrip
- Codeforces Gym100286B Blind Walk (dfs)