[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 ...
随机推荐
- gitlab centos 安装配置运维笔记
写在前面 如果你需要一个git服务器,为企业或自己的团队托管代码而又不希望将代码仓库存储到第三方.你可以在自己的服务器上搭建一个gitlab. 本文为我在最初安装配置gitlab服务器的时候留存的笔记 ...
- 设计模式(java)--Bridge模式之蜡笔与毛笔的故事
转自:吕震宇 http://www.cnblogs.com/zhenyulu/articles/67016.html#!comments 我想大家小时候都有用蜡笔画画的经历吧.红红绿绿的蜡笔一大盒,根 ...
- Smarty配置与实例化
在smarty文件夹下建立一个test文件夹,test下建立如下: 编辑test.php如下: <?php require('../smarty/Smarty.class.php'); $sma ...
- [GO]使用select实现斐波那契
package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...
- java.lang.NoClassDefFoundError: Could not initialize class com解决方案
编写的时候遇到这样一个bug, java.lang.NoClassDefFoundError: Could not initialize class com 纠结了两天多,但是,没有找到答案,这个问题 ...
- mybatis 存储过程调用
接口 UserInfoMapper.java xml UserInfoMapper.xml 如何关联 <?xml version="1.0" encoding=" ...
- shell 编程 变量
转自:http://blog.csdn.net/qq504196282/article/details/52994249 shell之变量和引用 分类:SHELL编程基础 (470) (0) 举报 ...
- Halcon标定与自标定
Halcon标定:https://blog.csdn.net/niyintang/article/details/78752585 Halcon自标定:https://www.cnblogs.com/ ...
- 团体程序设计天梯赛L3-010 是否完全二叉搜索树 2017-03-24 16:12 29人阅读 评论(0) 收藏
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- ES 内存深度解析
注: 本文主要针对ES 2.x. “该给ES分配多少内存?” “JVM参数如何优化?“ “为何我的Heap占用这么高?” “为何经常有某个field的数据量超出内存限制的异常?“ “为何感觉上没多少 ...