Deque
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1338 Accepted Submission(s): 476
Problem Description
Today, the teacher gave Alice extra homework for the girl weren't attentive in his class. It's hard, and Alice is going to turn to you for help.
The teacher gave Alice a sequence of number(named A) and a deque. The sequence exactly contains N integers. A deque is such a queue, that one is able to push or pop the element at its front end or rear end. Alice was asked to take out the elements from the sequence in order(from A_1 to A_N), and decide to push it to the front or rear of the deque, or drop it directly. At any moment, Alice is allowed to pop the elements on the both ends of the deque. The only limit is, that the elements in the deque should be non-decreasing.
Alice's task is to find a way to push as many elements as possible into the deque. You, the greatest programmer, are required to reclaim the little girl from despair.
The teacher gave Alice a sequence of number(named A) and a deque. The sequence exactly contains N integers. A deque is such a queue, that one is able to push or pop the element at its front end or rear end. Alice was asked to take out the elements from the sequence in order(from A_1 to A_N), and decide to push it to the front or rear of the deque, or drop it directly. At any moment, Alice is allowed to pop the elements on the both ends of the deque. The only limit is, that the elements in the deque should be non-decreasing.
Alice's task is to find a way to push as many elements as possible into the deque. You, the greatest programmer, are required to reclaim the little girl from despair.
Input
The first line is an integer T(1≤T≤10) indicating the number of test cases.
For each case, the first line is the length of sequence N(1≤N≤100000).
The following line contains N integers A 1,A 2,…,A N.
For each case, the first line is the length of sequence N(1≤N≤100000).
The following line contains N integers A 1,A 2,…,A N.
Output
For each test case, output one integer indicating the maximum length of the deque.
Sample Input
3 7 1 2 3 4 5 6 7 5 4 3 2 1 5 5 5 4 1 2 3
Sample Output
7 5 3
Source
Recommend
liuyiding
题意:有一个栈, 左边是栈顶。 每个数要么放, 要么不放。
问能放进一个双端队列的最长上升(非下降)子序列。
思路: LIS(nlogn)
考虑题目的一个简化版本:使双端队列单调上升。
对于序列A 和队列Q,找到队列中最早出 现的数字Ax,则A 将 Q 分成的两个部分分别是原序列中以A 开始的最长上升和最长下降序列,答案即为这两者之和的最大值。
而对于本题,由于存在相同元素,所以只要找到以Ax 为起点的最长不下降序列和最长不上升序列的和,然后减去两个里面出现Ax 次数的最小值即可。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int v = 100000 + 50;
int t, n, num[v], down[v], up[v], same_down[v], same_up[v];
void f(int dp[], int same[]) {
vector<int> v;
for(int i = 1; i <= n; i++) {
int a = lower_bound(v.begin(), v.end(), num[i]) - v.begin();
int b = upper_bound(v.begin(), v.end(), num[i]) - v.begin();
if(b == v.size()) { //num[i] 大于 栈的所有值
v.push_back(num[i]);
dp[i] = v.size();
}
else {
v[b] = num[i];
dp[i] = b + 1;
}
same[i] = b - a + 1;
}
}
int main() {
int i, j;
cin >> t;
while(t--) {
int ans = 0;
scanf("%d", &n);
for(i = n; i >= 1; --i)
scanf("%d", &num[i]);
f(down, same_down);
for(i = 1; i <= n; ++i)
num[i] = -num[i];
f(up, same_up);
for(i = 1; i <= n; ++i)
ans = max(ans, up[i] + down[i] - min(same_down[i], same_up[i]));
cout << ans << endl;
}
}