voidsolve(){ int n; std::cin >> n; std::vector<std::string> s(n); for (int i = 0; i < n; i++) { std::cin >> s[i]; } int ans = 0; for (int i = 0; i < (n + 1) / 2; i++) { for (int j = 0; j < n / 2; j++) { int sum = 0; int A = s[i][j] - '0'; int B = s[n - j - 1][i] - '0'; int C = s[j][n - i - 1] - '0'; int D = s[n - i - 1][n - j - 1] - '0'; sum += A + B + C + D; ans += std::min(sum, 4 - sum); } } std::cout << ans << "\n"; }
F.
Yet Another Problem About Pairs Satisfying an Inequality
思路:
这个公式是单调的,所以我们直接边插入边二分求个数,然后累加即可
时间复杂度:\(O(nlogn)\)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
voidsolve(){ int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } i64 ans = 0; std::vector<int> v; for (int i = 0; i < n; i++) { if (a[i] < i + 1) { ans += std::lower_bound(v.begin(), v.end(), a[i]) - v.begin(); v.push_back(i + 1); } } std::cout << ans << "\n"; }
std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; }
i64 ans = 0, sum = 0; for (int i = -1; i < n; i++) { i64 now = sum; for (int j = i + 1; j < std::min(n, i + 32); j++) { // 如果后面全部使用 bad key int copy = a[j]; copy >>= j - i; now += copy; } ans = std::max(ans, now); if (i + 1 != n) { sum += a[i + 1] - k; // 当前使用 good key } } std::cout << ans << "\n"; }