[ABC290E] Make it Palindrome 回文修改计数:本质也是两点问题

把所有连续区间子集 修改为回文串需要修改的数的数量。
这个是有别于官解的另一种计数角度。
实际上也是类似数组点对问题。 经典手法是枚举右端点 然后计算对应的左端点的个数。是从左往右枚举的。
这里是有内往外枚举。。枚举外面的端点 然后计算对应的内部符合的端点的个数。

#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值