传送门

\(\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的更多相关文章

  1. [TJOI2014]Alice and Bob[拓扑排序+贪心]

    题意 给出一个序列的以每一项结尾的 \(LIS\) 的长度a[],求一个序列,使得以每一项为开头的最长下降子序列的长度之和最大. \(n\leq 10^5\) . 分析 最优解一定是一个排列,因为如果 ...

  2. [bzoj5158][Tjoi2014]Alice and Bob

    好羞愧啊最近一直在刷水... 题意:给定序列$c$的$a_i$,构造出一个序列$c$使得$\sum b_i$最大. 其中$a_i$表示以$c_i$结尾的最长上升子序列长度,$b_i$表示以$c_i$为 ...

  3. BZOJ5158 [Tjoi2014]Alice and Bob 【贪心 + 拓扑】

    题目链接 BZOJ5158 题解 题中所给的最长上升子序列其实就是一个限制条件 我们要构造出最大的以\(i\)开头的最长下降子序列,就需要编号大的点的权值尽量小 相同时当然就没有贡献,所以我们不妨令权 ...

  4. [TJOI2014] Alice and Bob

    非常好的一道思维性题目,想了很久才想出来qwq(我好笨啊) 考虑a[]数组有什么用,首先可以yy出一些性质 (设num[i]为原来第i个位置的数是什么 , 因为题目说至少有一个排列可以满足a[],所以 ...

  5. 关于TJOI2014的一道题——Alice and Bob

    B Alice and Bob •输入输出文件: alice.in/alice.out •源文件名: alice.cpp/alice.c/alice.pas • 时间限制: 1s 内存限制: 128M ...

  6. UOJ #266 【清华集训2016】 Alice和Bob又在玩游戏

    题目链接:Alice和Bob又在玩游戏 这道题就是一个很显然的公平游戏. 首先\(O(n^2)\)的算法非常好写.暴力枚举每个后继计算\(mex\)即可.注意计算后继的时候可以直接从父亲转移过来,没必 ...

  7. 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  8. bzoj4730: Alice和Bob又在玩游戏

    Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...

  9. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

随机推荐

  1. LA3983 捡垃圾的机器人

    Problem C - Robotruck Background This problem is about a robotic truck that distributes mail package ...

  2. JMS 之 Active MQ 的消息传输

    本文使用Active MQ5.6 一.消息协商器(Message Broker) broke:消息的交换器,就是对消息进行管理的容器.ActiveMQ 可以创建多个 Broker,客户端与Active ...

  3. SIP简介

    说明:以下内容来着之前下载的一份文档,现将概念部分摘录在BLog,如需要完整文档将放在文件中或留言. SIP简介,第1部分:SIP初探 时间:2006-04-07作者:Emmanuel Proulx浏 ...

  4. query.validate.js使用说明+中文API

    转自:http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html 看到一篇好的文章不容易,记录下来以防丢失! 官网地址:http:/ ...

  5. ThinkPhp 生成静态页面

    //开启静态缓存'HTML_CACHE_ON' => true, //开启缓存'HTML_CACHE_TIME' =>60, //开启缓存时间'HTML_FILE_SUFFIX' => ...

  6. Linux ps 进程状态码

    D            不可中断睡眠(通常进程在进行I/O) R            运行中或者可运行状态(在运行队列中) S            可中断睡眠(等待event,进程idle中) ...

  7. [转]Control的Invoke和BeginInvoke

    转自:Control的Invoke和BeginInvoke  作者:Kuffy Wang 近日,被Control的Invoke和BeginInvoke搞的头大,就查了些相关的资料,整理如下.感谢这篇文 ...

  8. js 日期加一天或者减一天,最简单方法

    通过计算一天的时间戳是多少进行换算:(new Date(new Date().getTime()-86400000)).Format("yyyy-MM-dd hh:mm:ss")

  9. 适用于Java的嵌入式脚本语言

    此文已由作者赵昕授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. fakescript 轻量级嵌入式脚本语言 https://github.com/esrrhs/fakescr ...

  10. 「NOIP 2013」 货车运输

    题目链接 戳我 \(Solution\) 这一道题直接用\(kruskal\)重构树就好了,这里就不详细解释\(kruskal\)重构树了,如果不会直接去网上搜就好了.接下来讲讲详细过程. 首先构建\ ...