Data Sorting second lecture (various sorting methods combined with NOI question Bank 1.10)

Source: Internet
Author: User

First of all, practice practiced hand!

Examples

04: Scholarship
    • View
    • Submit
    • Statistics
    • Questions
Total time limit:
1000ms
Memory Limit:
65536kB
Describe

A primary school has recently received a sponsorship, and intends to take part in a scholarship for the top 5 students with outstanding academic performance. At the end of the semester, each student has 3 subjects: Chinese, maths, English. First, according to the total score from high to low, if the two students with the same total score, and then according to the language scores from high to low sort, if the total score of two students and the same language scores, then the number of students in the order of the students ranked in front, so that each student's sort is the only certainty.

Task: First calculate the total score according to the results of the 3 courses entered, then sort according to the above rules, and finally output the first five students ' number and total score in order of rank. Note that in the top 5 students, each person's scholarship is different, so you must strictly according to the above rules and order. For example, in a correct answer, if the output data for the first two rows (two numbers per line: study number, total score) is:

7 279

5 279

The meaning of these two lines of data is: The highest total score of two students of the school number is 7th, 5th number. The total score of the two students is 279 (total is equal to the input of the language, mathematics, English, the sum of the three subjects), but the student number of 7 of the students have a higher Chinese performance. If the output data for your first two names is:

5 279

7 279

The output error is processed and the score cannot be scored.

Input
contains n+1 lines:

The 1th act is a positive integer n, indicating the number of students enrolled in the school.

2nd to n+1, each line has 3 digits separated by a space, each of which is between 0 and 100. The 3 numbers in line J indicate the students ' grades in Chinese, maths and English in j-1. Each student's school number is numbered 1~n (the line number of the input data minus 1) according to the input order.

The data given are correct and do not have to be tested.

50% of the data to meet: Each student's overall scores are different;
100% of the data meet: 6<=n<=300.
Output
There are 5 lines, each line is two positive integers separated by a space, which in turn represents the first 5 students ' number and total score.
Sample input
Example #1:690 67 8087 66 9178 89 9188 99 7767 89 6478 89 98 Example #2:880 89 89 88 98 7890 67 8087 66 9178 89 9188 99 7767 89 6478 89 98
Sample output
Example #1:6 2654 2643 2582 2441 237 Sample #2:8 2652 2646 2641 2585 258
Source
The
first problem of the popularization group of NOIP2007
Do
you have any idea how to use this question?
Yes, it's using struct sort.
Let's take a look at the
code.
#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structstudent{intMach,chinese,english,xh,total;} a[305];intN;BOOLScore_comp (ConstStudent&x,Conststudent y) {    if(x.total>y.total)return 1; if(x.total<y.total)return 0; if(X.chinese>y.chinese)return 1; if(X.chinese<y.chinese)return 0; if(X.XH&LT;Y.XH)return 1; return 0; } intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) {cin>>a[i].chinese>>a[i].mach>>A[i].english; A[i].xh=i; A[i].total=a[i].chinese+a[i].mach+A[i].english; } sort (A+1, A +1+N,score_comp);  for(intI=1; i<=5; i++) cout<<a[i].xh<<" "<<a[i].total<<Endl; return 0;}

The following question is the same as the subject thought of the above question. 05: Fraction demarcation
Total time limit:
1000ms
Memory Limit:
65536kB
Describe

The selection of World Expo Volunteers is in full swing in a city. In order to select the most suitable talent, a city of all registered contestants have a written test, written scores to meet the interview scores of the contestants can enter the interview. The interview score is based on 150% of the planned number of admissions, that is, if you plan to enroll M volunteers, then the interview scores are ranked m*150% (rounded down), and the contestants who end up in the interview are not below the interview scores.

Now, please write a program to delimit the interview scores, and output all the contestants to enter the interview number and written test results.

Input
The first line, two integers n,m (5≤n≤5000,3≤m≤n), is separated by a space, where n represents the total number of contestants enrolled in the written test, and M indicates the number of volunteers planned to enroll. The input data guarantees that the m*150% is less than or equal to n after rounding down.
The second line to line n+1, each line consists of two integers, the middle is separated by a space, respectively, the contestant's registration number K (1000≤k≤9999) and the contestant's written test S (1≤s≤100). The data guarantees that the contestants ' registration number is different.
Output
The
first line, there are two integers, separated by a space, the first integer represents the interview fraction, and the second integer is the actual number of contestants entering the interview.
Starting from the second line, each line contains two integers, separated by a space, respectively, the entry number of the contestants entering the interview and the written test results, according to the written test results from high to low output, if the results are the same, the number of entries from small to large sequence output.
Sample input
6 31000 903239 882390 957231 841005 951001 88
Sample output
88 51005 952390 951000 901001 883239 88
Tips
Sample Description: m*150% = 3*150% = 4.5, rounded down to 4. To ensure that 4 people enter the interview score of 88, but because 88 have a heavy, so all the results of more than 88 players can enter the interview, so eventually 5 people into the interview.
Source
#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structstudent{intMach,chinese,english,xh,total;} a[305];intN;BOOLScore_comp (ConstStudent&x,Conststudent y) {    if(x.total>y.total)return 1; if(x.total<y.total)return 0; if(X.chinese>y.chinese)return 1; if(X.chinese<y.chinese)return 0; if(X.XH&LT;Y.XH)return 1; return 0; } intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) {cin>>a[i].chinese>>a[i].mach>>A[i].english; A[i].xh=i; A[i].total=a[i].chinese+a[i].mach+A[i].english; } sort (A+1, A +1+N,score_comp);  for(intI=1; i<=5; i++) cout<<a[i].xh<<" "<<a[i].total<<Endl; return 0;}

Insert Sort
1. Basic ideas:
Suppose the data to be sorted is stored in the array R[1...N] and a sentinel node X is added.
(1) r[1] self into an orderly area, the disordered area is R[2...N];
(2) from i=2 until I=n, put R[i] in the proper place, so that the r[1...i] array order
1.x=r[i];
2. Compare X to the number of previous i-1, J=i-1;wjile (X<a[j]) j=j-1;
3. Move the elements of the R array backward from J: for (k=i;k>=j;k--) a[k]=a[k-1]
4.r[j]=x;
(3) generating an ordered number area with n data
2. Base Code
int insertsort (int  r[]) {    for (i=2;i<=n;i++)    {        x=r[i];        J=i-1;          while (x<r[i])        {            r[j+1]=r[j];            J--;        }        R[j+1]=x;}    }

Quick Sort

Here's just the code.

#include <cstdio>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;inta[4000000];intN;voidQsortintLintR) {    inti,j,mid,p; I=l;//StartJ=r;//Endmid=a[(L+R)/2];  Do    {         while(A[i]<mid) i++;  while(A[j]>mid) j--; if(i<=j) {P=A[i]; A[i]=A[j]; A[J]=p; I++; J--; }    } while(i<=j); if(l<j) Qsort (L,J); if(i<r) qsort (I,R);}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) Cin>>A[i]; Qsort (1, N);  for(intI=1; i<n;i++) cout<<a[i]<<" "; cout<<A[n]; return 0;}

Data Sorting second lecture (various sorting methods combined with NOI question Bank 1.10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.