hdu5289(2015多校1)--Assignment(單調隊列),單調隊列

來源:互聯網
上載者:User

hdu5289(2015多校1)--Assignment(單調隊列),單調隊列

AssignmentTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 503    Accepted Submission(s): 256


Problem DescriptionTom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this. 
InputIn the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0<k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less than k. The second line contains n integers:a[1],a[2],…,a[n](0<=a[i]<=10^9),indicate the i-th staff’s ability. 
OutputFor each test,output the number of groups. 
Sample Input
24 23 1 2 410 50 3 4 5 2 1 6 7 8 9
 
Sample Output
528HintFirst Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3] 


題目大意:給出一個數列,問其中存在多少連續子序列,子序列的最大值-最小值<k

O(n)的時間複雜度,,,,單調隊列太牛叉                              

用單調隊列維護最大值最小值,雙指標,第一個第二個指標初始指向第一個資料,第一個指標按順序不斷向隊尾添加資料,當最大值最小值的差大於等於k後,就意味著新添加的這個不能作用於當前第二個指標的位置,也就能計算出,以第二個指標位置開始的連續子序列的個數,最後統計總和。

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std ;#define LL __int64deque <LL> deq1 , deq2 ;//單調隊列,deq1最大值,deq2最小值LL a[100010] ;int main() {    int t , n , i , j ;    LL k , ans ;    scanf("%d", &t) ;    while( t-- ) {        scanf("%d %I64d", &n, &k) ;        for(i = 0 ; i < n ; i++)            scanf("%I64d", &a[i]) ;        if(k == 0) {            printf("0\n") ;            continue ;        }        while( !deq1.empty() ) deq1.pop_back() ;        while( !deq2.empty() ) deq2.pop_back() ;        for(i = 0 , j = 0 , ans = 0; i < n ; i++) {//i在前,j在後            while( !deq1.empty() && deq1.back() < a[i] ) deq1.pop_back() ;            deq1.push_back(a[i]) ;            while( !deq2.empty() && deq2.back() > a[i] ) deq2.pop_back() ;            deq2.push_back(a[i]) ;            while( !deq1.empty() && !deq2.empty() && deq1.front() - deq2.front() >= k ) {                ans += (i-j) ;                //printf("%d %d,%I64d %I64d\n", i , j, deq1.front() , deq2.front() ) ;                if( deq1.front() == a[j] ) deq1.pop_front() ;                if( deq2.front() == a[j] ) deq2.pop_front() ;                j++ ;            }        }        while( j < n ) {            ans += (i-j) ;            j++ ;        }        printf("%I64d\n", ans) ;    }    return 0 ;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.