Title BACKGROUND WARNING: there may strengthen data subject description obtained when a natural number nn research survey times, each number of not more than 1.5 billion ( for 1.5 \ Times 10 ^ . 9 ) 1.5 billion ( for 1.5 × 10 . 9 ). It is known not to the same number of no more than 1.00001 billion, and now need to count the number of times each occurrence of these natural number, and follow the natural order of the number of small to large output statistics. Input Output Format Input Format: co-n- + 1N + . 1 line. The first row is an integer nn, indicates the number of natural numbers; 22 to n- + 1N + . 1 a natural number per line. Output format: total row mm (mm is the number of different numbers nn natural numbers), in accordance with a natural number sequentially output from small to large. Each output line 22 integers, respectively, and is a natural number times the number of occurrences of, separated by a space therebetween. Sample Input Output Input Sample # 1 : . 8 2 . 4 2 . 4 . 5 100 2 100 Output Sample # 1 : 2 . 3 . 4 2 . 5 1 100 2 explained 40 % of the data satisfies: 1 ≤n≤ 1000 80 % of the data satisfies: 1 ≤n≤ 50000 100 % data satisfies: 1 ≤n≤ 200000 , each number not more than 1.5 billion ( for 1.5 × 109 )
First of all, pay attention to this question of the scope of data
Secondly, personally think that this is the most appropriate solution to a problem Meng new
look with a map or other heavyweights are set such strange things, personally think that a one-dimensional array can be completely out of this AC questions
I feel that my code should be considered a fast took 321ms 1448KB
First with a fast row
Then the following process (explained in the core code in a ~ QAQ ~)
for ( int i = . 1 ; i <= n-; i ++ ) { int SUM1 = 0 ; // see if there are several numbers are the same for ( int J = i; J <= n-; J ++) // from i have been Get { IF (S [J] == S [J + . 1 ]) // if the same { SUM1 ++; // increment counter } the else { BREAK ; // or else exit, to avoid the same number of two additional situation } } COUT << S [I] << " " << SUM1 + . 1 << endl; // output I + = SUM1; // corresponds to the same number counted together, and then directly to the location of a number of unequal }
Here is the code:
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<algorithm> using namespace std; int s[200001]; int main() { int n; int maxn=-1; cin>>n; for(int i=1;i<=n;i++) { cin>>s[i]; } sort(s+1,s+n+1); for(int i=1;i<=n;i++) { int sum1=0; //cout<<s[i]<<endl; for(int j=i;j<=n;j++) { if(s[j]==s[j+1]) { sum1++; } else { break; } } cout<<s[i]<<" "<<sum1+1<<endl; i+=sum1; } return 0; }