1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Brute force.  No other idea :(

#include <cstdio>
#include <vector>

using namespace std;

const int kModulus = 1000000007;

void AddTo(const long long a, int *b) {
  *b = (a + *b) % kModulus;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> c(n), d(n);
  for (int i = 0; i < n; ++i) scanf("%d%d", &c[i], &d[i]);
  vector<int> best(n + 1, -1);
  vector<int> teams(n + 1, -1);
  vector<int> ways(n + 1, 0);
  best[0] = 0;
  teams[0] = 0;
  ways[0] = 1;
  for (int j = 1; j <= n; ++j) {
    best[j] = best[j - 1];
    int cc = 0;
    int dd = n;
    for (int i = j - 1; i >= 0; --i) {
      cc = max(cc, c[i]);
      dd = min(dd, d[i]);
      if (j - i > dd) break;
      if (cc > dd) break;
      if (best[i] + 1 < teams[j]) break;
      if (teams[i] < 0) continue;
      if (j - i >= cc) {
        if (teams[i] + 1 > teams[j]) {
          teams[j] = teams[i] + 1;
          ways[j] = 0;
          best[j] = max(best[j], teams[j]);
        }
        if (teams[i] + 1 == teams[j]) AddTo(ways[i], &ways[j]);
      }
    }
  }
  if (teams[n] == -1) printf("NIE\n"); else printf("%d %d\n", teams[n], ways[n]);
  return 0;
}