解
第一次想到的是前缀和,维护每一次梳果之后的个数,虽然正确,但最后发现没有很大的必要。
围成一个圈自然是和取余有关啦。
//
// Created by 29273 on 2021-04-06.
//
#include "bits/stdc++.h"
using namespace std;
const int maxN = 1e3 + 10;
struct node {
int flag;
int num;
int pre;
};
int T, D, E, N, n;
node a[maxN][maxN];
int t[maxN], d[maxN], rel[maxN];
int main() {
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> t[i];
for (int j = 1; j <= t[i]; ++j) {
cin >> n;
a[i][j].num = n;
a[i][j].flag = n > 0;
a[i][j].pre = j == 1 ? n : (n > 0 ? a[i][j - 1].pre : a[i][j - 1].pre + n);
rel[i] += n;
if (n > 0) {
rel[i] = n;
}
}
}
for (int i = 1; i <= N; ++i) {
T += rel[i];
for (int j = 1; j <= t[i]; ++j)
if (a[i][j].flag && a[i][j].pre != a[i][j].num) {
d[i] = 1;
D++;
break;
}
}
for (int i = 0; i < N && N >= 3; ++i)
E += d[(i - 1 + N) % N + 1] && d[(i + 1 + N) % N + 1] && d[(i + N) % N + 1];
cout << T << " " << D << " " << E << endl;
return 0;
}
更简便的解
维护一个drop数组,根据题意只需要知道每一棵树是否掉落过,只需要在输入的时候进行简单判断即可,所以前缀和的做法大材小用。
//
// Created by 29273 on 2021-04-07.
//
#include "bits/stdc++.h"
using namespace std;
const int maxN = 1E3 + 5;
int N, T, D, E, M, a, b;
int drop[maxN];
int main() {
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> M >> a;
for (int j = 0; j < M - 1; ++j) {
cin >> b;
if (b > 0) {
if (a != b) {
drop[i] = 1;
a = b;
}
} else
a += b;
}
T += a;
D += drop[i];
}
for (int i = 0; i < N; ++i)
E += drop[(i + N) % N] && drop[(i - 1 + N) % N] && drop[(i + 1 + N) % N];
cout << T << " " << D << " " << E << endl;
return 0;
}