# jerry_fuyi

Working out everything from the first principles.

## PAT甲级题分类汇编——排序

 题号 标题 分数 大意 时间 1055 The World's Richest 25 限定范围排序结果 500ms 1056 Mice and Rice 25 分组排序 200ms 1062 Talent and Virtue 25 一定规则的排序 400ms 1075 PAT Judge 25 复杂排序 200ms 1080 Graduate Admission 30 志愿与录取 250ms 1083 List Grades 25 限定范围排序结果 400ms

1056：

 1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4
5 struct Programmer
6 {
7     int index;
8     int mice;
9     int score;
10     int rank;
11 };
12
13
14 int main(int argc, char const *argv[])
15 {
16     int total, per;
17     std::cin >> total >> per;
18     std::vector<Programmer> prog(total);
19     for (int i = 0; i != total; ++i)
20         prog[i].index = i, std::cin >> prog[i].mice;
21     std::vector<int> current(total);
22     for (int i = 0; i != total; ++i)
23         std::cin >> current[i];
24
25     std::vector<int> next;
26     while (1)
27     {
28         auto iter = current.begin();
29         int index;
30         while (iter != current.end())
31         {
32             int max = -1;
33             for (int i = 0; i != per && iter != current.end(); ++i, ++iter)
34                 if (prog[*iter].mice > max)
35                 {
36                     index = *iter;
37                     max = prog[*iter].mice;
38                 }
39             ++prog[index].score;
40             next.push_back(index);
41         }
42         if (next.size() == 1)
43             break;
44         current = next;
45         next.clear();
46     }
47
48     std::sort(prog.begin(), prog.end(), [](const Programmer& lhs, const Programmer& rhs) {
49         return lhs.score > rhs.score;
50     });
51     int count = 2;
52     prog.front().rank = 1;
53     for (auto iter = prog.begin() + 1; iter != prog.end(); ++iter, ++count)
54         if (iter->score == (iter - 1)->score)
55             iter->rank = (iter - 1)->rank;
56         else
57             iter->rank = count;
58     std::sort(prog.begin(), prog.end(), [](const Programmer& lhs, const Programmer& rhs) {
59         return lhs.index < rhs.index;
60     });
61     auto end = prog.end() - 1;
62     for (auto iter = prog.begin(); iter != end; ++iter)
63         std::cout << iter->rank << ' ';
64     std::cout << end->rank << std::endl;
65
66     return 0;
67 }

1075：

  1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <algorithm>
