给定 N 张卡片,正面分别写上 1、2、……、N,然后全部翻面,洗牌,在背面分别写上 1、2、……、N。将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差?
输入格式:
输入第一行给出一个正整数 N(2 ≤ N ≤ 10 000),随后一行给出 1 到 N 的一个洗牌后的排列,第 i 个数表示正面写了 i 的那张卡片背面的数字。
输出格式:
按照“差值 重复次数”的格式从大到小输出重复的差值及其重复的次数,每行输出一个结果。
输入样例:
8
3 5 8 6 2 1 4 7
输出样例:
5 2
3 3
2 2
AC代码:
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
int n;
cin >> n;
vector<int> nums;
for (int i = 0; i < n; i ++ ) {
int num;
cin >> num;
nums.push_back(num);
}
unordered_map<int, int> hash;
vector<int> negatives;
for (int i = 0; i < n; i ++ ) {
int negative = abs((i + 1) - nums[i]);
if (hash.count(negative) == 0) {
hash.insert({negative, 1});
negatives.push_back(negative);
} else {
hash[negative] ++ ;
}
}
sort(negatives.begin(), negatives.end(), cmp);
for (int i = 0; i < negatives.size(); i ++ ) {
if (hash[negatives[i]] != 1) {
cout << negatives[i] << " " << hash[negatives[i]] << endl;
}
}
return 0;
}