博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
防线问题
阅读量:6195 次
发布时间:2019-06-21

本文共 3102 字,大约阅读时间需要 10 分钟。

UVA1471

 

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 

Description

 

After the last war devastated your country, you - as the king of the land of Ardenia - decided it was high time to improve the defense of your capital city. A part of your fortification is a line of mage towers, starting near the city and continuing to the northern woods. Your advisors determined that the quality of the defense depended only on one factor: the length of a longest contiguous tower sequence of increasing heights. (They gave you a lengthy explanation, but the only thing you understood was that it had something to do with firing energy bolts at enemy forces).

After some hard negotiations, it appeared that building new towers is out of question. Mages of Ardenia have agreed to demolish some of their towers, though. You may demolish arbitrary number of towers, but the mages enforced one condition: these towers have to be consecutive.

For example, if the heights of towers were, respectively, 5, 3, 4, 9, 2, 8, 6, 7, 1, then by demolishing towers of heights 9, 2, and 8, the longest increasing sequence of consecutive towers is 3, 4, 6, 7.

 

 

The input contains several test cases. The first line of the input contains a positive integer 
Z$ \le$25, denoting the number of test cases. Then Z test cases follow, each conforming to the format described below.

The input instance consists of two lines. The first one contains one positive integer n$ \le$2 . 105 denoting the number of towers. The second line containsn positive integers not larger than 109 separated by single spaces being the heights of the towers.

 

 

For each test case, your program has to write an output conforming to the format described below.

You should output one line containing the length of a longest increasing sequence of consecutive towers, achievable by demolishing some consecutive towers or no tower at all.

 

 

 

2 9 5 3 4 9 2 8 6 7 1 7 1 2 3 10 4 5 6

 

 

 

4 6

#include"iostream"

#include"algorithm"
#include"cstring"
#include"cstdio"
using namespace std;

const int maxn=200000+10;

const int Max=1000000000;

int a[maxn],righ[maxn],lef[maxn],d[maxn],g[maxn];

int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;

for(int i=0;i<n;i++) cin>>a[i];

memset(righ,0,sizeof(righ));

memset(lef,0,sizeof(lef));

lef[0]=1;righ[n-1]=1;

for(int i=n-2;i>=0;i--)
{
if(a[i]>=a[i+1]) righ[i]=1;
else righ[i]=righ[i+1]+1;
}
for(int j=1;j<n;j++)
{
if(a[j]>a[j-1]) lef[j]=lef[j-1]+1;
else lef[j]=1;
}

//为节省数一数所耗时间而做好的预处理表格,时间复杂度从O(n^3)可降到O(n^2),因为每次得到长度的时间从O(n)降到O(1)

/*for(int j=0;j<n;j++) cout<<righ[j]<<" ";
cout<<endl;
for(int j=0;j<n;j++) cout<<lef[j]<<" ";
cout<<endl;*/

int ans=-1;

//方法一:超时,耗时O(n^2)

/*for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
if(min(lef[j]+righ[i]-1,j-i)>ans) ans=min(lef[j]+righ[i]-1,j-i);*/

//方法二:只遍历一次i,j完全靠二分查找,耗时O(nlogn)

for(int i=1;i<=n;i++) g[i]=Max;
for(int i=0;i<n;i++)
{
int k=lower_bound(g+1,g+n+1,a[i])-g; //找出的k肯定大于i,且k-i大于k+j-1.
d[i]=k+righ[i]-1; //k即为从i开始的最大递增序列长度
if(a[i]<g[lef[i]]) g[lef[i]]=a[i]; //维护左值有序表
ans=max(ans,d[i]);
}

cout<<ans<<endl;

}
return 0;
}

转载于:https://www.cnblogs.com/zsyacm666666/p/4709088.html

你可能感兴趣的文章
PathInterpolator
查看>>
iOS底层原理总结 - 探寻block的本质(一)
查看>>
《面向对象精要》 学习笔记
查看>>
[译]开发者眼中 iOS 11 都更新了什么?
查看>>
这些常见的机器学习工具,不知道的快来补课
查看>>
你踩过几个?盘点微信H5小游戏开发中的那些坑
查看>>
Android 检测内存泄露
查看>>
express,koa2等node处理前端上传图片并保存到文件中
查看>>
回归初心:极简 Android 组件化方案 — AppJoint
查看>>
Flutter KO React Native? 让时间去决定吧...
查看>>
卷积自编码
查看>>
数据结构——二叉树遍历
查看>>
【预告】腾讯移动 即刻登陆 2017 GMTC 全球移动技术大会
查看>>
node Cluster 模块分析
查看>>
为什么 redux 要返回一个新的 state 引发的血案(二)
查看>>
《第一行代码》综合案例改进版
查看>>
一句话设置UITextField、UITextview的字数限制和placeholder
查看>>
再说深度学习是黑匣子,就把这篇文章糊 Ta 脸上
查看>>
轻松搞定RocketMQ入门
查看>>
babel编译配置的正确姿势
查看>>