5
6 int num_problem;
7 std::vector<int> problem_full;
8
9 class User
10 {
11 public:
12     User(int _id)
13         : id_(_id), problems(num_problem, -2)
14     {
15         ;
16     }
17     void submission(int _pro, int _score)
18     {
19         if (_score > problems[_pro])
20             problems[_pro] = _score;
21     }
22     bool operator<(const User& _user) const
23     {
24         calculate();
25         _user.calculate();
26         if (score_ > _user.score_)
27             return true;
28         if (score_ < _user.score_)
29             return false;
30         if (perfect_ > _user.perfect_)
31             return true;
32         if (perfect_ < _user.perfect_)
33             return false;
34         return id_ < _user.id_;
35     }
36     bool valid() const
37     {
38         calculate();
39         return valid_;
40     }
41     void rank(int _rank)
42     {
43         rank_ = _rank;
44     }
45     int rank() const
46     {
47         return rank_;
48     }
49     int score() const
50     {
51         calculate();
52         return score_;
53     }
54     friend std::ostream& operator<<(std::ostream& _os, const User& _user);
55 private:
56     int id_;
57     std::vector<int> problems;
58     mutable bool calculated_;
59     mutable int score_;
60     mutable int perfect_;
61     mutable bool valid_;
62     int rank_;
63     void calculate() const
64     {
65         if (!calculated_)
66         {
67             calculated_ = true;
68             for (int i = 0; i != problems.size(); ++i)
69                 if (problems[i] >= 0)
70                 {
71                     score_ += problems[i];
72                     if (problems[i] == problem_full[i])
73                         ++perfect_;
74                     valid_ = true;
75                 }
76         }
77     }
78 };
79
80 std::ostream& operator<<(std::ostream& _os, const User& _user)
81 {
82     std::cout << std::setfill('0');
83     _os << _user.rank_ << ' ';
84     _os << std::setw(5) << _user.id_ << ' ';
85     _os << _user.score_;
86     for (int s : _user.problems)
87     {
88         std::cout << ' ';
89         if (s >= 0)
90             std::cout << s;
91         else if (s == -1)
92             std::cout << '0';
93         else
94             std::cout << '-';
95     }
96     return _os;
97 }
98
99 int main(int argc, char const *argv[])
100 {
101     int num_user;
102     int num_submission;
103     std::cin >> num_user >> num_problem >> num_submission;
104
105     std::vector<User> users;
106     users.reserve(num_user);
107     for (int i = 0; i != num_user; ++i)
108     {
109         users.emplace_back(i + 1);
110     }
111     problem_full.reserve(num_problem);
112     for (int i = 0; i != num_problem; ++i)
113     {
114         int t;
115         std::cin >> t;
116         problem_full.push_back(t);
117     }
118     for (int i = 0; i != num_submission; ++i)
119     {
120         int user, pro, score;
121         std::cin >> user >> pro >> score;
122         --user;
123         --pro;
124         users[user].submission(pro, score);
125     }
126     std::sort(users.begin(), users.end());
127     int count = 1;
128     users.front().rank(count++);
129     for (auto iter = users.begin() + 1; iter != users.end(); ++iter, ++count)
130     {
131         if (iter->score() == (iter - 1)->score())
132             iter->rank((iter - 1)->rank());
133         else
134             iter->rank(count);
135     }
136     for (const auto& u : users)
137         if (u.valid())
138             std::cout << u << std::endl;
139         else
140             break;
141
142     return 0;
143 }

1080：

 1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 #include <functional>
5 #include <utility>
6 using std::rel_ops::operator>;
7
8 struct School
9 {
10     int quota;
12     bool free()
13     {
14         return free_;
15     }
16     void lock()
17     {
18         free_ = admitted.size() < quota;
19     }
20 private:
21     bool free_;
22 };
23
24 struct Student
25 {
26     int id;
29     std::vector<int> choices;
30     int rank;
31     bool operator<(const Student& _rhs) const
32     {
34             return true;
36             return false;
38     }
39     bool operator==(const Student& _rhs) const
40     {
42     }
43 };
44
45 int main()
46 {
47     int num_applicant, num_school, num_choice;
48     std::cin >> num_applicant >> num_school >> num_choice;
49     std::vector<School> schools(num_school);
50     std::vector<Student> students(num_applicant);
51     for (auto& s : schools)
52         std::cin >> s.quota;
53     for (int i = 0; i != num_applicant; ++i)
54     {
55         auto& s = students[i];
56         s.id = i;
58         s.choices.resize(num_choice);
59         for (auto& i : s.choices)
60             std::cin >> i;
61     }
62     std::sort(students.begin(), students.end(), std::greater<Student>());
63     for (auto iter = students.begin() + 1; iter != students.end(); ++iter)
64         if (*iter == *(iter - 1))
65             iter->rank = (iter - 1)->rank;
66         else
67             iter->rank = (iter - 1)->rank + 1;
68     auto end = students.begin();
69     while (end != students.end())
70     {
71         auto iter = end;
72         while (end != students.end() && *end == *iter)
73             ++end;
74         for (auto& s : schools)
75             s.lock();
76         for (; iter != end; ++iter)
77         {
78             for (const auto& s : iter->choices)
79                 if (schools[s].free())
80                 {
82                     break;
83                 }
84         }
85     }
86     for (auto& s : schools)
87     {
90         {
91             auto end = s.admitted.end() - 1;
92             for (auto iter = s.admitted.begin(); iter != end; ++iter)
93                 std::cout << *iter << ' ';
94             std::cout << *end;
95         }
96         std::cout << std::endl;
97     }
98 }

posted on 2019-09-01 23:47  jerry_fuyi  阅读(538)  评论(0编辑  收藏  举报