【類比】CSU 1807 最長上升子序列~ (2016湖南省第十二屆大學生電腦程式設計競賽)

來源:互聯網
上載者:User

標籤:

題目連結:

  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807

題目大意:

  給你一個長度為N(N<=105)的數列,數列中的0可以被其他數字替換,最終形成一個1~N的排列,求這個排列的最長上升子序列長度為N-1的方案數。

題目思路:

  【類比】

  這道題需要分類討論。

  首先可以肯定,一個長度為n的序列最長上升子序列長度為n-1(最長下降子序列長度為2),那麼這個序列的樣子是1~n從小到大排列後其中一個數字挪到其餘數字中間(錯位)

  一個長度為L的0區間,考慮第i個數字,如果放在第i位上,則f[i]=f[i-1]

  如果放在第i-1位上,則第i位有i-1种放法(前面i-1個數都可以放在第i位,此時錯位的為第i位上的數)

  如果放在其他位置,則這個數就是錯位的數,那這個數可以放的位置有i-2個(其他的數必須按從小到大排列才能保證錯位元為1個)

  

  現在考慮給定的數字,如果某個數字a[i]與i的位移量超過1,那麼意味著它前面必然有至少2個比它大的(a[i]-i>1),或它後面有至少2個比它小的(a[i]-i<-1),那麼當前的a[i]必然是唯一的最終答案裡位移的數,或者無解。

  如果給定的數字沒有位移,那麼那個位移的數字一定在只含0的區間中的一個,那麼ans=Σ F[l] (F[l]即為長度為l的0區間位移量為1的方案數)

  如果區間[L,R]裡給定的數字都往前位移了1,那麼代表有一個小於L的數在R的後面(如果區間[L,R]裡給定的數字都往後位移了1,代表一個大於R的數在L前面),並且根據前面判斷得出這個數字還未被填(位移量超過1),左端小於L的能填的數=L左端到第一個a[i]=i之間的0數(l[L]),能夠填的位置就是R右端到第一個a[i]=i之間的0數(r[R]),所以ans=l[L]*r[R]。(另一種情況同理)

  還有一種情況是某兩個數字交換位置,這個我一開始考慮漏了。兩位元字交換隻有在這兩個數字相鄰的時候才可行且只有唯一解,並且要求其他數字必須要從小到大排列不能再有錯位。

 

  1 //  2 //by coolxxx  3 //#include<bits/stdc++.h>  4 #include<iostream>  5 #include<algorithm>  6 #include<string>  7 #include<iomanip>  8 #include<map>  9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #define min(a,b) ((a)<(b)?(a):(b)) 21 #define max(a,b) ((a)>(b)?(a):(b)) 22 #define abs(a) ((a)>0?(a):(-(a))) 23 #define lowbit(a) (a&(-a)) 24 #define sqr(a) ((a)*(a)) 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define eps (1e-10) 28 #define J 10000 29 #define mod 1000000007 30 #define MAX 0x7f7f7f7f 31 #define PI 3.14159265358979323 32 #pragma comment(linker,"/STACK:1024000000,1024000000") 33 #define N 100004 34 using namespace std; 35 typedef long long LL; 36 double anss; 37 LL aans; 38 int cas,cass; 39 int n,m,lll,ans; 40 int a[N]; 41 LL f[N]; 42 void init() 43 { 44     int i; 45     f[0]=f[1]=0; 46     for(i=2;i<N;i++)f[i]=f[i-1]+i-1+i-2; 47 } 48 void work1(int x) 49 { 50     int i,t=1; 51     for(i=1;i<=n;i++) 52     { 53         if(i==x)continue; 54         if(t==a[x])t++; 55         if(t==a[i] || !a[i])t++; 56         else break; 57     } 58     if(i<=n)puts("0"); 59     else puts("1"); 60 } 61 void work2(int l,int r) 62 { 63     int i,x,ll=0,rr=0; 64     for(i=l;i<=r;i++) 65     { 66         if(!a[i])continue; 67         if(a[i]-i!=a[l]-l)break; 68     } 69     if(i<=r){puts("0");return;} 70     for(i=l-1;i;i--) 71     { 72         if(!a[i])ll++; 73         else if(a[i]-i!=a[l]-l)break; 74     } 75     for(i=r+1;i<=n;i++) 76     { 77         if(!a[i])rr++; 78         else if(a[i]-i!=a[l]-l)break; 79     } 80     if(a[l]-l==1)ll++; 81     else rr++; 82     printf("%lld\n",1LL*ll*rr); 83 } 84 void work3(int x,int y,int l,int r) 85 { 86     int i; 87     if(x!=y || l!=r || x!=l-1){puts("0");return;} 88     puts("1"); 89 } 90 void work4() 91 { 92     int i,sz=0; 93     for(i=1;i<=n;i++) 94     { 95         if(!a[i])sz++; 96         else aans+=f[sz],sz=0; 97     } 98     if(sz)aans+=f[sz]; 99     printf("%lld\n",aans);100 }101 int main()102 {103     #ifndef ONLINE_JUDGE104 //    freopen("1.txt","r",stdin);105 //    freopen("2.txt","w",stdout);106     #endif107     int i,j,k;108     int x,y,l,r;109     init();110 //    for(scanf("%d",&cass);cass;cass--)111 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)112 //    while(~scanf("%s",s))113     while(~scanf("%d",&n))114     {115         aans=0;116         x=l=n+1,y=r=0;117         for(i=1;i<=n;i++)scanf("%d",a+i);118         for(i=1;i<=n;i++)119         {120             if(!a[i])continue;121             if(abs(a[i]-i)>1){work1(i);break;}122             if(a[i]-i==1)x=min(x,i),y=max(y,i);123             if(a[i]-i==-1)l=min(l,i),r=max(y,i);124         }125         if(i<=n)continue;126         if(x<=n && y>0 && !r)work2(x,y);127         else if(l<=n && r>0 && !y)work2(l,r);128         else if(y && r)work3(x,y,l,r);129         else work4();130     }131     return 0;132 }133 /*134 //135 136 //137 */
View Code

 

【類比】CSU 1807 最長上升子序列~ (2016湖南省第十二屆大學生電腦程式設計競賽)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.