hihocoder #1114

题目链接:http://hihocoder.com/problemset/problem/1114

终于开始刷hihocoder了~

 1 /*
 2  * Problem: hihocoder #1114 
 3  * Author:  SHJWUDP
 4  * Created Time:  2015/4/2 星期四 14:29:53
 5  * File Name: 233.cpp
 6  * State: Accepted
 7  * Memo: function
 8  */
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13 
14 using namespace std;
15 
16 const int MaxA=1e5+7;
17 
18 int N;
19 int arr[MaxA];
20 int bomb[2][MaxA];
21 int opA[MaxA], opB[MaxA], la, lb;
22 bool func(int *b) {
23     for(int i=1; i<N; i++) {
24         b[i+1]=arr[i]-(b[i]+b[i-1]);
25         if(!(0<=b[i+1] && b[i+1]<=1)) return false;
26     }
27     return b[N]+b[N-1]==arr[N];
28 }
29 void solve() {
30     bomb[0][0]=bomb[1][0]=0;
31     bomb[0][1]=0;
32     bool flag1=func(bomb[0]);
33     bomb[1][1]=1;
34     bool flag2=func(bomb[1]);
35     la=0; lb=0;
36     if(flag1 && flag2) {
37         for(int i=1; i<=N; i++) {
38             if(bomb[0][i] == bomb[1][i]) {
39                 if(bomb[0][i]) opA[la++]=i;
40                 else opB[lb++]=i;
41             }
42         }
43     } else if(flag1 || flag2){
44         int *b=flag1?bomb[0]:bomb[1];
45         for(int i=1; i<=N; i++) {
46             if(b[i]) opA[la++]=i;
47             else opB[lb++]=i;
48         }
49     }
50 }
51 int main() {
52 #ifndef ONLINE_JUDGE
53     freopen("in", "r", stdin);
54     //freopen("out", "w", stdout);
55 #endif
56     int T;
57     scanf("%d", &T);
58     while(T--) {
59         scanf("%d", &N);
60         for(int i=1; i<=N; i++) {
61             scanf("%d", &arr[i]);
62         }
63     
64         solve();
65         printf("%d", la);
66         for(int i=0; i<la; i++)
67             printf(" %d", opA[i]);
68         printf("
%d", lb);
69         for(int i=0; i<lb; i++)
70             printf(" %d", opB[i]);
71         printf("
");
72     }
73     return 0;
74 }
hihocoder #1114
原文地址:https://www.cnblogs.com/shjwudp/p/4387306.html