2015 Multi-University Training Contest 1 - 1002 Assignment
Assignment
Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289
Mean:
给你一个数列和一个k,求连续区间的极值之差小于k的数的个数。
analyse:
用两个优先队列来维护区间的最大值和最小值,每次插入新值的时候检查区间内的极值差是否满足条件,不满足就将最左边的数删除,直到满足条件为止。ans每次加上区间的长度即得最终答案。
Time complexity: O(N)
Source code:
/* * this code is made by crazyacking * Verdict: Accepted * Submission Date: 2015-07-21-21.38 * Time: 0MS * Memory: 137KB */ #include <queue> #include <cstdio> #include <set> #include <string> #include <stack> #include <cmath> #include <climits> #include <map> #include <cstdlib> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #define LL long long #define ULL unsigned long long using namespace std; int main() { ios_base::sync_with_stdio( false ); cin.tie( 0 ); int Cas; cin >> Cas; while( Cas-- ) { int n, k, tmp; cin >> n >> k; vector<int> v; for( int i = 0; i < n; ++i ) { cin >> tmp; v.push_back( tmp ); } multiset<pair<int, int> > q1, q2; int l = 0, r = 0; LL ans = 0; while( r < n ) { q1.insert( make_pair( v[r], r ) ), q2.insert( make_pair( -v[r], r ) ); while( -( *q1.begin() ).first - ( *q2.begin() ).first >= k ) { q1.erase( make_pair( v[l] , l ) ), q2.erase( make_pair( -v[l], l ) ); l++; } ans += r - l + 1; r++; } cout << ans << endl; } return 0; } /* */