Problem UVA1471-Defense Lines

Accept: 297  Submit: 2776
Time Limit: 9000 mSec

Problem 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.

Input

The input contains several test cases. The first line of the input contains a positive integer Z ≤ 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 ≤ 2 · 105 denoting the number of towers. The second line contains n positive integers not larger than 109 separated by single spaces being the heights of the towers.

 Output

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.

 Sample Input

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

 Sample Output

4
6

题解:好题!首先是算法设计过程中的优化,将不可能成为最优解的情况删去,使得所有可能最优解构成单调序列,即可二分将复杂度从O(n^2)降到O(nlogn).

其次就是实现时的技巧,这里结构体中 < 的设计时精髓,我原来总是想着把它定义的尽量严谨,对任意的情况都能准确找到二者的关系,但是有时候并不方便,这里只将 < 定义得与val有关,方便之处就体现在可以实现val的严格单调,具体原因我们可以来分析一下为什么只定义 < 就可以实现所有关系。

紫书中在第五章给出了符号重载的具体实现,当有了 < 之后 >= 就意味着 !< ,但是这里只有在val严格递增时 < 才会成立,因此如果两个Candidiate的val相同的话,<= 和 >= 都会成立,这时再看 == 的定义,发现它会返回true,也就是说,只要val相同就会返回true,这样在set里就不会有val相同的情况,只可能严格增,这正是我们想要的。这个操作大大简化了各种if else判断,mark下来。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  *  + ;

 int n, num[maxn];
int f[maxn], g[maxn]; struct Candidate {
int val, lon;
Candidate(int val = , int lon = ) : val(val), lon(lon) {}
bool operator < (const Candidate &a)const {
return val < a.val;
}
}; set<Candidate> iset; int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &num[i]);
} if (n == ) {
printf("1\n"); continue;
} g[] = ;
for (int i = ; i < n; i++) {
if (num[i] > num[i - ]) g[i] = g[i - ] + ;
else g[i] = ;
} f[n - ] = ;
for (int i = n - ; i >= ; i--) {
if (num[i] < num[i + ]) f[i] = f[i + ] + ;
else f[i] = ;
} int ans = ; iset.clear();
iset.insert(Candidate(num[], g[]));
for (int i = ; i < n; i++) {
Candidate v(num[i],g[i]);
set<Candidate>::iterator iter = iset.lower_bound(v);
bool keep = true;
if (iter != iset.begin()) {
Candidate pre = *(--iter);
int len = pre.lon + f[i];
ans = ans > len ? ans : len;
if (v.lon <= pre.lon) keep = false;
} if (keep) {
iset.erase(v);
iset.insert(v);
set<Candidate>::iterator it = iset.find(v);
it++;
while (it != iset.end() && it->val > v.val && it->lon <= v.lon) {
iset.erase(it++);
}
}
}
printf("%d\n", ans);
}
return ;
}

UVA1471-Defense Lines(思维+STL)的更多相关文章

  1. 【二分】Defense Lines

    [UVa1471] Defense Lines 算法入门经典第8章8-8 (P242) 题目大意:将一个序列删去一个连续子序列,问最长的严格上升子序列 (N<=200000) 试题分析:算法1: ...

  2. UVA - 1471 Defense Lines 树状数组/二分

                                  Defense Lines After the last war devastated your country, you - as the ...

  3. 1471 - Defense Lines

    After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...

  4. Anton and Lines(思维)

    Anton and Lines time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. hud 5124 lines(思维 + 离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines   Problem Description: John has several lines. ...

  6. hdu 4779 Tower Defense (思维+组合数学)

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  7. 883H - Palindromic Cut(思维+STL)

    题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...

  8. 【uva 1471】Defense Lines(算法效率--使用数据结构+部分枚举+类贪心)

    P.S.我完全一个字一个字敲出来的血泪史啊~~所以,没有附代码,也是可以理解的啦.OvO 题意:给一个长度为N(N≤200000)的序列,要删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增 ...

  9. UVa 1471 (LIS变形) Defense Lines

    题意: 给出一个序列,删掉它的一个连续子序列(该子序列可以为空),使得剩下的序列有最长的连续严格递增子序列. 分析: 这个可以看作lrj的<训练指南>P62中讲到的LIS的O(nlogn) ...

随机推荐

  1. Go实现基于WebSocket的弹幕服务

    拉模式和推模式 拉模式 1.数据更新频率低,则大多数请求是无效的 2.在线用户量多,则服务端的查询负载高 3.定时轮询拉取,实时性低 推模式 1.仅在数据更新时才需要推送 2.需要维护大量的在线长连接 ...

  2. Python3 系列之 基础语法篇

    基础数据类型 整数 python 可以处理任意大小的整数 浮点数 python 可以处理任意大小的浮点数,但是需要注意的一点是:整数运算永远是精确的(除法也是精确的),而浮点数运算则可能会有四舍五入的 ...

  3. 用kafka实现消息推送

    一个人知道的Topic是单点推送,大家都知道Topic是广播. kafka消息消费机制: 1.广播消费:通过定义topic前缀来标识属于广播的消息(例如:topicname:gonggao153568 ...

  4. java.net.ProtocolException:unexpected end of stream

    原因:php 给android 写接口出现java.net.ProtocolException:unexpected end of stream,查找android方面原因时发现数据超长 ,发现htm ...

  5. IP 协议

    在网络层中,使用的是 ip 协议,它规定网络地址的协议. ip 地址分为两个部分: 网络部分:标识子网 主机部分:标识主机 子网掩码 表示子网络特征的一个参数,它规定 网络部分全部为1,主机部分全部为 ...

  6. Django下配置静态文件以及渲染图片

    js,css,img等都叫做静态文件,那么关于django中静态文件的配置,我们就需要在setting配置文件里面写上下面这些内容: #STATIC_URL = '/xxx/' #别名,随便写的名字, ...

  7. 【20190223】HTTP-知识点整理:HTTPS

    HTTPS:添加了加密及认证机制的HTTP HTTPS 并非是应用层的一种新协议.只是 HTTP 通信接口部分用SSL(Secure Socket Layer)和 TLS(Transport Laye ...

  8. -moz、-ms、-webkit浏览器前缀解释(PS:后续在详细解释)

    -moz-是Firefox Gecko内核,moz代表的是Firefox的开发商Mozill -ms代表ie浏览器私有属性 -webkit代表safari.chrome私有属性

  9. centos6 自带python2.6升级python2.7+

    centos6系统自带Python为2.6.6版本,升级搞版本操作如下(python2-python3都一样) 1.下载需要升级的python包 官方下载地址:https://www.python.o ...

  10. OkHttp3源码详解(六) Okhttp任务队列工作原理

    1 概述 1.1 引言 android完成非阻塞式的异步请求的时候都是通过启动子线程的方式来解决,子线程执行完任务的之后通过handler的方式来和主线程来完成通信.无限制的创建线程,会给系统带来大量 ...