hackerrank>Dashboard>C++>STL>Deque-STL
2017-06-10 09:37
537 查看
Double ended queue or Deque(part of C++ STL) are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). The member functions of deque that are mainly used are:
Deque Template:
Declaration:
Size
Push
Pop
Empty
To know more about deque,
click here
Given a set of arrays of size
and an integer
, you have to find the maximum integer for each and every contiguous subarray of size
for each of the given arrays.
Input Format
First line of input will contain the number of test cases T. For each test case, you will be given the size of array
N and the size of subarray to be used K. This will be followed by the elements of the array
Ai.
Constraints
, where
is the
element in the array
.
Output Format
For each of the contiguous subarrays of size
of each array, you have to print the maximum integer.
Sample Input
Sample Output
Explanation
For the first case, the contiguous subarrays of size 2 are {3,4},{4,6},{6,3} and {3,4}. The 4 maximum elements of subarray of size 2 are: 4 6 6 4.
For the second case,the contiguous subarrays of size 4 are {3,4,5,8},{4,5,8,1},{5,8,1,4} and {8,1,4,10}. The 4 maximum element of subarray of size 4 are: 8 8 8 10.
输出每一个子集的最大值。刚开始练习STL。
#include <iostream>
#include <deque>
using namespace std;
int main()
{
int N;
cin >> N;
while(N--)
{
int n,k,com;
cin >> n >> k;
deque < int > mydeq;
deque < int > :: iterator it;
pair < int , deque <int> :: iterator > place;
place.first = 0;
com = n - k ;
while ( n-- )
{
int t;
cin >> t;
mydeq.push_back (t);
}
int t = com+1;
while ( t-- )
{
if( place.first !=0 )
place.second--;
if(place.first == 0)
{
for( it = mydeq.begin() ; it != mydeq.begin()+k ; it++ )
if( *it > place.first )
{
place.first = *it;
place.second = it;
}
}
else
{
it = mydeq.begin()+k-1;
if( *it >= place.first )
{
place.first = *it;
place.second = it;
}
}
if(t == com)
cout << place.first;
else
cout << ' ' << place.first;
if ( place.second -mydeq.begin() <= 0 )
place.first = 0;
mydeq.erase( mydeq.begin() );
}
cout<<endl;
}
return 0;
}
Deque Template:
std::deque<value_type>
Declaration:
deque<int> mydeque; //Creates a double ended queue of deque of int type
Size
int length = mydeque.size(); //Gives the size of the deque
Push
mydeque.push_back(1); //Pushes element at the end mydeque.push_front(2); //Pushes element at the beginning
Pop
mydeque.pop_back(); //Pops element from the end mydeque.pop_front(); //Pops element from the beginning
Empty
mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not
To know more about deque,
click here
Given a set of arrays of size
and an integer
, you have to find the maximum integer for each and every contiguous subarray of size
for each of the given arrays.
Input Format
First line of input will contain the number of test cases T. For each test case, you will be given the size of array
N and the size of subarray to be used K. This will be followed by the elements of the array
Ai.
Constraints
, where
is the
element in the array
.
Output Format
For each of the contiguous subarrays of size
of each array, you have to print the maximum integer.
Sample Input
2 5 2 3 4 6 3 4 7 4 3 4 5 8 1 4 10
Sample Output
4 6 6 4 8 8 8 10
Explanation
For the first case, the contiguous subarrays of size 2 are {3,4},{4,6},{6,3} and {3,4}. The 4 maximum elements of subarray of size 2 are: 4 6 6 4.
For the second case,the contiguous subarrays of size 4 are {3,4,5,8},{4,5,8,1},{5,8,1,4} and {8,1,4,10}. The 4 maximum element of subarray of size 4 are: 8 8 8 10.
输出每一个子集的最大值。刚开始练习STL。
#include <iostream>
#include <deque>
using namespace std;
int main()
{
int N;
cin >> N;
while(N--)
{
int n,k,com;
cin >> n >> k;
deque < int > mydeq;
deque < int > :: iterator it;
pair < int , deque <int> :: iterator > place;
place.first = 0;
com = n - k ;
while ( n-- )
{
int t;
cin >> t;
mydeq.push_back (t);
}
int t = com+1;
while ( t-- )
{
if( place.first !=0 )
place.second--;
if(place.first == 0)
{
for( it = mydeq.begin() ; it != mydeq.begin()+k ; it++ )
if( *it > place.first )
{
place.first = *it;
place.second = it;
}
}
else
{
it = mydeq.begin()+k-1;
if( *it >= place.first )
{
place.first = *it;
place.second = it;
}
}
if(t == com)
cout << place.first;
else
cout << ' ' << place.first;
if ( place.second -mydeq.begin() <= 0 )
place.first = 0;
mydeq.erase( mydeq.begin() );
}
cout<<endl;
}
return 0;
}
相关文章推荐
- hackerrank>Dashboard>C++>STL>Sets-STL
- hackerrank>Dashboard>C++>STL> Maps-STL
- hackerrank>Dashboard>C++>STL>Lower Bound-STL
- hackerrank>Dashboard>C++>STL>Print Pretty
- hackerrank>Dashboard>C++>STL>Vector-Erase
- hackerrank>Dashboard>C++>STL>Vector-Sort
- [翻译] C++ STL容器参考手册(第二章 <deque>)
- C++的STL模板set,multiset,map,multimap,deque,list,stack,queue和priority_queue
- C++ STL deque
- C++ STL 容器技术 之 deque双端队列容器
- C++ STL容器之 deque
- C++ STL学习之三:容器deque深入学习
- c++的STL模板库中3种容器类:vector,list,deque的比较
- C++ STL学习笔记三 deque双端队列容器
- C++ STL容器deque内部实现原理
- C++ STL容器deque内部实现原理
- C++ STL 容器技术 之 deque双端队列容器
- 【C++】 STL vector list deque
- C++ STL学习容器deque深入学习
- 【C++ STL】细数C++ STL 的那些事 -- deque(双端队列)