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) ...
随机推荐
- 【转】AtomicReference与volatile的区别
来源:AtomicReference与volatile的区别 AtomicReference与volatile的在可见性上的意义是一致的. volatile不能保证原子性,AutomicReferen ...
- Ubuntu 安装 chrome
依次执行命令: sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ wget - ...
- Springboot整合Websocket遇到的坑
Springboot整合Websocket遇到的坑 一.使用Springboot内嵌的tomcat启动websocket 1.添加ServerEndpointExporter配置bean @Confi ...
- datetime模块+calendar模块
datetime: """ 模块中的类: datatime 同时有时间和日期 timedelta 主要用于计算时间的跨度 tzinfo 时区相关 time 只关注时间 d ...
- ReactDom
今天工作中使用了这个,感觉很好用啊! 首先: 这个ReactDom是干嘛用的? 答: react-dom 包提供了 DOM 特定的方法,可以在你的应用程序的顶层使用,如果你需要的话,也可以作为 R ...
- WORLD 文件选择的操作方法
1,按住鼠标左键拖动选择文本. 2,双击鼠标可选中光标前面一个字,如果光标左右两边是一个词,那么就会选中整个词. 3,三击-----整段. 4,光标(鼠标)移至文本左边(外面),变成向右倾斜的光标箭头 ...
- js 对象转数组
function objToArray(array) { var arr = [] for (var i in array) { arr.push(array[i]); } console.log(a ...
- jQuery效果之jQuery Color animation 色彩动画扩展
jQuery 的动画方法(animate)支持各种属性的过渡,但是默认并不支持色彩的过渡,该插件正是来补足这一点! PS: 该插件支持 RGBA 颜色的过渡,但是请注意,IE8以下的版本不支持 RGB ...
- Salesforce 数据备份和恢复小知识
数据备份的类型 在Salesforce中可以使用多种API进行数据备份,它们是: REST API SOAP API Buik API Metadata API 数据备份有三种选择: 完全备份(Ful ...
- Linux PCI设备驱动的实现思路与思想
概述 1.PCI设备一般都具有双重身份,一方面作为PCI设备注册到Linux内核,另一方面,作为字符设备或者块设备,或者网络设备注册到Linux内核,所以,在看PCI设备时一定要注意到这点. 2. 一 ...