题目链接

BZOJ5158

题解

题中所给的最长上升子序列其实就是一个限制条件

我们要构造出最大的以\(i\)开头的最长下降子序列,就需要编号大的点的权值尽量小

相同时当然就没有贡献,所以我们不妨令权值为一个\(1\)到\(n\)的排列

考虑如何满足限制条件

对于所有\(a[i] = v\)的点,点与点之间一定是单调下降的,连边

对于位置\(i\),如果\(a[i] = v\),那么\(i\)至少要比上一个\(a[j] = v - 1\)的位置大,连边

然后用大顶堆拓扑排序即可得到每个点最优的权值

\(O(nlogn)\)求一遍最长下降子序列长度即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define cls(s) memset(s,0x3f3f3f3f,sizeof(s))
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne = 2,de[maxn];
struct EDGE{int to,nxt;}ed[maxn << 1];
inline void build(int u,int v){
ed[ne] = (EDGE){v,h[u]}; h[u] = ne++;
de[v]++;
}
int last[maxn],n,a[maxn],cnt,x[maxn],f[maxn],bac[maxn];
priority_queue<int> q;
void work(){
REP(i,n) if (!de[i]) q.push(i);
int u;
while (!q.empty()){
u = q.top(); q.pop();
x[u] = ++cnt;
Redge(u) if (!(--de[to = ed[k].to])) q.push(to);
}
}
int getn(int t){
int l = 0,r = n,mid;
while (l < r){
mid = (l + r + 1) >> 1;
if (bac[mid] < t) l = mid;
else r = mid - 1;
}
return l;
}
void cal(){
cls(bac); bac[0] = 0;
for (int i = n; i; i--){
f[i] = getn(x[i]) + 1;
bac[f[i]] = min(bac[f[i]],x[i]);
}
LL ans = 0;
REP(i,n) ans += f[i];
printf("%lld\n",ans);
}
int main(){
n = read();
REP(i,n) a[i] = read();
for (int i = 1; i <= n; i++){
if (a[i] > 1) build(last[a[i] - 1],i);
if (last[a[i]]) build(i,last[a[i]]);
last[a[i]] = i;
}
work();
cal();
return 0;
}

BZOJ5158 [Tjoi2014]Alice and Bob 【贪心 + 拓扑】的更多相关文章

  1. [bzoj5158][Tjoi2014]Alice and Bob

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

  2. Alice and Bob(贪心HDU 4268)

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

  3. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

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

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

  5. [TJOI2014] Alice and Bob

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

  6. [BZOJ 5158][Tjoi2014]Alice and Bob

    传送门 \(\color{green}{solution}\) 贪心 /************************************************************** P ...

  7. HDU4268 Alice and Bob(贪心+multiset)

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...

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

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

  9. HDU 4268 Alice and Bob(贪心+Multiset的应用)

     题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...

随机推荐

  1. rsync+lsyncd实现实时同步

    1.接收端安装rsync,修改/etc/rsyncd.conf配置文件,然后启动服务. uid = rootgid = rootuse chroot = nomax connection = 4sec ...

  2. opencms研究笔记

    最近公司一新产品,众多选型之后: 最后还是准备在用opencms 在opencms的基础上,进行二次开发: 有一起研究的没 欢迎交流:

  3. jquery横向手风琴效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 时间轮算法的定时器(Delphi)

    源码下载 http://files.cnblogs.com/lwm8246/uTimeWheel.rar D7,XE2 编译测试OK //时间轮算法的定时器 //-- : QQ unit uTimeW ...

  5. 使用postMan测试insert或者update接口

    URL : http://localhost:8099/orderVoice/updateAgentLogin?access_token=7f10e803-f886-47df-b3dc-9ed307d ...

  6. aspx页面 按钮不响应回车键

    aspx页面在IE浏览器中,页面上的按钮默认都响应回车键,但有的时候我们的文本框可能需要响应回车键,这时我们就不想让按钮再响应回车键,这时我们只需要设置按钮的属性即可. 按钮分为两种,一种是<b ...

  7. scala Actor -03

    1.对于上一篇讲解的scala的一些补充 val files = Array[String]("a.txt","b.txt","c.txt" ...

  8. Android 图片放错位置会拉伸变形

    今天做了一个很小的需求,然后需要图片,我给ui要图片.直接给了我三套,还命名 x . xx. 2k 真的一开始都不知道.没有玩过这么正规的.我就用了一张,放到了hdpi下面. 后来同事帮我才知道, 图 ...

  9. Oracle 11g数据库安装与卸载的方法图解(windows)

    一.Oracle 11g安装 安装之前要先确定自己的电脑配置,以windows为例,如果是win7以下系统如xp等,可以选择Oracle 10g.因为10g的程序文件只有200多兆,而11g及达到了2 ...

  10. Postman-CI集成Jenkins(3)

    Postman-CI集成Jenkins(3) Postman-简单使用 Postman-进阶使用 Postman-CI集成Jenkins Newman 官方说明:Postman's command-l ...