[BZOJ 5158][Tjoi2014]Alice and Bob
\(\color{green}{solution}\)
贪心
/**************************************************************
Problem: 5158
User: MiEcoku
Language: C++
Result: Accepted
Time:420 ms
Memory:5980 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct Edge {
int v; Edge *next;
Edge (int v, Edge *next) : v(v), next(next) {}
Edge () {}
}*head[maxn], pool[maxn << 1], *pis = pool;
int val[maxn], rd[maxn], top[maxn], n, cnt;
long long ans;
inline void link(int a, int b) {
head[a] = new (pis ++)Edge(b, head[a]); rd[b]++;
}
priority_queue<int> q;
int tr[maxn << 2];
inline void modify(int c, int l, int r, int L, int o) {
if( l == r) { tr[c] = o; return;}
int mid = l + r >> 1;
if( L <= mid) modify(c << 1, l, mid, L, o);
else modify(c << 1|1, mid+1, r, L, o);
tr[c] = max(tr[c << 1], tr[c << 1|1]);
}
int qry(int c, int l, int r, int L) {
if( r <= L) return tr[c];
int mid = l + r >> 1;
if( L <= mid) return qry(c << 1, l, mid, L);
return max(tr[c << 1], qry(c << 1|1, mid+1, r, L));
}
int main() {
scanf("%d", &n);
for ( register int i = 1, x; i <= n; ++ i) {
scanf("%d", &x); if( (x-1)) link(top[x-1], i);
if( top[x]) link(i, top[x]); top[x] = i;
}
for ( register int i = 1; i <= n; ++ i)
if( !rd[i]) q.push(i);
while ( !q.empty()) {
int u = q.top(); q.pop();
val[u] = ++cnt;
for ( Edge *now = head[u]; now; now = now->next)
if( !(--rd[now->v])) q.push(now->v);
}
for ( register int i = n, ret; i; -- i) {
ret = 1 + qry(1, 1, n, val[i]);
modify(1, 1, n, val[i], ret); ans += ret;
}
printf("%lld\n", ans);
}

[BZOJ 5158][Tjoi2014]Alice and Bob的更多相关文章
- [TJOI2014]Alice and Bob[拓扑排序+贪心]
题意 给出一个序列的以每一项结尾的 \(LIS\) 的长度a[],求一个序列,使得以每一项为开头的最长下降子序列的长度之和最大. \(n\leq 10^5\) . 分析 最优解一定是一个排列,因为如果 ...
- [bzoj5158][Tjoi2014]Alice and Bob
好羞愧啊最近一直在刷水... 题意:给定序列$c$的$a_i$,构造出一个序列$c$使得$\sum b_i$最大. 其中$a_i$表示以$c_i$结尾的最长上升子序列长度,$b_i$表示以$c_i$为 ...
- BZOJ5158 [Tjoi2014]Alice and Bob 【贪心 + 拓扑】
题目链接 BZOJ5158 题解 题中所给的最长上升子序列其实就是一个限制条件 我们要构造出最大的以\(i\)开头的最长下降子序列,就需要编号大的点的权值尽量小 相同时当然就没有贡献,所以我们不妨令权 ...
- [TJOI2014] Alice and Bob
非常好的一道思维性题目,想了很久才想出来qwq(我好笨啊) 考虑a[]数组有什么用,首先可以yy出一些性质 (设num[i]为原来第i个位置的数是什么 , 因为题目说至少有一个排列可以满足a[],所以 ...
- 关于TJOI2014的一道题——Alice and Bob
B Alice and Bob •输入输出文件: alice.in/alice.out •源文件名: alice.cpp/alice.c/alice.pas • 时间限制: 1s 内存限制: 128M ...
- UOJ #266 【清华集训2016】 Alice和Bob又在玩游戏
题目链接:Alice和Bob又在玩游戏 这道题就是一个很显然的公平游戏. 首先\(O(n^2)\)的算法非常好写.暴力枚举每个后继计算\(mex\)即可.注意计算后继的时候可以直接从父亲转移过来,没必 ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- bzoj4730: Alice和Bob又在玩游戏
Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
随机推荐
- spring之IOC容器创建对象
1.术语了解 1.1组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架非侵入式设计 引入了框架,对现有的类结构没有影响. 例如:Hi ...
- [模板]割点(tarjan)
洛谷P3388 注意:记得tarjan的打法 注意割点的判断条件:子节点个数>2并且为根节点 当它不为根节点时并且low[to]>dfn[u] 判断时是在子节点未被记录的时候 #incl ...
- 使用python管理Cisco设备-乾颐堂
今天发现一个老外使用python写的管理cisco设备的小框架tratto,可以用来批量执行命令. 下载后主要有3个文件: Systems.py 定义了一些不同设备的操作系统及其常见命令. Conne ...
- [GO]结构体的值传递和地址传递
package main import "fmt" type student struct { id int name string sex byte age int addr s ...
- JW Player开始播放和结束播放事件的捕获
项目中使用到jwPlayer,需要在用户开始播放盒结束播放的时候触发一些事件,所以研究JWPlayer. <script type="text/javascript"> ...
- 洛谷P4174 [NOI2006]最大获利(最大流)
题目描述 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU 集团旗下的 CS&T 通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需 ...
- jQuery控制iframe框架内元素
用jQuery在IFRAME里取得父窗口的某个元素的值只好用DOM方法与jquery方法结合的方式实现了 1.在父窗口中操作 选中IFRAME中的所有单选钮$(window.frames[" ...
- 利用html5 postMessage接口跨域设置iframe大小
<!doctype html> <html> <head> <title>Document A</title> <meta chars ...
- MySQL8.0本地访问设置为远程访问权限
1.登录MySQL mysql -u root -p 输入您的密码 2.选择 mysql 数据库 use mysql; 因为 mysql 数据库中存储了用户信息的 user 表. 3.在 mysql ...
- 魅力 .NET:从 Mono、.NET Core[转]
前段时间,被问了这样一个问题:.NET 应用程序是怎么运行的? 当时大概愣了好久,好像也没说出个所以然,得到的回复是:这是 .NET 程序员最基本的...呵呵! 微软开源,其实不只是对 .NET 本身 ...