1. 程式人生 > >bzoj千題計劃316:bzoj3173: [Tjoi2013]最長上升子序列(二分+樹狀陣列)

bzoj千題計劃316:bzoj3173: [Tjoi2013]最長上升子序列(二分+樹狀陣列)

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

#define N 100001

#define lowbit(x) x&-x

int n;
int p[N];

int c[N];

int a[N];
int dp[N],m;
int f[N];

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    
while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } } void add(int pos,int x) { while(pos<=n) { c[pos]+=x; pos+=lowbit(pos); } } int query(int x) { int sum=0; while(x) { sum+=c[x]; x-=lowbit(x); } return sum; }
int find(int x) { int l=1,r=n,mid,tmp; while(l<=r) { mid=l+r>>1; if(mid-query(mid)>=x) tmp=mid,r=mid-1; else l=mid+1; } return tmp; } void init() { read(n); for(int i=1;i<=n;++i) read(p[i]); int pos; for(int
i=n;i;--i) { pos=find(p[i]+1); a[pos]=i; add(pos,1); } } void pre_LIS() { dp[m=1]=a[1]; f[a[1]]=1; int pos; for(int i=2;i<=n;++i) { if(a[i]>dp[m]) { dp[++m]=a[i]; f[a[i]]=m; } else { pos=lower_bound(dp+1,dp+m+1,a[i])-dp; if(a[i]<dp[pos]) dp[pos]=a[i]; f[a[i]]=pos; } } } void solve() { int now=0; for(int i=1;i<=n;++i) { now=max(now,f[i]); printf("%d\n",now); } } int main() { init(); pre_LIS(); solve(); }

相關推薦

bzoj計劃316bzoj3173: [Tjoi2013]上升序列二分+陣列

#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 #define lowbit(x) x&-x

BZOJ3173: [Tjoi2013]上升序列

pac struct class -i 記得 cst 子序列 hide cnblogs 1~n<=1e5依次插入序列中某一個位置,求每次插入後的最長上升子序列。 因為新插入的數對前面插入的答案沒影響,所以只要能想方設法構造出最終序列即可。 方法一:平衡樹!。。。。 方

BZOJ】3173: [Tjoi2013]上升序列數組

nss 貢獻 isp 轉化 復雜 src printf efi col 【題意】給定ai,將1~n從小到大插入到第ai個數字之後,求每次插入後的LIS長度。 【算法】樹狀數組||平衡樹 【題解】 這是樹狀數組的一個用法:O(n log n)尋找前綴和為k的最小位置。(當數列

BZOJ - 5427上升序列 二分&思維

現在給你一個長度為n的整數序列,其中有一些數已經模糊不清了,現在請你任意確定這些整數的值, 使得最長上升子序列最長。(為何最長呢?因為hxy向來對自己的rp很有信心)   Input 第一行一個正整數n 接下來n行第i行格式如下

動態規劃上升序列二分演算法 nlogn

解題心得: 1、在資料量比較大的時候n^2會明顯超時,所以可以使用nlogn 的演算法,此演算法少了雙重迴圈,用的lower_bound(二分法)。 2、lis中的數字並沒有意義,僅僅是找到最小點lis[0]和最大點lis[len],其中,在大於lis[le

bzoj計劃315bzoj3172: [Tjoi2013]單詞AC自動機

#include<queue> #include<cstdio> #include<cstring> using namespace std; #define N 2000001 using namespace std; int pos[20

bzoj3173】[Tjoi2013]上升序列

isp ont const getch sed led hint 直接 close Description 給定一個序列,初始為空。現在我們將1到N的數字插入到序列中,每次將一個數字插入到一個特定的位置。每插入一個數字,我們都想知道此時最長上升子序列長度是多少? Inp

BZOJ 3173: [Tjoi2013]上升序列

treap做法太裸了 不打了(我哪會treap啊。。懶得打splay 233) 發現一種很妙的離線做法 樹狀陣列 一開始每個位置都為1 從後往前,找到字首和等他要加到的位置+1的位置,這個位置就是這個值最後的位置(不信你手玩一下) 然後把這個位置值變成0。 上面一步用樹狀陣列上二

【LG4309】【BZOJ3173】[TJOI2013]上升序列

【LG4309】【BZOJ3173】[TJOI2013]最長上升子序列 題面 洛谷 BZOJ 題解 插入操作顯然用平衡樹就行了 然後因為後面的插入對前面的操作無影響 就直接在插入完的序列上用樹狀陣列求下每個點為終點的最長上升子序就行了 然而懶得手寫平衡樹了 直接用了\(rope\) rope用法 程式

bzoj3173】: [Tjoi2013]上升序列

ms是個簡單的splay+nlogn的最長上升子序列。。? 但是我老是搞不清順序怎麼辦0.0 根據題目加入的順序發現的性質要注意阿0.0 orzorzlyh #include<iostream> #include<cstdio>

bzoj3173上升序列

節點 return hup 位置 一個 ostream online %d pre Portal --> bzoj3173 Solution   感覺自己需要智力康復qwq   首先題目給的這個序列肯定是一個\(1-n\)的排列,並且插入的順序是從小到大   仔細思考

[TJOI2013]上升序列

[TJOI2013]最長上升子序列 題目大意: 給定一個序列,初始為空。將\(1\sim n(n\le10^5)\)的數字插入到序列中,每次將一個數字插入到一個特定的位置。每插入一個數字後輸出LIS長度。 思路: 首先用線段樹(或rope)求出最終狀態的序列,然後就變成了普通的LIS問題。 原始碼:

初入DP上升序列LIS

Description 我們都知道上體育課時,體育老師會讓我們按身高從小到大(或從大到小)排成一排。可是近日體育老師周老師卻有點煩心, 他教的班級來了幾個插班生,可他們有的不守規矩,沒有按照身高大小來插入隊伍,導致隊伍很難看。現在周老師有一個問 題:給定一排人的身高,問能否至多去掉一個人,使

Leetcode 300上升序列詳細的解法!!!

給定一個無序的整數陣列,找到其中最長上升子序列的長度。 示例: 輸入: [10,9,2,5,3,7,101,18] 輸出: 4 解釋: 最長的上升子序列是 [2,3,7,101],它的長度是 4。

bzoj計劃311bzoj5017: [Snoi2017]炸彈線段優化tarjan構圖

#include<cstdio> #include<vector> #include<iostream> #include<algorithm> using namespace std; const int mod=1e9+7;

bzoj計劃319bzoj2865: 字串識別字尾自動機 + 線段

#include<map> #include<cstdio> #include<cstring> #include<algorithm> #define N 500001 using namespace std; char s[

bzoj計劃310bzoj5285: [Hnoi2018]尋寶遊戲(思維+雜湊)

#include<cstdio> #include<algorithm> #define N 5001 using namespace std; const int mod=1e9+7; int bit[N]; char s[N]; int has

bzoj計劃309bzoj4332: JSOI2012 分零食分治+FFT

#include<cmath> #include<cstdio> #include<algorithm> using namespace std; const int M=1<<17; #define N 10001 int m,

bzoj計劃321bzoj5251: [2018多省省隊聯測]劈配網路流 + 二分

#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d

bzoj計劃312bzoj2119: 股市的預測字尾陣列+st表

#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d