Deque (问题)

Deque

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 0
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.
 

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.
 

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
 


错误代码:杭电AC不了

#include <stdio.h>
#include <iostream>
using namespace std;


int main()
{
    int i,j,t,n,sum;
    int input[100001],a[2];
    cin>>t;
    while(t--)
    {
        cin>>n;
        sum=2;
        for(i=0;i<n;i++)
        {
            cin>>input[i];
        }
if(input[0]<input[1])
{
a[0]=input[0];
a[1]=input[1];
}
else
{
a[0]=input[1];
a[1]=input[0];
}
        for(i=2;i<n;i++)
        {
            if(a[0]>=input[i])
            { 
                sum++;
                a[0]=input[i];
            }
            if(a[1]<=input[i])
            {
                sum++;
                a[1]=input[i];         
            }
        }
        cout<<sum<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的Deque是一种双端队列,它可以在两端进行插入和删除操作。Deque是"Double Ended Queue"的缩写。在Java中,Deque接口是Queue接口的子接口,它扩展了Queue接口,提供了更多的方法来支持双端操作。 可以使用Java的LinkedList类来实现Deque接口。下面是一个使用Deque的简单示例: ```java import java.util.Deque; import java.util.LinkedList; public class DequeExample { public static void main(String[] args) { Deque<String> deque = new LinkedList<>(); // 在队尾添加元素 deque.addLast("element1"); deque.offerLast("element2"); deque.offer("element3"); // offer方法等同于offerLast // 在队头添加元素 deque.addFirst("element4"); deque.offerFirst("element5"); System.out.println("Deque: " + deque); // 获取并移除队尾元素 String lastElement = deque.removeLast(); System.out.println("Removed last element: " + lastElement); // 获取并移除队头元素 String firstElement = deque.removeFirst(); System.out.println("Removed first element: " + firstElement); System.out.println("Updated Deque: " + deque); } } ``` 输出结果: ``` Deque: [element5, element4, element1, element2, element3] Removed last element: element3 Removed first element: element5 Updated Deque: [element4, element1, element2] ``` 在上面的示例中,我们使用了Deque的常用方法,如addFirst、addLast、offerFirst、offerLast、removeFirst和removeLast等。 希望这个示例对你有帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值