码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 5449: 序列

时间:2018-10-23 22:59:59      阅读:264      评论:0      收藏:0      [点我收藏+]

标签: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: 序列

标签:const   bzoj   close   can   test   max   spl   因此   problem   

原文地址:https://www.cnblogs.com/hehe54321/p/9839540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!