CF 319B Psychos in a Line 【单调队列】
维护一个单调下降的队列。
对于每一个人,只需要找到在他前面且离他最近的可以杀掉他的人即可。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 100005
vector<int> v;
int f[N], n, t, cnt; int main() {
scanf("%d", &n);
memset(f, 0, sizeof(f)); for (int i=0; i<n; i++) {
scanf("%d", &t);
cnt = 0;
if (v.size() == 0) { v.push_back(t); continue; }
while (v.back() < t) {
cnt = max(cnt, f[v.back()]);
v.pop_back();
if (v.size() == 0) break;
}
if (v.size() == 0) { v.push_back(t); continue; }
f[t] = cnt + 1;
v.push_back(t);
} int ans = 0;
for (int i=0; i<n; i++) ans = max(ans, f[i]);
printf("%d\n", ans); return 0;
}
CF 319B Psychos in a Line 【单调队列】的更多相关文章
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...
- Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp
D. Psychos in a Line time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces 319B Psychos in a Line(模拟)
There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At eac ...
- 【模板】deque实现单调队列
双端队列deque容器: 关于deque最常用的有这几个函数: 都是成员函数 双端队列模板题:[洛谷]P2952 [USACO09OPEN]牛线Cow Line #include<iostrea ...
- Psychos in a Line CodeForces - 319B (单调栈的应用)
Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...
- codeforces 251A Points on Line(二分or单调队列)
Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...
- 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列
第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...
- hdu 3401 单调队列优化DP
Trade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu3530 单调队列
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
随机推荐
- shorter concat [reverse longer]
shorter concat [reverse longer] Description: Given 2 strings, a and b, return a string of the form: ...
- EDM排版table设置padding在ie7下bug
今天搞EDM发现一个在ie7下很扯淡的bug,由于以前没遇到过,所以花了点时间来解决下. IE7下bug重现: <table cellpadding="0" cellspac ...
- 结构体 lock_sys
typedef struct lock_sys_struct lock_sys_t; extern lock_sys_t* lock_sys; struct lock_sys_struct{ hash ...
- 深入理解Arrays.sort()
两种方法: 1.类本来就实现java.lang.Comparable接口,使类本身就有比较能力.接口实现compareTo方法,次方法接收另一个Object为参数,如果当前对象小于参数则返回负值,如果 ...
- Android 隐藏RadoiButton左边按钮
声明方式 添加属性 android:button=“@null”? 代码方式 radioBtn.setButtonDrawable(new StateListDrawable());
- shell部分命令缩写
bin = BINaries /dev = DEVices /etc = ETCetera /lib = LIBrary /proc = PROCesses /sbin = Superuser BIN ...
- 【js与jquery】电子邮箱、手机号、邮政编码的正则验证
//验证邮政编码 $("#postcode").blur(function(){ //获取邮政编码 var postcode=$("#postcode").va ...
- JS分页 + 获取MVC地址栏URL路径的最后参数
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- linux 查看用户所在组(groups指令的使用) 含实例
经常将某个文件夹的权限赋给某个用户的时候,也需要配置该用户所在的组,因此,我们需要查看该用户有哪些组,我们可以使用如上命令查看用户所在组 [oracle@gl ~]$ vi /etc/group ro ...
- Android ViewTreeObserver简介
Android ViewTreeObserver简介 一.结构 public final class ViewTreeObserver extends Object java.lang.Objec ...