把所有连续区间子集 修改为回文串需要修改的数的数量。
这个是有别于官解的另一种计数角度。
实际上也是类似数组点对问题。 经典手法是枚举右端点 然后计算对应的左端点的个数。是从左往右枚举的。
这里是有内往外枚举。。枚举外面的端点 然后计算对应的内部符合的端点的个数。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128_t
#define ar array<int, 2>
#define arr array<int, 3>
int n, m, k, inf = 1LL << 61, mod = 998244353;// 1e9+7;
const int N = 5e5 + 50;
int a[N], cnt[N];
void solve() {
cin >> n;
vector<ar>c;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
c.push_back({min(i, n + 1 - i), a[i]});
}
sort(c.begin(), c.end(), greater());
// 这个实际上就是从中间往两边。扩展。。
// 谁距离边界短 就谁来算?
int p = 0, ans = 0;
for (auto[i, x] : c) {
int t = p - cnt[x];
ans += t * i;
cnt[x]++;
p++;
}
cout << ans;
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
#ifdef DEBUG
freopen("../1.in", "r", stdin);
#endif
//init_f();
//init();
//expr();
// int T; cin >> T; while(T--)
solve();
return 0;
}