PAT Basic 1083 whether there is a difference equal to (20 minutes)

Given N cards, are written on the front 1,2, ......, N, and all upside down, shuffle, are written on the rear surface 1,2, ......, N. The both sides of each card digital subtraction (large decrease), to give the N non-negative difference, wherein the difference of whether there is an equal?

Input formats:

Input of the first row is given a positive integer N (2  ≤ N  ≤ 10 000), followed by the arrangement of a given row shuffle 1 to N, i-th write a front piece of a rear surface of the digital card i .

Output formats:

According to "the difference between the number of repetitions" format and a descending output difference repeat number of repetitions, it outputs a result of each line.

Sample input:

8
3 5 8 6 2 1 4 7

Sample output:

5 2
3 3
2 2



#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main(){
    int n,tmp;
    cin>>n;
    vector<int> v(n+1,0);
    for(int i=0;i<n;i++){
        cin>>tmp;
        v[abs(tmp-i-1)]++;
    }
    for(int i=n;i>=0;i--)
        if(v[i]>1) cout<<i<<" "<<v[i]<<endl;
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11618964.html