Link: D. Population Size
Some numbers must be divided into blocks so that each segment is an equal-difference series, but some numbers are-1, which indicates any number (but must be greater than 0). A question must be divided into at least a few digits.
Idea: greedy. Each time two adjacent numbers are found, and the number of-1 before the first number is recorded, the tolerances can be determined, and then the tolerance can be used to determine whether the previous-1 can be filled in, if it cannot be ans, it will be 1 more, and then it will start from the second definite digit position. If it can, it will use the tolerances to locate the last position that can be placed, and then start from that position next time.
There are many details, and the code is frustrated:
#include
#include
const int N = 200005;__int64 n, i, j;__int64 a[N];int main() {__int64 ans = 0;scanf("%I64d", &n);for (i = 0; i < n; i++)scanf("%I64d", &a[i]);i = 0;__int64 s1 = 0;while (i < n) {s1 = 0;while (a[i] == -1 && i < n) {s1++;i++;}__int64 s = i;if (i < n)i++;while (a[i] == -1 && i < n) {i++;}if (i == n) {ans++;break;}__int64 e = i, d;if ((a[e] - a[s]) % (e - s) == 0) {d = (a[e] - a[s]) / (e - s);}else {ans++;continue;}if (d > 0 && s1 > (a[s] - 1) / d) {ans++;i = e;continue;}__int64 sum = a[s];for (j = s + 1; j < n; j++) {sum += d;if (sum < 1) {i = j;ans++;break;}if (a[j] != -1 && sum != a[j]) {i = j;ans++;break;}}if (j == n) {i = n;ans++;}}printf("%I64d\n", ans);return 0;}