SRM 653 CountryGroupHard

SRM 653 CountryGroupHard

题意:

  n 个人坐成一排,同一个国家的人坐在一起,记者问了一些人他们的国家有多少人,他们都回答了正确的信息。已知m条信息,每条信息形如第 pi 个位置上的人的国家有 ai 个人,求已知了这些信息能否推断出所有人所属国家的情况。

分析:

  f[i]表示到第i个位置,方案数是多少。然后枚举后面的多少人属于一个国家的。当f[i]>2的时候,设为2就行。初始化f[n]=1。

代码:

 1 /*
 2 * @Author: mjt
 3 * @Date:   2018-10-17 20:55:42
 4 * @Last Modified by:   mjt
 5 * @Last Modified time: 2018-10-17 21:37:10
 6 */
 7 #include<cstdio>
 8 #include<algorithm>
 9 #include<cstring>
10 #include<cmath>
11 #include<iostream>
12 #include<cctype>
13 #include<set>
14 #include<vector>
15 #include<queue>
16 #include<map>
17 #define fi(s) freopen(s,"r",stdin);
18 #define fo(s) freopen(s,"w",stdout);
19 using namespace std;
20 typedef long long LL;
21 
22 inline int read() {
23     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
24     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
25 }
26 
27 class CountryGroupHard{
28 public:
29     int f[105];
30     string solve(vector <int> a) {
31         int n = a.size();
32         f[n] = 1;
33         for (int i = n - 1; i >= 0; --i) {
34             if (a[i] == 0) {
35                 int ok = -1;
36                 for (int j = 1; (i + j) <= n; ++j) {
37                     if (a[i + j - 1] != 0) { // 最近的有数的位置
38                         if (ok != -1 && ok != a[i + j - 1]) break;
39                         else ok = a[i + j - 1];
40                     }
41                     if (ok == -1 || ok == j) f[i] += f[i + j];
42                 }
43             }
44             else {
45                 bool ok = (i + a[i] > n); // 要求后面的a[i]个相等
46                 for (int j = i; j < n && j < i + a[i]; ++j) 
47                     if (a[j] && a[j] != a[i]) ok = true;
48                 if (!ok) f[i] = f[i + a[i]];
49             }
50             if (f[i] >= 2) f[i] = 2;
51         }
52         return f[0] > 1 ? "Insufficient" : "Sufficient";
53     }
54 
55 };
原文地址:https://www.cnblogs.com/mjtcn/p/9807337.html