【bzoj 十連測】[noip2016十連測第八場]Problem B: 降雷皇(最長上升子序列+線段樹|next數組)

來源:互聯網
上載者:User
Problem B: [noip2016十連測第八場]降雷皇 Time Limit: 10 Sec   Memory Limit: 512 MB
Submit: 39   Solved: 20
[ Submit][ Status][ Web Board] Description

降雷皇哈蒙很喜歡雷電,他想找到神奇的電光。哈蒙有n條導線排成一排,每條導線有一個電阻值,神奇的電光只能從一根導線傳到電阻比它大的上面,而且必須從左邊向右傳導,當然導線不必是連續的。哈蒙想知道電光最多能通過多少條導線,還想知道這樣的方案有多少。 Input 第一行兩個整數n和type。type表示資料類型
第二行n個整數表示電阻。
n<=100000,電阻值不超過100000 Output 第一行一個整數表示電光最多能通過多少條導線。
如果type=1則需要輸出第二行,表示方案數,對123456789模數。

Sample Input

5 11 3 2 5 4
Sample Output
34
HINT 【題解】【最長上升子序列+next數組(好伐,其實本應該是線段樹,然而資料辣麼弱。。。)】
【O(n log n)求最長上升子序列,用堆來搞】
#include<cstdio>#include<cstring>#include<algorithm>#define mod 123456789using namespace std;int f[100010],top,g[100010],ans[100010];int a[100010],nxt[100010],p[100010],num[100010],tot;int n,opt;inline void add(int x,int y,int val){    tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot; num[tot]=val;}inline void solve(int x,int val){    if(x==1) {add(x,val,1); ans[x]++; return;}    int cnt=0;    for(int i=p[x-1];i!=-1;i=nxt[i])     if(a[i]>=val) break;      else cnt+=num[i],cnt%=mod;    ans[x]+=cnt; ans[x]%=mod;    add(x,val,cnt%mod);}inline void ask(int nm,int len,int x){    int l=1,r=len,mid;    while(l<=r)     {        mid=(l+r)>>1;        if(f[mid]<x) l=mid+1;         else r=mid-1;     }    f[l]=x;     if(opt==1) g[nm]=l,solve(l,x);}int main(){    //freopen("hamon9.in","r",stdin);    int i,j;    memset(p,-1,sizeof(p));    memset(nxt,-1,sizeof(nxt));    scanf("%d%d",&n,&opt);    for(i=1;i<=n;++i)     {        int x;        scanf("%d",&x);        if(x>f[top])          {            f[++top]=x;            if(opt==1) g[i]=top,solve(top,x);          }          else ask(i,top,x);     }    if(opt==1) printf("%d\n%d\n",top,ans[top]%mod);    else printf("%d\n",top);    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.