1015 theory of virtue and talent (25 points)
Sima Guang, a historian of the Song Dynasty, wrote a famous "theory of virtue and talent" in Zizhi Tongjian: "therefore, the perfection of talent and morality is called a saint, the death of talent and morality is called a fool, the victory of virtue is called a gentleman, and the victory of virtue is called a villain. If a gentleman takes the skill of man, he can't be a saint. If a gentleman takes it, he won't be a fool rather than a villain."
The scores of virtue and talent of a group of candidates are given. Please give the admission ranking according to Sima Guang's theory.
Input format:
Input the first line to give 3 positive integers, respectively: N (≤ 10 ﹤ 5), that is, the total number of candidates; L (≥ 60), which is the lowest score line for admission, that is, candidates whose moral score and talent score are not lower than , L , are eligible for admission; H (< 100) is the priority admission line - those whose moral score and talent score are not lower than this line are defined as "full of talent and virtue", and such candidates are ranked from high to low according to the total score of morality and talent; The candidates who can't get the talent score but get the moral score line belong to "virtue wins talent", which is also sorted according to the total score, but they are ranked behind the first category of candidates; Candidates whose moral and talent scores are lower than {h, but whose moral scores are not lower than talent scores belong to "both talent and morality" but still have "virtue wins talent", which are sorted according to the total score, but ranked behind the second category of candidates; Other candidates who have reached the lowest line of , L , are also ranked according to the total score, but they are ranked behind the third category of candidates.
Then, in line N, each line gives the information of one candidate, including: admission card number, German score, in which the admission card number is an 8-digit integer, and German score is an integer in the interval [0, 100]. Numbers are separated by spaces.
Output format:
The first line of output first gives the number of candidates who have reached the lowest score line, and then ^ M ^ lines. Each line outputs the information of one candidate according to the input format, and the candidates are sorted from high to low according to the rules described in the input. When there are many candidates with the same total score, they are arranged in descending order according to their moral score; If the scores are also in parallel, they will be output in ascending order of the admission number.
Input example:
14 60 80 10000001 64 90 10000002 90 60 10000011 85 80 10000003 85 80 10000004 80 85 10000005 82 77 10000006 83 76 10000007 90 78 10000008 75 79 10000009 59 90 10000010 88 45 10000012 80 100 10000013 90 99 10000014 66 60
Output example:
12 10000013 90 99 10000012 80 100 10000003 85 80 10000011 85 80 10000004 80 85 10000007 90 78 10000006 83 76 10000005 82 77 10000002 90 60 10000014 66 60 10000008 75 79 10000001 64 90
When I first wrote, Num used the string type of char array (since num is an 8-bit number, it can directly use int to save space and time), and the operation timed out
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<stdio.h> using namespace std; struct student { char num[9];// int de; int cai; int sum; }; bool cmp(student a,student b) { if(a.sum!=b.sum)return a.sum>b.sum; else { if(a.de!=b.de) return a.de>b.de; } return a.num<b.num; } int main() { vector <student>v[4]; int n,L,H,total=0; scanf("%d %d %d",&n,&L,&H); student s; for(int i=0;i<n;i++) { scanf("%s %d %d",s.num,&s.de,&s.cai); s.sum=s.de+s.cai; if(s.de>=L&&s.cai>=L) { total++; if(s.de>=H&&s.cai>=H) { v[0].push_back(s); } else if(s.de>=H&&s.cai<H) { v[1].push_back(s); } else if(s.de<H&&s.de>=s.cai) { v[2].push_back(s); } else { v[3].push_back(s); } } } printf("%d\n",total); for(int i=0;i<4;i++) { sort(v[i].begin(),v[i].end(),cmp); for(int j=0;j<v[i].size();j++) { printf("%s %d %d\n",v[i][j].num,v[i][j].de,v[i][j].cai); } } }
The second time num uses Int , type, it is found that it still can't pass. The reason is that CIN and cout are slower than scanf and printf functions (I don't understand the details). Therefore, if the algorithm is not well written, it can't pass the big data test point. Here is the optimized code, which can barely pass
#include<vector> #include<iostream> #include<algorithm> #include<stdio.h> using namespace std; struct student { int num; int de; int cai; int sum; }; bool cmp(student a,student b) { if(a.sum!=b.sum)return a.sum>b.sum; else { if(a.de!=b.de) return a.de>b.de; } return a.num<b.num; } int main() { vector <student>v[4]; int n,L,H,total=0; cin>>n>>L>>H; student s; for(int i=0;i<n;i++) { cin>>s.num>>s.de>>s.cai; s.sum=s.de+s.cai; if(s.de>=L&&s.cai>=L) { total++; if(s.de>=H&&s.cai>=H) { v[0].push_back(s); } else if(s.de>=H&&s.cai<H) { v[1].push_back(s); } else if(s.de<H&&s.de>=s.cai) { v[2].push_back(s); } else { v[3].push_back(s); } } } cout<<total<<endl; for(int i=0;i<4;i++) { sort(v[i].begin(),v[i].end(),cmp); for(int j=0;j<v[i].size();j++) { cout<<v[i][j].num<<" "<<v[i][j].de<<" "<<v[i][j].cai<<endl; } } }
If the algorithm is not well written, it is recommended to use scanf and printf functions, which are nearly three or four times more efficient. You can see by comparing the time-consuming
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<stdio.h> using namespace std; struct student { int num; int de; int cai; int sum; }; bool cmp(student a,student b) { if(a.sum!=b.sum)return a.sum>b.sum; else { if(a.de!=b.de) return a.de>b.de; } return a.num<b.num; } int main() { vector <student>v[4]; int n,L,H,total=0; scanf("%d %d %d",&n,&L,&H); student s; for(int i=0;i<n;i++) { scanf("%d %d %d",&s.num,&s.de,&s.cai); s.sum=s.de+s.cai; if(s.de>=L&&s.cai>=L) { total++; if(s.de>=H&&s.cai>=H) { v[0].push_back(s); } else if(s.de>=H&&s.cai<H) { v[1].push_back(s); } else if(s.de<H&&s.de>=s.cai) { v[2].push_back(s); } else { v[3].push_back(s); } } } printf("%d\n",total); for(int i=0;i<4;i++) { sort(v[i].begin(),v[i].end(),cmp); for(int j=0;j<v[i].size();j++) { printf("%d %d %d\n",v[i][j].num,v[i][j].de,v[i][j].cai); } } }
All less than 100ms