您的位置:首页 > 大数据 > 人工智能

2015 Multi-University Training Contest 1 - 1002 Assignment

2015-07-22 14:27 567 查看

Assignment


Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289

[b]Mean:[/b]

给你一个数列和一个k,求连续区间的极值之差小于k的数的个数。

[b]analyse:[/b]

用两个优先队列来维护区间的最大值和最小值,每次插入新值的时候检查区间内的极值差是否满足条件,不满足就将最左边的数删除,直到满足条件为止。ans每次加上区间的长度即得最终答案。

[b]Time complexity: O(N)[/b]

[b]Source code: [/b]

/*
* 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;
}
/*

*/


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: