Codeforces 283B Cow Program

來源:互聯網
上載者:User

標籤:

題意:給你a[2] - a[n], 一個初始x = 1,初始y = 0  ,執行下面步驟

1) x +=a[x] ,y += a[x]

2) x -= a[x] ,y += a[x]

3)重複1-2步驟。只要中間x <=0 || x > n  就跳出。輸出y的值

問你a[1] 從 [1,n-1]分別的值為多少。

解題思路:dfs 求 dp[i][0/1]  求以加法進入 i 和以減法進入 i 能得到的值。

解題代碼:

  1 // File Name: 283b.cpp  2 // Author: darkdream  3 // Created Time: 2015年03月12日 星期四 10時59分22秒  4   5 #include<vector>  6 #include<list>  7 #include<map>  8 #include<set>  9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25  26 using namespace std; 27 int a[200005]; 28 int vis[200005][2]; 29 LL dp[200005][2]; 30 int n ; 31 void dfs(int k ,int t,int color) 32 { 33      if(t == 1) 34      { 35         dp[k][t] = a[k]; 36         if(k +a[k] > n) 37         { 38            return ; 39         } 40         if(vis[k + a[k]][t^1] != 0  ) 41         { 42             if(vis[k+a[k]][t^1] == color) 43                 dp[k][t] = -1; 44             else{ 45                 if(dp[k+a[k]][t^1] == -1) 46                     dp[k][t] = -1; 47                 else dp[k][t] += dp[k+a[k]][t^1]; 48             } 49             return ; 50         } 51         vis[k+a[k]][t^1] = color;  52         dfs(k+a[k],0,color); 53         if(dp[k+a[k]][t^1] == -1) 54             dp[k][t] = -1; 55         else dp[k][t] += dp[k+a[k]][t^1]; 56      }else if(t == 0 ){ 57           dp[k][t] = a[k]; 58         if(k - a[k] <= 0) 59         { 60            return ; 61         } 62         if(k - a[k] ==  1) 63         { 64            dp[k][t] = -1; 65            return ; 66         } 67         if(vis[k - a[k]][t^1] != 0  ) 68         { 69             if(vis[k-a[k]][t^1] == color) 70                 dp[k][t] = -1; 71             else{ 72                 if(dp[k-a[k]][t^1] == -1) 73                     dp[k][t] = -1; 74                 else dp[k][t] += dp[k-a[k]][t^1]; 75             } 76             return ; 77          78         } 79         vis[k-a[k]][t^1] = color;  80         dfs(k-a[k],1,color); 81         if(dp[k-a[k]][t^1] == -1) 82             dp[k][t] = -1; 83         else dp[k][t] += dp[k-a[k]][t^1]; 84      } 85 } 86 int main(){ 87     scanf("%d",&n); 88     for(int i = 2 ; i <= n;i ++) 89     { 90            scanf("%d",&a[i]);        91     } 92     for(int i = 2;i <= n;i ++) 93     { 94          if(vis[i][0] == 0) 95          { 96             vis[i][0] = i;  97             dfs(i,0,i); 98          } 99          if(vis[i][1] == 0 )100          {101             vis[i][1] = i; 102             dfs(i,1,i);103          }104     }105     for(int i = 1;i < n;i ++)106     {107         if(1 + i > n)108         {109             printf("%d\n",i);110             continue;111         }else{112             if(dp[1+i][0] != -1)113             printf("%I64d\n",i+dp[1+i][0]);    114             else printf("-1\n");115         }116     }117     118 return 0;119 }
View Code

 

Codeforces 283B Cow Program

聯繫我們

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