题目链接

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. ABAP 调用远程rfc

    ABAP 调用rfc DESTINATION附加项后面接的是远程目标名称,该目标在事务SM59中设定,其中包含连接和登录远程系统所需的全部参数信息.如果调用的就是本机的RFC目标,则DESTINATI ...

  2. Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 36

    例: <aop:config> <aop:pointcut expression="execution(* com.zsn.Service.Impl.*.*(..))&qu ...

  3. Mysql:case when then end 的用法

    0.创建一张数据表 表名为 test_when_case CREATE TABLE `test_when_case` ( `id` int(11) unsigned NOT NULL AUTO_INC ...

  4. Open source cryptocurrency exchange

    Peatio: https://github.com/peatio/peatio ViaBTC: https://github.com/viabtc/viabtc_exchange_server

  5. jira安装说明

    阅读目录 1.1 jira说明 1.2 安装配置jira 1.3 web界面访问 1.4 创建第一个项目 1.5 参考文献 回到顶部 1.1 jira说明 JIRA是Atlassian公司出品的项目与 ...

  6. 004---Django简单示例

    一.MVC与MTV模型  在web开发领域里著名的MVC模式,所谓MVC就是把web应用分为模型(M).控制器(C).视图(V)三层,达到了解耦的效果. 一次完整的请求如图: 但是django用的是M ...

  7. POJ:2674-Linear world(名字交换碰撞)

    Linear world Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4514 Accepted: 1025 Descript ...

  8. ABAP CDS - SELECT, association

    ABAP CDS - SELECT, association Syntax ... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_e ...

  9. HDFS写数据和读数据流程

    HDFS数据存储 HDFS client上传数据到HDFS时,首先,在本地缓存数据,当数据达到一个block大小时.请求NameNode分配一个block. NameNode会把block所在的Dat ...

  10. Java继承的缺点

    转载自:https://www.cnblogs.com/xz816111/archive/2018/05/24/9080173.html JAVA中使用到继承就会有两个无法回避的缺点: 1.打破了封装 ...