Deque

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 731    Accepted Submission(s): 236

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
 
Source
 
Recommend
liuyiding
 

我直接附上标准题解吧。。当时是看出这是最长下降跟最长上升序列的求和的,只是因为没相处nlogn算法,最终没有做出来。 今天终于AC掉了。 刚开始考虑的时候,还是没有考虑到

5

5 5 5 5 5 这种需要过滤掉重复的方法,导致WA了很多次,后来多开了一个数组保留才AC掉。

标准题解是:

Problem E. Deque考虑题目的一个简化版本:使双端队列单调上升。对于序列 A 和队列 Q,找到队列中最早出现的数字Ax,则Ax将 Q 分成的两个部分分别是原序列中以Ax开始的最长上升和最长下降序列,答案即为这两者之和的最大值。而对于本题,由于存在相同元素,所以只要找到以Ax为起点的最长不下降序列和最长不上升序列的和,然后减去两个里面出现Ax 次数的最小值即可。

我的代码是:

/*
* @author ipqhjjybj
* @date 20130723
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 100005
#define clr(x,k) memset((x),(k),sizeof(x))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int n;
int a[MAXN];
int d_p[MAXN]; //存储递减序列的数字
int d_f[MAXN];
int n_d_f[MAXN];//相同数字出现次数
int i_p[MAXN]; //存储递增序列的数字
int i_f[MAXN];
int n_i_f[MAXN];//相同数字出现次数
int find_dec(int l,int r,int x){
int i=l,j=r,mid;
while(i<=j){
mid=(i+j)>>1;
if(d_p[mid]<=x) i=mid+1;
else j=mid-1;
}return i;
}
int find_inc(int l,int r,int x){
int i = l,j=r,mid;
while(i<=j){
mid=(i+j)>>1;
if(i_p[mid]>=x) i=mid+1;
else j=mid-1;
}return i;
}
int main(){
//freopen("1005.in","r",stdin);
int z;
scanf("%d",&z);
while(z--){
clr(n_i_f,0),clr(n_d_f,0);
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
int ans=1;
int d_ans=1;
d_p[1]=a[n];
d_f[n]=1;
n_d_f[1]=1;
int i_ans=1;
i_p[1]=a[n];
i_f[n]=1;
n_i_f[1]=1;
for(int i=n-1;i>0;i--){
int t1=find_dec(1,d_ans,a[i]);
if(t1>d_ans)d_ans++;
d_p[t1]=a[i];
n_d_f[t1]=(t1>1&&d_p[t1-1]==a[i])?n_d_f[t1-1]+1:1;
d_f[i]=t1;
int t2=find_inc(1,i_ans,a[i]);
if(t2>i_ans)i_ans++;
i_p[t2]=a[i];
n_i_f[t2]=(t2>1&&i_p[t2-1]==a[i])?n_i_f[t2-1]+1:1;
i_f[i]=t2; //printf("m_t1=%d m_t2=%d\n",m_t1,m_t2);
int temp = t1+t2-min(n_d_f[t1],n_i_f[t2]);
ans = max(ans,temp);
}
printf("%d\n",ans);
}
return 0;
}

325MS过掉。。

附上标准程序

#include "iostream"
#include "cstring"
#include "cstdio"
#include "vector"
#include "algorithm"
#include "map"
using namespace std;
const int N = 100010;
int a[N];
int num_up[N],num_down[N];
int dp_up[N],dp_down[N];
int n;
void getdp(int dp[],int num[])
{
dp[n]=1;
vector<int> v;
v.push_back(a[n]);
vector<int>::iterator iter;
for(int i=n-1;i>=1;i--){
int sz=v.size();
if(a[i]>v[sz-1]){
v.push_back(a[i]);
dp[i]=sz+1;
num[i]=1;
}else if(a[i]==v[sz-1]){
iter=upper_bound(v.begin(),v.end(),a[i]);
dp[i]=iter-v.begin()+1;
v.push_back(a[i]);
pair<vector<int>::iterator,vector<int>::iterator> bounds;
bounds=equal_range(v.begin(),v.end(),a[i]);
num[i]=bounds.second-bounds.first;
}else{
iter=upper_bound(v.begin(),v.end(),a[i]);
dp[i]=iter-v.begin()+1;
*iter=a[i];
pair<vector<int>::iterator,vector<int>::iterator> bounds;
bounds=equal_range(v.begin(),v.end(),a[i]);
num[i]=bounds.second-bounds.first;
}
}
}
void debug(int a[])
{
for(int i=1;i<=n;i++){
printf("%d ",a[i]);
}
printf("\n");
}
int main(void)
{
int T;
scanf("%d",&T);
while(T--){
int ans=0;
scanf("%d",&n);
map<int,int> mp;
mp.clear();
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
getdp(dp_up,num_up);
for(int i=1;i<=n;i++){
a[i]=-a[i];
}
getdp(dp_down,num_down);
for(int i=1;i<=n;i++){
mp[a[i]]++;
ans=max(ans,dp_down[i]+dp_up[i]-min(num_up[i],num_down[i]));
}
printf("%d\n",ans);
}
return 0;
}

标准程序用了986MS。。。可能我的程序优化的还好点吧。。。

总而言之是水平不够。。当时没想出nlogn算法。。

多校联合练习赛1 Problem1005 Deque LIS+LDS 再加一系列优化的更多相关文章

  1. HDU 多校联合练习赛2 Warm up 2 二分图匹配

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  2. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  3. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  4. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  5. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  6. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  7. HDU 5792---2016暑假多校联合---World is Exploding

    2016暑假多校联合---World is Exploding Problem Description Given a sequence A with length n,count how many ...

  8. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  9. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

随机推荐

  1. 【Sqlserver清空数据库中所有表数据】

    脚本: CREATE PROCEDURE sp_DeleteAllData AS EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT AL ...

  2. Response JSON数据返回

    简述: 在servlet填充Response的时候,做JSON格式的数据转换 使用的类是net.sf.json.JSONObject,传入response对象和返回的显示类,修改response,返回 ...

  3. Java 基础类型

    在Java中,基本的数据类型主要有8种: 1)int  4 字节(byte)  -2的31次方到2的31次方-1 2)short 2 字节(byte) -2的15次方到2的15次方-1 3)long ...

  4. poj 2704 Pascal's Travels_记忆化搜索

    一道简单但是题意蛋疼的题目 题意:给你个n*n的图,开始在左上角,要求走到右下角有多种走法,图上的数表示走几步,只能向右或向下走. #include<iostream> #include& ...

  5. sigaction函数解析

    http://blog.chinaunix.net/uid-1877180-id-3011232.html sigaction函数解析  sigaction函数的功能是检查或修改与指定信号相关联的处理 ...

  6. nyoj 1237 最大岛屿(dfs)

    描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王. 这是一个由海洋.岛屿和海盗组 ...

  7. UITableView 或 UIScrollView 点击状态栏列表回到顶部

    整理来自互联网- 这是tableView继承的scrollView的一个属性 scrollsToTop. 官方说明是这样的: // When the user taps the status bar, ...

  8. hadoop结构出现后format变态

    14/07/10 18:50:47 FATAL conf.Configuration: error parsing conf file: com.sun.org                     ...

  9. Android长方形图片生成正圆形,以及矩形图片生成圆角

    一般要做正圆形图片,只能是正方形的基础上才能实现,否则就变成椭圆了,下面说说如何使长方形的图片生成正圆形图片 废话不多说,没图没真相,先上图吧: 原图:  变成正圆后:  下面上代码: public ...

  10. android初级应用到高端架构教程------ 完整体系化学习android开发

    系统的学习android开发技术,从应用到底层,再到架构,告别乱糟糟的学习方式,不再是抓不住重点.从上到下贯通,全面学习android开发.让你拥有清晰的思路,一步步学习android开发! 一般而言 ...