http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=570&pid=1001
Official:
For each line of input one or two integer to make a difference, according to the difference from large to small, if the difference is the same, according to the following integer from small to large, if the same as the ID from small to large sort.
First notice the data range, about 100 sets of data, all integers in the range of [1,100], even if the bubble method or selection method is not tle. Second, it is necessary to consider how to sort the city labels together, you can build a special save the city label array, sorting by the city marking the corresponding data to compare, only change the location of the city label, the data is not sorted. 、
Put one of the simplest codes first.
#include <stdio.h>intMain () {inti,j,n,pm[ the][2],c[ the],t,s[ the]; while(SCANF ("%d", &n)! =EOF) { for(i=0; i<n;i++) {scanf ("%d%d", &pm[i][0],&pm[i][1]); C[i]=pm[i][0]-pm[i][1]; S[i]=i; s array records the city's label} for(i=0; i<n-1; i++)//First sort by difference, note just sort the city's labels { for(j=0; j<n-1-i;j++) { if(c[s[j]]<c[s[j+1]]) {T=S[j]; S[J]=s[j+1]; S[j+1]=T; } } } for(i=0; i<n-1; i++)// According to the second measured value in ascending order, also sort the label of the city {if(c[s[i]]==c[s[i+1]]) { if(pm[s[i]][1]>pm[s[i+1]][1]) {T=S[i]; S[i]=s[i+1]; S[i+1]=T; if(i>0) I=i-2; } } } for(i=0; i<n-1; i++)// sort in order of input {if(c[s[i]]==c[s[i+1]]) { if(pm[s[i]][1]==pm[s[i+1]][1]) { if(s[i]>s[i+1]) {T=S[i]; S[i]=s[i+1]; T[n+1]=T; if(i>0) I=i-2; } } } } for(i=0; i<n;i++) { if(i==0) printf ("%d", S[i]); Elseprintf ("%d", S[i]); } printf ("\ n"); } return 0;}
After the game think of too much time, found that you can use a structure to save the data, sorted by the sort function, just write the CMP function.
The code is as follows:
Bestcoder Round #32 1001