思路:先把最左和最右的干掉,剩下的再一起dfs。dfs的时候注意当第cur-1个死了之后(而第cur个不要求必须死)才向后面搜索第cur+1个。
当前的第cur个可以被扔在自己身上的火球,也可被扔在第cur+1个的火球干掉。
1 #include <iostream> 2 #include <queue> 3 #include <stack> 4 #include <cstdio> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <bitset> 9 #include <algorithm> 10 #include <cmath> 11 #include <cstring> 12 #include <cstdlib> 13 #include <string> 14 #include <sstream> 15 #include <time.h> 16 #define x first 17 #define y second 18 #define pb push_back 19 #define mp make_pair 20 #define lson l,m,rt*2 21 #define rson m+1,r,rt*2+1 22 #define mt(A,B) memset(A,B,sizeof(A)) 23 #define mod 1000000007 24 using namespace std; 25 typedef long long LL; 26 const int N=2e5+10; 27 const int inf = 0x3f3f3f3f; 28 const LL INF=0x3f3f3f3f3f3f3f3fLL; 29 int x[N]; 30 int n,a,b,ans,res_2=inf; 31 32 33 void dfs(int cur,int num) 34 { 35 int p=0,q; 36 if(cur==n-1) 37 { 38 ans=min(ans,num); 39 return; 40 } 41 if(x[cur-1]<0)dfs(cur+1,num); 42 43 if(x[cur-1]>=0) 44 { 45 p=x[cur-1]/b+1; 46 x[cur-1]-=p*b; 47 x[cur]-=p*a; 48 x[cur+1]-=p*b; 49 dfs(cur+1,num+p); 50 x[cur-1]+=p*b; 51 x[cur]+=p*a; 52 x[cur+1]+=p*b; 53 } 54 q=x[cur]/a+1; 55 if(x[cur]>=0&&q>p) 56 { 57 for(int i=p+1;i<=q;i++) 58 { 59 x[cur-1]-=i*b; 60 x[cur]-=i*a; 61 x[cur+1]-=i*b; 62 dfs(cur+1,num+i); 63 x[cur]+=i*a; 64 x[cur+1]+=i*b; 65 x[cur-1]+=i*b; 66 } 67 } 68 } 69 int main() 70 { 71 #ifdef Local 72 freopen("data.txt","r",stdin); 73 #endif 74 int p=0,q=0; 75 cin>>n>>a>>b; 76 ans=inf; 77 for(int i=0; i<n; i++)scanf("%d",&x[i]); 78 p=x[0]/b+1; 79 x[0]-=p*b;; 80 x[1]-=p*a; 81 x[2]-=p*b; 82 if(x[n-1]>=0) 83 { 84 q=x[n-1]/b+1; 85 x[n-2]-=q*a; 86 x[n-3]-=q*b; 87 x[n-1]-=q*b; 88 } 89 dfs(1,0); 90 if(ans==inf)ans=0; 91 cout<<ans+p+q<<endl; 92 #ifdef Local 93 cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl; 94 #endif 95 }