#include<bits/stdc++.h>
using namespace std;
unsigned seed=chrono::steady_clock().now().time_since_epoch().count();
mt19937_64 rng(seed);
struct Treap{
struct node{
int key,val,pos,pri,sz;
int mx,mxPos;
node *lch,*rch;
node(int _key,int _val,int _pos){
key=_key;
val=_val;
pos=_pos;
mx=val;
mxPos=pos;
pri=rng();
lch=rch=nullptr;
}
void pull(){
sz=1;
mx=val;
mxPos=pos;
if(lch){
sz+=lch->sz;
if(mx<lch->mx || (mx==lch->mx && lch->key>key)){
mx=lch->mx;
mxPos=lch->mxPos;
}
}
if(rch){
sz+=rch->sz;
if(mx<rch->mx || (mx==rch->mx && rch->key>key)){
mx=rch->mx;
mxPos=rch->mxPos;
}
}
}
};
node *rt=nullptr;
int fs(node *a){
return a ? a->sz : 0;
}
node *merge(node *a,node *b){
if(!a || !b) return a ? a : b;
if(a->pri < b->pri){
a->rch=merge(a->rch,b);
a->pull();
return a;
}else{
b->lch=merge(a,b->lch);
b->pull();
return b;
}
}
void split(node *T,node *&a,node *&b,int v){
if(!T){
a=b=nullptr;
return;
}
if(T->key <= v){
a=T;
split(T->rch,a->rch,b,v);
a->pull();
}else{
b=T;
split(T->lch,a,b->lch,v);
b->pull();
}
}
void splitSz(node *T,node *&a,node *&b,int p){
if(!T){
a=b=nullptr;
return;
}
if(fs(T->lch)<p){
a=T;
splitSz(T->rch,a->rch,b,p-fs(T->lch)-1);
a->pull();
}else{
b=T;
splitSz(T->lch,a,b->lch,p);
b->pull();
}
}
void insert(int k,int v,int p){
node *a=new node(k,v,p),*b;
split(rt,rt,b,k);
rt=merge(rt,a);
rt=merge(rt,b);
}
int find_max(int v){
node *a;
split(rt,rt,a,v-1);
int ret;
if(!rt) ret=0;
else ret=rt->mx;
rt=merge(rt,a);
return ret;
}
int find_max_pos(int v){
node *a;
split(rt,rt,a,v-1);
int ret;
if(!rt) ret=0;
else ret=rt->mxPos;
rt=merge(rt,a);
return ret;
}
void DeleteTree(node *n){
if(!n) return;
DeleteTree(n->lch);
DeleteTree(n->rch);
delete(n);
}
~Treap(){
DeleteTree(rt);
}
};
int dp[100010],v[100010],pre[100010];
// main solution
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int n,mx=0;
cin>>n;
Treap tp;
for(int i=1;i<=n;++i){
int a;
cin>>a;
v[i]=a;
dp[i]=tp.find_max(a)+1;
pre[i]=tp.find_max_pos(a);
tp.insert(a,dp[i],i);
mx=max(mx,dp[i]);
}
int mxPos=find(dp+1,dp+1+n,mx)-dp;
vector<int> lis;
while(mxPos>0){
lis.emplace_back(v[mxPos]);
mxPos=pre[mxPos];
}
reverse(lis.begin(),lis.end());
for(auto i:lis){
cout<<i<<" ";
}
cout<<"\n";
cout<<mx<<"\n";
}
定義子問題。
Dec 20, 2024給定一個帶權圖G,求一條路徑讓S到T的權重總和最小。
Aug 9, 2023題目敘述 神獸日京元帶著得意門生黃瓜學長去科博館,在館外有一個裝置,內有多個球不斷的被送進一個"單一"開口的管子,而過了一段時間後,系統會將管子傾斜並將部分的球送出。而現在正在舉辦一個活動,主辦單位將球編號(1,2,3...,n),而參加者要控制系統並將球經過操作後排成特定的順序,完成者能免費進入科博館。黃瓜學長作為一個資訊高手aka厭惡零錢大鈔主義者,又不想被坑錢,他必須完成目標。 輸入說明 第一行為一整數$n(0<n<10^5)$ 第二行有$n$個正數$a_1,a_2,....a_n(1 \le a_i \le n)$ 輸出說明 求第二行之排序有沒有可能達成,有的話輸出"Yes",否則輸出"No"
Jun 17, 2023相關線上題目,judge : 宜中程式解題系統 http://120.101.80.83/ 洛谷程式解題系統 https://www.luogu.com.cn/ Codeforces程式解題系統 https://codeforces.com/
Jun 17, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up