[原創題]個人申請 - 提示、題解
GuguGagaOJ #6 個人申請
[原創題]個人申請 - 提示、題解
題目簡介
給定多個採計標準的篩選標準及篩選倍率,並給定學生的成績,依照個人申請規則,試輸出哪些人可以進入面試。
提示
推薦打開之後想想看再繼續,不要一次使用全部
提示1
多數語言的sort好像都可以自訂要使用哪些東西排序?
提示2
要將哪些人排序呢?
題解
點開會有完整解答,請想過後再開喔
思路
這題主要採用原本的sort函數,透過lambda設定比較條件就能夠快速排序。排序時可以限定範圍(C++本身就可以有範圍,Python可以用切片)在通過上個標準的人,即可達成需求。超額的部分,可以用變數紀錄上一次的人數,並從這次的目標人數開始判斷是否同分,直到上一次的人數為止。
完整程式碼
請注意,本程式碼僅供寫法上參考,直接複製貼上程式碼會導致你學不到任何東西。
C++ 範例程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
#define raymondwengcode ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main() {
raymondwengcode;
int p, n, m;
cin >> p >> n >> m;
vector<pair<int, vector<int>>> stu = vector<pair<int, vector<int>>>(m);
vector<pair<int, vector<int>>> subject(n);
for (int i = 0; i < n; i++) {
int k;
cin >> k >> subject[i].first;
subject[i].second = vector<int>(k);
for (int j = 0; j < k; j++) {
cin >> subject[i].second[j];
}
}
sort(subject.begin(), subject.end(), [](auto &a, auto &b) {return a.first > b.first;});
for (int i = 0; i < m; i++) {
stu[i] = make_pair(i, vector<int>(5));
for (int j = 0; j < 5; j++) {
cin >> stu[i].second[j];
}
}
int lastM = m;
for (int i = 0; i < n; i++) {
if (subject[i].first*p < lastM) {
sort(stu.begin(), stu.begin()+lastM, [&](pair<int, vector<int>>& a, pair<int, vector<int>>& b) {
int aa = 0, bb = 0;
for (auto j: subject[i].second) {
aa += a.second[j];
bb += b.second[j];
}
return aa > bb;
});
m = subject[i].first*p-1;
int sco = 0;
for (auto j: subject[i].second) {
sco += stu[m].second[j];
}
for (int j = m; j < lastM; j++) {
int currsco = 0;
for (auto k: subject[i].second) {
currsco += stu[j].second[k];
}
if (currsco == sco) {
m++;
}
}
lastM = m;
}
}
sort(stu.begin(), stu.begin()+m, [](auto &a, auto &b) {return a.first < b.first;});
cout << m << '\n';
for (int i = 0; i < m; i++) {
cout << stu[i].first << '\n';
}
return 0;
}
Python 範例程式碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def f(sub, sco):
res = 0
for i in sub:
res += sco[i+1]
return res
p, n, m = map(int, input().split())
sub = [list(map(int, input().split())) for _ in range(n)]
sub.sort(key=lambda x: x[1], reverse=True)
sco = [[i, *list(map(int, input().split()))] for i in range(m)]
lastM = m
for i in sub:
if p*i[1] > lastM:
continue
sco, m = sorted(sco[:lastM], key=lambda x: f(i[2:], x), reverse=True), p*i[1]
temp = f(i[2:], sco[m-1])
while m < lastM and temp == f(i[2:], sco[m]): m += 1
lastM = m
print(lastM)
for i in sorted(sco[:lastM], key=lambda x: x[0]): print(i[0])
本文章以
CC BY 4.0
授權