跳到正文

题解

题解:AT_abc417_c [ABC417C] Distance Indicators

weifengzhaomi约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月28日
篇幅
约 1 分钟

版本

  1. v1当前

C 题什么时候那么水了…

思路

首先,题目要我们求满足以下两个条件的数对。

1≤i<j≤n1 \le i < j \le n j−i=aj+aij - i = a_j + a_i

我们把第二个式子转化一下:

j−aj=i+aij - a_j = i + a_i

那么,题目就变成了:有多少个 i,ji,j 满足 j−aj=i+aij - a_j = i + a_i。

注意,这里的 i,ji,j 是不分先后顺序的。

为什么呢,因为,我们假设有一组 x,yx,y 满足 x−ax=y+ayx - a_x = y + a_y ,那么,不可能 y−ay=x+axy - a_y = x + a_x 是合法的。

所以,题目的第一个条件不用考虑,所以,先求出所有 i−aii - a_i 的数量,在加上所有 i+aii + a_i 的个数。

剩下的就是注意爆 int。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
unordered_map<int,int> mp;
int n,a[200010],ans;
signed main(){
	cin >> n;
	for (int i = 1;i <= n;i++) scanf("%lld",&a[i]),mp[i - a[i]]++;
	for (int i = 1;i <= n;i++) ans += mp[i + a[i]];
	cout << ans << endl;
}
原文