2015 Multi-University Training Contest 1 - 1002 Assignment

简介:  Assignment Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5289   Mean:  给你一个数列和一个k,求连续区间的极值之差小于k的数的个数。

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

*/

  

目录
相关文章
|
Java
HDU - 2018 Multi-University Training Contest 3 - 1012: Visual Cube
HDU - 2018 Multi-University Training Contest 3 - 1012: Visual Cube
130 0
HDU - 2018 Multi-University Training Contest 3 - 1012: Visual Cube
|
Java
HDU - 2018 Multi-University Training Contest 1 - 1001: Maximum Multiple
HDU - 2018 Multi-University Training Contest 1 - 1001: Maximum Multiple
99 0
|
Java
HDU - 2018 Multi-University Training Contest 2 - 1010: Swaps and Inversions
HDU - 2018 Multi-University Training Contest 2 - 1010: Swaps and Inversions
113 0
|
Java
HDU - 2018 Multi-University Training Contest 2 - 1004: Game
HDU - 2018 Multi-University Training Contest 2 - 1004: Game
104 0
lecture 3.2 problem set 3
"Radioactive decay" is the process by which an unstable atom loses energy and emits ionizing particles - what is commonly refered to as radiation.
1140 0
lecture 2.2 problem set 1 and 2
1 COUNTING VOWELS   (10/10 分数) Assume s is a string of lower case characters.
1042 0
|
Java
2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】
CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 127    Accepted Submission(s): 20 Pro...
1416 0
2015 Multi-University Training Contest 5 1009 MZL&#39;s Border
MZL's Border  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5351   Mean:  给出一个类似斐波那契数列的字符串序列,要你求给出的f[n]字符串中截取前m位的字符串s中s[1...i] = s[s.size()-i+1....s.size()]的最大长度。
930 0
|
机器学习/深度学习
2015 Multi-University Training Contest 3 1002 RGCDQ
RGCDQ  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5317   Mean:  定义函数f(x)表示:x的不同素因子个数。
891 0