Codeforces 754A Lesha and array splitting (搜索)

题目链接 Lesha and array splitting

设s[i][j]为序列i到j的和,当s[i][j]≠0时,即可从i跳到j+1.目标为从1跳到n+1,所以按照题意暴力即可。

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 
 6 #define rep(i,a,b)              for(int i(a); i <= (b); ++i)
 7 #define dec(i,a,b)              for(int i(a); i >= (b); --i)
 8 
 9 const int Q     =    1000        +       10;
10 
11 struct node{
12     int x, y;
13 } ans[Q];
14 int s[Q][Q];
15 int a[Q];
16 
17 bool flag = false;
18 int n;
19 int sum;
20 
21 void print(int x){
22     printf("YES\n%d\n", x);
23     rep(i, 1, x) printf("%d %d\n", ans[i].x, ans[i].y);
24 }
25 
26 void dfs(int x, int step){
27 
28     if (flag) return;
29     if (x == n + 1){
30         flag = true;
31         print(step - 1);
32         return;
33     }
34 
35     dec(i, n, x) if (s[x][i]){
36         ans[step].x = x, ans[step].y = i;
37         dfs(i + 1, step + 1);
38     }
39 }    
40 
41 int main(){
42 
43     scanf("%d", &n);
44     rep(i, 1, n) scanf("%d", a + i);
45     rep(i, 1, n){
46         sum = 0;
47         rep(j, i, n){
48             sum += a[j];
49             s[i][j] = sum;
50         }
51     }
52 
53     dfs(1, 1);
54     if (!flag) puts("NO");
55     
56     return 0;
57 
58 }

 


posted @ 2017-01-26 23:39  cxhscst2  阅读(247)  评论(0编辑  收藏  举报