hdoj5805 NanoApe Loves Sequence && hdoj 5806 NanoApe Loves Sequence Ⅱ

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


Problem Description
NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination!

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.
 

Input
The first line of the input contains an integer T, denoting the number of test cases.

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.

1T10, 3n100000, 1Ai109
 

Output
For each test case, print a line with one integer, denoting the answer.

In order to prevent using float number, you should print the answer multiplied by n.
 

Sample Input
1 4 1 2 3 4
 

Sample Output
6


求出前ii個數裏相鄰差值的最大值f_ifiiinn裏相鄰差值的最大值g_igi,那麼ans=\sum_{i=1}^n \max(|A_{i-1}-A_{i+1}|,f_{i-1},g_{i+1})ans=i=1nmax(Ai1Ai+1,fi1,gi+1)

時間複雜度O(n)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


Problem Description
NanoApe, the Retired Dog, has returned back to prepare for for the National Higher Education Entrance Examination!

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.
 

Input
The first line of the input contains an integer T, denoting the number of test cases.

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.

1T10, 2n200000, 1kn/2, 1m,Ai109
 

Output
For each test case, print a line with one integer, denoting the answer.
 

Sample Input
1 7 4 2 4 2 7 7 6 5 1
 

Sample Output
18


將不小於mm的數看作11,剩下的數看作00,那麼只要區間內11的個數不小於kk則可行,枚舉左端點,右端點可以通過two-pointer求出。

時間複雜度O(n)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;
}





發佈了40 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章