NanoApe Loves Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 219 Accepted Submission(s): 92
In math class, NanoApe picked up sequences once again. He wrote down a sequence with n numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each two adjacent remained numbers, denoted as F.
Now he wants to know the expected value of F, if he deleted each number with equal probability.
In each test case, the first line of the input contains an integer n, denoting the length of the original sequence.
The second line of the input contains n integers A1,A2,...,An, denoting the elements of the sequence.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109
In order to prevent using float number, you should print the answer multiplied by n.
求出前i個數裏相鄰差值的最大值fi,i到n裏相鄰差值的最大值gi,那麼ans=∑i=1nmax(∣Ai−1−Ai+1∣,fi−1,gi+1)。
時間複雜度O(n)。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 200010;
typedef long long ll;
int T,n;
int A[maxn],pre[maxn],suf[maxn];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i = 1; i <= n; ++i) scanf("%d",&A[i]);
memset(pre,0,sizeof(pre));
memset(suf,0,sizeof(suf));
for(int i = 2; i <= n; ++i){
pre[i] = abs(A[i] - A[i - 1]);
pre[i] = max(pre[i],pre[i - 1]);
}
for(int i = n - 1; i >= 1; --i){
suf[i] = abs(A[i] - A[i + 1]);
suf[i] = max(suf[i],suf[i + 1]);
}
ll ans = 0;
for(int i = 1; i <= n; ++i){
if(i == 1) ans += suf[2];
else if(i == n) ans += pre[n - 1];
else{
ans += max(pre[i - 1],max(suf[i + 1],abs(A[i + 1] - A[i - 1])));
//printf("%d %d\n",i,max(pre[i - 1],max(suf[i + 1],abs(A[i + 1] - A[i - 1]))));
}
}
printf("%I64d\n",ans);
}
return 0;
}
NanoApe Loves Sequence Ⅱ
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 230 Accepted Submission(s): 112
In math class, NanoApe picked up sequences once again. He wrote down a sequence with n numbers and a number m on the paper.
Now he wants to know the number of continous subsequences of the sequence in such a manner that the k-th largest number in the subsequence is no less than m.
Note : The length of the subsequence must be no less than k.
In each test case, the first line of the input contains three integers n,m,k.
The second line of the input contains n integers A1,A2,...,An, denoting the elements of the sequence.
1≤T≤10, 2≤n≤200000, 1≤k≤n/2, 1≤m,Ai≤109
將不小於m的數看作1,剩下的數看作0,那麼只要區間內1的個數不小於k則可行,枚舉左端點,右端點可以通過two-pointer求出。
時間複雜度O(n)。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200005;
int T, n, m, k;
int a[N];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] >= m) a[i] = 1;
else a[i] = 0;
a[i] += a[i - 1];
}
int r = 1;
long long ans = 0;
for (int l = 1; l <= n; l++) {
while (r <= n && a[r] - a[l - 1] < k) r++;
ans += n - r + 1;
}
printf("%lld\n", ans);
}
return 0;
}