码迷,mamicode.com
首页 > 其他好文 > 详细

1025. PAT Ranking (25)

时间:2015-12-06 11:22:37      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

尽量避免二次循环,很容易运行超时。
该算法用printf 用时30ms 使用cout 运行75ms,只差这一句话。所以输出很多的时候不应该使用cout
g++ 编译器使用 strcmp 一定要引用 string.h 头



Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (<=100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (<=300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

 
  1. #pragma warning(disable:4996)
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. #include <stdio.h>
  7. #include <string.h>
  8. using namespace std;
  9. struct Stu
  10. {
  11. // string regNum;
  12. char regNum[15];
  13. int group;
  14. int total_rank;
  15. int group_rank;
  16. int grade;
  17. };
  18. int score[101];
  19. vector<Stu> s;
  20. vector<Stu> all;
  21. bool cmp1(Stu a,Stu b) {
  22. if (a.grade != b.grade)
  23. return a.grade > b.grade;
  24. else
  25. // return a.regNum < b.regNum;
  26. return strcmp(a.regNum, b.regNum) < 0;
  27. }
  28. int main(void) {
  29. for (int i = 0; i <= 100; i++)
  30. score[i] = 0;
  31. int n;
  32. cin >> n;
  33. for (int i = 0; i < n; i++) {
  34. int k;
  35. cin >> k;
  36. s.clear();
  37. for (int j = 0; j < k; j++) {
  38. Stu temp;
  39. // cin >> temp.regNum >> temp.grade;
  40. scanf("%s %d", temp.regNum, &temp.grade);
  41. temp.group = i+1;
  42. s.push_back(temp);
  43. score[temp.grade]++;
  44. }
  45. sort(s.begin(), s.end(), cmp1);
  46. for (int p = 0; p < k; p++) {
  47. for (int j = 0; j <= p; j++) {
  48. if (s[p].grade == s[j].grade) {
  49. s[p].group_rank = j + 1;
  50. break;
  51. }
  52. }
  53. }
  54. for (int r = 0; r < k;r++)
  55. all.push_back(s[r]);
  56. }
  57. sort(all.begin(), all.end(), cmp1);
  58. for (int i = 0; i < all.size(); i++) {
  59. all[i].total_rank = 1;
  60. if (all[i].grade != 100) {
  61. for (int q = 100; q > all[i].grade; q--) {
  62. all[i].total_rank += score[q];
  63. }
  64. }
  65. }
  66. cout << all.size() << endl;
  67. for (int i = 0; i < all.size(); i++) {
  68. cout << all[i].regNum <<" "<< all[i].total_rank << " " << all[i].group << " " << all[i].group_rank << endl;
  69. //printf("%s %d %d %d\n", all[i].regNum, all[i].total_rank, all[i].group, all[i].group_rank);
  70. }
  71. return 0;
  72. }





1025. PAT Ranking (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!