BZOJ1800
-
题目
-
分析
采用断环成链的处理方式,枚举链的起点,再枚举三个断点 i , j , k i,j,k i,j,k ,满足 s u m [ i ] − s u m [ s − 1 ] = = s u m [ k ] − s u m [ j ] & & s [ j ] − s u m [ i ] = = s u m [ s + n − 1 ] − s u m [ k ] sum[i] - sum[s - 1] == sum[k] - sum[j] \&\& s[j] - sum[i] == sum[s + n - 1] - sum[k] sum[i]−sum[s−1]==sum[k]−sum[j]&&s[j]−sum[i]==sum[s+n−1]−sum[k] 即可。
注意计算是会重复处理矩形的四条边,所以最后答案要 a n s 4 \frac{ans}{4} 4ans 。
时间复杂度 : O ( n 4 ) O(n ^ 4) O(n4)
-
代码
int a[55]; int sum[55]; int main () { //freopen("input.in", "r", stdin); //freopen("test.out", "w", stdout); int n; read(n); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= n; i++) a[n + i] = a[i]; for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + a[i]; int ans = 0; for (int s = 1; s <= n; s++) { for (int i = s; i <= s + n - 1; i++) { for (int j = i + 1; j <= s + n - 1; j++) for (int k = j + 1; k <= s + n - 1; k ++) { if ((sum[i] - sum[s - 1] == sum[k] - sum[j]) && (sum[j] - sum[i] == sum[s + n - 1] - sum[k])) { ans++; } } } } cout << ans / 4 << endl; return 0 ; }
-
题型
前缀和 + 暴力