[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 ...
随机推荐
- 基于CacheManager组件的缓存产品配置
一.Couchbase 使用CacheManager组件,在配置Couchbase缓存支持时,由于对配置节cache handle命名规则要求不了解,费了点时间查了源码才明白. section配置节 ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
- U盘安装RedHat linux 5.3
U盘安装RedHat linux 5.3 1.下载rhel-5.3-server-i386-dvd.iso文件: 2.下载绿色版UltraISO软件: 3.将rhel-5.3-server-i386- ...
- Mac Android8.0源码编译笔记
原因:内存不够 办法:添加限制,输入如下命令:export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompil ...
- Eclipse连接数据库
原创 操作数据库之前首先得连接数据库,连接数据库的步骤如下: 将驱动包导入JDK中 将sqljdbc4.jar(一个举例)类库文件拷贝到D:\Program Files\Java\jdk1.7.0\j ...
- js链式调用
我们都很熟悉jQuery了,只能jQuery中一种非常牛逼的写法叫链式操作 * $('#div').css('background','#ccc').removeClass('box').stop() ...
- MySQL如何查看连接数和状态
查看连接数 命令:show processlist 如果要是root账号,能够看见当前所有用户的连接.如果是普通账号,只能看到自己占用的连接数. show processlist只能是列出前100 ...
- [LeetCode 题解]: Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- linux下PHP5.5的安装【oci8,pdo-oci,memcache,Zend OPCache扩展】
最近一段时间学习了一下PHP,用CI做了一个小项目,为了开发方便,本地windows下使用了集成环境XAMPP,不过当把项目部署到linux上时,确实遇到了很多问题,下面把我在linux上安装php的 ...
- (zxing.net)一维码Code 39的简介、实现与解码
一.简介 一维码Code 39:由于编制简单.能够对任意长度的数据进行编码.支持设备广泛等特性而被广泛采用. Code 39码特点: 能够对任意长度的数据进行编码,其局限在于印刷品的长度和条码阅读器的 ...