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静态数据的初始化
Java中无论创建多少对象,静态数据都只占一份存储区域. 下面程序示例静态存储区域的初始化: //: initialization/StaticInitialization.java // Speci ...
- Mybatis入门实例
MyBatis 简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- 内省(Introspector)
/** * 内省:通过反射来操作javabean * 内省类 --> Bean信息 --> 属性描述符 --> 属性的get/set对应的Method --> 进行反射 * c ...
- [HTML/CSS]三角形
CSS盒子模型 当我们把padding和width,height全部设置为0,border设为一个较大的像素时 即:我们需要什么方向的三角形,只需把其余的三角形背景色设置为transparent:
- [工具配置]使用requirejs模块化开发多页面一个入口js的使用方式
描述 知道requirejs的都知道,每一个页面需要进行模块化开发都得有一个入口js文件进行模块配置.但是现在就有一个很尴尬的问题,如果页面很多的话,那么这个data-main对应的入口文件就会很多. ...
- 山东理工大学SDUT - ACM OJ 题: Python代码 及分析
Python基础语法学习完成,先刷基础题100道巩固 ,附 题目.代码.知识分析 题目:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index ...
- ReactNative编写规范
<一> React 代码规范 文件与组件命名 扩展名: 使用.js作为js文件的扩展名.如果同一个文件夹下有同名而不同作用的js文件,则通过中缀(小写)进一步区分,例如:HomePage ...
- Android内存优化(五) Lint代码扫描工具
1.使用 工具栏 -> Analyze -> Inspect Code… 点击 Inspect Code 后会弹出检查范围的对话框: 默认是检查整个项目,我们可以点击 Custom sc ...
- 浅析C/C++中的switch/case陷阱
浅析C/C++中的switch/case陷阱 先看下面一段代码: 文件main.cpp #include<iostream> using namespace std; int main(i ...
- mysql学习之完整的select语句
本文内容: 完整语法 去重选项 字段别名 数据源 where group by having order by limit 首发日期:2018-04-11 完整语法: 先给一下完整的语法,后面将逐一来 ...