#P16572. [USACO26OPEN] Arranging Cows

[USACO26OPEN] Arranging Cows

题目描述

You are given a length-NN bitstring s1Ns_1 \ldots N (2N1092 \leq N \leq 10^9). In one operation, you can reverse a range slrs_l \ldots r if the following conditions are true:

  1. The size of the range is even.
  2. The first half of the range consists of one character (either 00 or 11), and the second half contains the opposite character
  3. Either l=1l = 1 or sl1sls_{l-1} \neq s_l
  4. Either r=Nr = N or sr+1srs_{r+1} \neq s_r

Find the minimum number of operations to move all of the 11s to the front, or report that it is impossible. If it is possible to do so, also output the number of sequences of operations achieving this minimum, modulo 109+710^9 + 7.

输入格式

The first line contains TT (1T20261 \leq T \leq 2026), the number of independent tests. Each test is specified in the following format:

The bitstring is given in a compressed format. The first line contains RR, the number of runs in the string (2R8002 \leq R \leq 800), and the first character of the string (either 00 or 11).

The next line contains RR space-separated integers l1,l2,l3,lRl_1, l_2, l_3, \ldots l_R (0<li<1090 < l_i < 10^9), the lengths of maximal consecutive blocks of equal characters in ss. It’s guaranteed that N=i=1Rli109N = \sum_{i=1}^{R} l_i \leq 10^9.

Additionally, it is guaranteed that the sum of R2R^2 over all tests does not exceed 1.51061.5 \cdot 10^6.

输出格式

For each test case, print the minimum number of operations to move all of the 11s to the front or 1-1 if it is impossible, as well as the number of sequences of operations achieving this minimum modulo 109+710^9 + 7.

9
2 0
1 1
2 1
1 1
2 1
2 1
2 0
1 2
5 0
1 1 1 2 1
3 0
1 2 1
8 0
1 1 2 1 1 2 1 1
6 0
3 3 1 2 2 1
7 0
5 1 1 3 2 1 1
1 1
0 1
0 1
-1 0
2 1
-1 0
4 7
3 1
4 1
5
2 1
1 1
4 1
1 1 1 1
6 1
1 1 1 1 1 1
8 1
1 1 1 1 1 1 1 1
10 1
1 1 1 1 1 1 1 1 1 1
0 1
1 1
2 1
3 3
4 9

提示

Sample 1

Here is the sequence of two operations for the fifth testcase: 010110100110111000010110 \to 100110 \to 111000.

Sample 2

In all of these test cases, the minimum number of operations equals R/21R/2-1.

Here are all three possible sequences of three operations for the fourth test case:

(1)
   10101010
-> 11001010
-> 11001100
-> 11110000

(2)
   10101010
-> 10110010
-> 10001110
-> 11110000

(3)
   10101010
-> 10101100
-> 11001100
-> 11110000

SCORING

  • Input 33: N10N \le 10, all tests are distinct
  • Input 44: R10R \le 10
  • Inputs 55-88: R100R \le 100, the sum of R2R^2 over all tests does not exceed 10510^5, the minimum number of operations is guaranteed to equal R/21R/2 - 1.
  • Inputs 99-1212: R100R \le 100, the sum of R2R^2 over all tests does not exceed 10510^5.
  • Inputs 1313-1616: No additional constraints.