PAT Basic 1083 whether there is a difference equal to (20) [hash map, map STL]

topic

Given N cards still pictures, each front-on write 1,2, ......, N, then all turned ⾯, shuffle, are written on the BACK 1,2, ......, N. The pros and cons of each card ⾯ digital subtraction (Decreases the zoomed), the N comes in handy to give a negative difference, wherein the difference of whether there is an equal?
START input format:
input START ⼀ first frame ⾏ given positive integer N (2 <= N <= 10000), followed by ⼀ ⾏ arranged after ⼀ given a shuffle 1 to N, the number i represents a front-on of wrote a digital still pictures that card i the bACK of.
Output Format:
according to "the difference between the number of repetitions" format to ⼩ zoomed from the output difference and repeating the number of repetitions, each output ⼀ ⾏ results.
Sample input START:
. 8
. 3. 4. 5. 1 2. 8. 6. 7
Output Sample:
. 5 2
. 3. 3
2 2

Topic analysis

  1. A second input line, start index from 1 to N end - front of the card number, corresponding to the location of the digital - digital back of the card (Subject: i represents the number of writing the digital front-on that card i of the still pictures BACK)
  2. When entering statistical difference in the number of occurrences statistics complete entry is complete

map

  • Defined map, key as a difference absolute value, value of the difference between the number of occurrences
  • Reverse key value of the output of the map (map default key ascending), if the value <= 1 is skipped (Subject known: the difference was repeated, not repeated 1 time)

Array

  • Definition array dfs
    • Size is set to 10,000 (Subject known: 1 <= N <= 10000), a maximum difference of 10000-1;
    • The subscript is the difference (subscript 0 discarding not used);
    • Value of the recording elements corresponding to the number of index difference appears;
  • Reverse output array (element value <= 1 is skipped because it is not repeated difference)

Error-prone point

  1. The second input line index start counting from 1 (subscript> = 1 && index <= N)
  2. 1 is not repeated

Knowledge Point

map reverse output
for(map<int,int>::reverse_iterator it=m.rbegin(); it!=m.rend(); it++) {}

Code

Code 01(map)

#include <iostream>
#include <map>
#include <cmath>
using namespace std;
int main(int argc, char * argv[]) {
    int N,g;
    scanf("%d",&N);
    map<int,int> m;
    for(int i=1; i<=N; i++) {
        scanf("%d",&g);
        m[abs(g-i)]++;
    }
    for(map<int,int>::reverse_iterator it=m.rbegin(); it!=m.rend(); it++) {
        if(it->second>1)printf("%d %d\n",it->first,it->second);
    }
    return 0;
}

Code 02 (array)

#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char * argv[]) {
    int N,g;
    scanf("%d",&N);
    int dfs[10000]={0};
    for(int i=1; i<=N; i++) {
        scanf("%d",&g);
        dfs[abs(g-i)]++;
    }
    for(int i=10000-1;i>=0;i--){
        if(dfs[i]>1)printf("%d %d\n",i,dfs[i]);
    }
    return 0;
}


Guess you like

Origin www.cnblogs.com/houzm/p/12238287.html