bzoj 5449: 序列

來源:互聯網
上載者:User

標籤:const   bzoj   close   can   test   max   spl   因此   problem   

https://www.lydsy.com/JudgeOnline/problem.php?id=5449

話說很早以前做過。。算是IDA*的板子吧,一個簡單的估價函數就可以過去了

 1 %:pragma GCC optimize(2) 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 int a[50],n,maxd,T; 6 bool fl; 7 inline bool check() 8 { 9     for(int i=1;i<n;++i)10         if(a[i]>a[i+1])11             return 0;12     return 1;13 }14 //reverse一次,相鄰兩數絕對值之差大於1的最多減少1個,15 //因此相鄰兩數絕對值之差大於1的個數一定小於等於所需reverse次數16 inline int hh()17 {18     int cnt=0;19     for(int i=1;i<n;++i)20         if(abs(a[i+1]-a[i])>1)21             cnt++;22     return cnt;23 }24 inline void reverse1(const int& r)25 {26     int i,t;27     for(i=1;i<=r/2;++i)28     {29         t=a[i];a[i]=a[r-i+1];a[r-i+1]=t;30     }31 }32 void dfs(int x,int last)33 {34     if(fl)  return;35     if(x==maxd)36     {37         if(check()) fl=1;38         return;39     }40     if(x+hh()>maxd)  return;41     for(int i=2;i<=n;++i)42         if(i!=last)43         {44             reverse(a+1,a+i+1);45             dfs(x+1,i);46             reverse(a+1,a+i+1);47         }48 }49 int main()50 {51     int i;52     scanf("%d",&T);53     while(T--)54     {55         fl=0;56         scanf("%d",&n);57         for(i=1;i<=n;++i)58             scanf("%d",&a[i]);59         for(maxd=0;maxd<=30;++maxd)60         {61             dfs(0,0);62             if(fl)  break;63         }64         printf("%d\n",maxd);65     }66     return 0;67 }
View Code

此題http://210.33.19.103/contest/823/problem/2(序列)同以上題

bzoj 5449: 序列

聯繫我們

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