1. 程式人生 > >PAT 1083

PAT 1083

1083 是否存在相等的差 (20 分)

給定 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
#include <cstdio>
using namespace std;
const int maxn = 10010;
int arr[maxn];
int main()
{
    int N;
    scanf("%d", &N);
    int temp;
    for(int i=0; i<N; i++)
    {
        scanf("%d", &temp);
        temp = i+1 - temp;
        if(temp<0) temp = -temp;
        arr[temp]++;
    }
    for(int i=maxn-1; i>=0; i--)
    {
        if(arr[i]>=2)
        {
            printf("%d %d\n", i, arr[i]);
        }
    }
    return 0;
}