UVA1471-Defense Lines(思维+STL)
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
Sample Input
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)的更多相关文章
- 【二分】Defense Lines
[UVa1471] Defense Lines 算法入门经典第8章8-8 (P242) 题目大意:将一个序列删去一个连续子序列,问最长的严格上升子序列 (N<=200000) 试题分析:算法1: ...
- UVA - 1471 Defense Lines 树状数组/二分
Defense Lines After the last war devastated your country, you - as the ...
- 1471 - Defense Lines
After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...
- Anton and Lines(思维)
Anton and Lines time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- hud 5124 lines(思维 + 离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines Problem Description: John has several lines. ...
- hdu 4779 Tower Defense (思维+组合数学)
Tower Defense Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 883H - Palindromic Cut(思维+STL)
题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...
- 【uva 1471】Defense Lines(算法效率--使用数据结构+部分枚举+类贪心)
P.S.我完全一个字一个字敲出来的血泪史啊~~所以,没有附代码,也是可以理解的啦.OvO 题意:给一个长度为N(N≤200000)的序列,要删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增 ...
- UVa 1471 (LIS变形) Defense Lines
题意: 给出一个序列,删掉它的一个连续子序列(该子序列可以为空),使得剩下的序列有最长的连续严格递增子序列. 分析: 这个可以看作lrj的<训练指南>P62中讲到的LIS的O(nlogn) ...
随机推荐
- Java中关于Map的使用(HashMap、ConcurrentHashMap)
在日常开发中Map可能是Java集合框架中最常用的一个类了,当我们常规使用HashMap时可能会经常看到以下这种代码: Map<Integer, String> hashMap = new ...
- eclipse编写js代码没有提示
安装插件 点击Help,选择Eclipse Marketplace... 搜索js,安装AngularJS Eclipse 重启eclipse,右键项目,选择Configure(配置),选择Conve ...
- 字符串方法之padStart和padEnd
ECMAScript 2017 有两个新的字符串方法:padStart和padEnd; 很有用啊啊,不用写if判断啦!开心脸 padStart在字符串开始出填充,padStart(num,‘要填充的 ...
- Javascript删除数组里的某个元素
删除array数组中的某个元素,首先需要确定需要删除元素的索引值. ? 1 2 3 4 5 6 7 var arr=[1,5,6,12,453,324]; function indexOf(val){ ...
- 学用纯CSS3打造可折叠树状菜单
CSS执行顺序与优先权的问题其实就是一个冲突解决的问题,当同一个元素(或内容)被CSS选择符选中时,就要按照优先权取舍不同的CSS规则,这其中涉及到的问题其实很多.首先就是CSS规则的specific ...
- bootstrap timepicker 在angular中取值赋值 并转化为时间戳
上一篇我们讲到angular对于timepicker的一个封装后的插件angular-bootstrap-timepicker,但是由于angular的版本必须是v1.2.30以上的.对于有些涉及到多 ...
- 照葫芦画瓢系列之Java --- Maven的配置
一.Maven仓库分类 Maven中,仓库只分为两类:本地仓库和远程仓库.当Maven根据坐标寻找构件的时候,它首先去查看本地仓库,如果本地仓库有此构件,则直接使用,如果本地仓库不存在此构件,或者需要 ...
- java排序算法之希尔排序
希尔排序是冲破二次时间屏障的第一批算法之一. 它是通过比较相距一定间隔的元素来工作,各趟比较所用的距离随着算法的进行而减小,直到最后一趟(比较相邻元素)为止.因此希尔排序也叫缩减增量排序. 希尔排序使 ...
- 小程序实践(五):for循环绑定item的点击事件
微信展示列表效果借助于 wx:for 简单写一个列表(wxml文件中): 对应的数据源(js文件中): 写一个点击监听: 效果: 以上.可以实现列表的item点击效果,但是无法到点击的item对应的 ...
- 【爬虫】在使用xpath时,排除指定标签
xpath排除某个节点 主要时应用name()这个函数获取便签名 res = html.xpath("//*[name(.)!='style']")