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

*/

  

目录
相关文章
|
自然语言处理 算法 数据可视化
Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
|
Java
HDU - 2018 Multi-University Training Contest 1 - 1001: Maximum Multiple
HDU - 2018 Multi-University Training Contest 1 - 1001: Maximum Multiple
71 0
|
机器学习/深度学习 算法
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
175 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
|
Java
HDU - 2018 Multi-University Training Contest 2 - 1010: Swaps and Inversions
HDU - 2018 Multi-University Training Contest 2 - 1010: Swaps and Inversions
77 0
|
Java
HDU - 2018 Multi-University Training Contest 2 - 1004: Game
HDU - 2018 Multi-University Training Contest 2 - 1004: Game
77 0
|
Java
HDU - 2018 Multi-University Training Contest 3 - 1012: Visual Cube
HDU - 2018 Multi-University Training Contest 3 - 1012: Visual Cube
105 0
HDU - 2018 Multi-University Training Contest 3 - 1012: Visual Cube
lecture 2.2 problem set 1 and 2
1 COUNTING VOWELS   (10/10 分数) Assume s is a string of lower case characters.
1016 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.
1092 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()]的最大长度。
901 0

热门文章

最新文章