BZOJ1046 HAOI2007 上公升序列

2022-05-08 07:51:10 字數 2537 閱讀 1659

對於乙個給定的s=,若有p=,滿足(x1 < x2 < … < xm)且( ax1 < ax

2 < … < axm)。那麼就稱p為s的乙個上公升序列。如果有多個p滿足條件,那麼我們想求字典序最小的那個。任務給

出s序列,給出若干詢問。對於第i個詢問,求出長度為li的上公升序列,如有多個,求出字典序最小的那個(即首先

x1最小,如果不唯一,再看x2最小……),如果不存在長度為li的上公升序列,則列印impossible.

第一行乙個n,表示序列一共有n個元素第二行n個數,為a1,a2,…,an 第三行乙個m,表示詢問次數。下面接m

行每行乙個數l,表示要詢問長度為l的上公升序列。n<=10000,m<=1000

對於每個詢問,如果對應的序列存在,則輸出,否則列印impossible.

63 4 1 2 3 636

45impossible

1 2 3 6

impossible

正解:dp+貪心

解題報告:

其實挺水的卻花了我半個多小時。

顯然先dp做最長上公升子串行,然後做貪心。顯然從前往後掃,每次掃到第乙個發現長度大於要求長度,且當前值》last的就輸出,然後長度--,last=當前值。這樣貪心應該是正確的。

我wa了兩發,因為當長度變成0時應該及時跳出,而不是往後做。

然後我還學了一下nlogn的最長上公升子串行的dp,思想也很簡單,就是從後往前做,然後維護每種長度當前的開始的結點的值最大值,對於每個i都二分查詢出它之後可以接的最大長度。這顯然是nlogn的。

n^2 的做法:

1

//it is made by jump~

2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include

13#ifdef win32

14#define ot "%i64d"

15#else

16#define ot "%lld"

17#endif

18using

namespace

std;

19 typedef long

long

ll;20

const

int maxn = 10011;21

const

int inf = (1

<<30

);22

intn,m,maxl;

23int

a[maxn],f[maxn];

2425 inline int

getint()

2634

35 inline void

work()

43 f[i]=len+1; maxl=max(f[i],maxl);44}

45 m=getint(); int

x,last;

46for(int o=1;o<=m;o++)

50for(int i=1;i<=n;i++) 57}

58}59}

6061

intmain()

62

nlogn的做法:

1

//it is made by jump~

2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include

13#ifdef win32

14#define ot "%i64d"

15#else

16#define ot "%lld"

17#endif

18using

namespace

std;

19 typedef long

long

ll;20

const

int maxn = 10011;21

const

int inf = (1

<<30

);22

intn,m;

23int a[maxn],f[maxn],c[maxn];//

c[i]表示長度為i的開始結點的值的最大值

24int

maxl;

2526 inline int

getint()

2735

36 inline int search(int x)

43return

pos;44}

4546 inline void

work()

54 m=getint(); int

last;

55for(int o=1;o<=m;o++)

59for(int i=1;i<=n;i++)65}

66}67}

6869

intmain()

70

BZOJ 1046 HAOI 上公升序列

1046 haoi2007 上公升序列 time limit 10 sec memory limit 162 mbsubmit 5376 solved 1862 submit status discuss description 對於乙個給定的s 若有p 滿足 x1 x2 xm 且 ax1 2 出s...

bzoj 1046 HAOI2007 上公升序列

首先用f i 表示從i開始的最長上公升子串行的長度 注意這裡和平時的不一樣,是以i開頭而不是以1到i 這就相當於倒序做一遍最長下降子串行 然後要用到貪心 首先假設要取長度為x的,如果比算出來的max大 max正序倒序都一樣的 肯定無解 然後從頭開始取,因為從頭取的下標字典序最小,如果a i 比上乙個...

BZOJ1046 HAOI2007 上公升序列

portal 首先是否存在很容易。考慮如何輸出最小字典序的方案。注意。是位子的字典序,不是值。那麼這樣的話,倒過來做一遍最長下降子串行。f i 表示以 i 開頭的最長上公升子串行長度 找答案的時候就順著找,每次滿足條件就輸出。include include include define n 1000...