BZOJ5158 [Tjoi2014]Alice and Bob 【贪心 + 拓扑】
题目链接
题解
题中所给的最长上升子序列其实就是一个限制条件
我们要构造出最大的以\(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 【贪心 + 拓扑】的更多相关文章
- [bzoj5158][Tjoi2014]Alice and Bob
好羞愧啊最近一直在刷水... 题意:给定序列$c$的$a_i$,构造出一个序列$c$使得$\sum b_i$最大. 其中$a_i$表示以$c_i$结尾的最长上升子序列长度,$b_i$表示以$c_i$为 ...
- Alice and Bob(贪心HDU 4268)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- [TJOI2014]Alice and Bob[拓扑排序+贪心]
题意 给出一个序列的以每一项结尾的 \(LIS\) 的长度a[],求一个序列,使得以每一项为开头的最长下降子序列的长度之和最大. \(n\leq 10^5\) . 分析 最优解一定是一个排列,因为如果 ...
- [TJOI2014] Alice and Bob
非常好的一道思维性题目,想了很久才想出来qwq(我好笨啊) 考虑a[]数组有什么用,首先可以yy出一些性质 (设num[i]为原来第i个位置的数是什么 , 因为题目说至少有一个排列可以满足a[],所以 ...
- [BZOJ 5158][Tjoi2014]Alice and Bob
传送门 \(\color{green}{solution}\) 贪心 /************************************************************** P ...
- HDU4268 Alice and Bob(贪心+multiset)
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
- 关于TJOI2014的一道题——Alice and Bob
B Alice and Bob •输入输出文件: alice.in/alice.out •源文件名: alice.cpp/alice.c/alice.pas • 时间限制: 1s 内存限制: 128M ...
- HDU 4268 Alice and Bob(贪心+Multiset的应用)
题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...
随机推荐
- lintcode_69_二叉树的层次遍历
二叉树的层次遍历 描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? LinkedIn Airb ...
- Struts2基础学习2
Struts2基础学习2 项目结构,测试页面与实体类 <%@ page language="java" contentType="text/html; charse ...
- Navicat-12.0.26的激活
1.卸载掉早期版本,卸载干净,然后安装最新版Navicat(使用群文件中Iobit uninstaller8卸载) 2.安装完成后将破解补丁复制到安装目录下,运行破解补丁. 4.先patch,然后选择 ...
- 用ajax获取淘宝关键字接口
可定需要查看淘宝界面的结构,按F12查看网页,此时先清除一下网页中的数据,让Network制空,随后在输入框中输入新的内容,比如钱包,数据中会出现新的数据.点击及查看蓝色方框中的内容 点击之后,你可以 ...
- is和==,encode和decode
0.编码解码 >encode和decode a = "你好" s = a.encode("GBK") print(s) # b'\xc4\xe3\xba\ ...
- 图像压缩函数imagecopyresampled
<?php //制作缩略图.图像压缩 //参数1:目的地图像资源(通常指的是画布资源) $dst_image = imagecreatetruecolor(100, 100); $color = ...
- php生成微信小程序二维码源码
目前有3个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口.第一步:获取 access_token public function getWxAccessToken(){ $appid ...
- Linux下MySQL安装及配置
Linux下MySQL安装及配置 安装MySQL Ubuntu系统中,直接使用apt install的方式去安装MySQL的服务端和客户端,MySQL的客户端必须安装,否则无法通过命令连接并操作MyS ...
- 搭建Linpack
环境:vmware workstation14 + centos7(linux基本都可以) 一.开始安装mpich 1. 解决依赖gcc gcc-gfortran sudo yum install g ...
- [bzoj3196][tyvj1728]普通平衡树
真是太差了,到现在才打出一个平衡树的板子.. 感谢blackjack大佬提供的数组版treap板子!!基本完全照搬,blackjack太神啦! 但目前我只会这几个最基本的操作(说白了STL的(mult ...