2 条题解
-
0
快速排序方法:
#include <bits/stdc++.h> using namespace std; struct P { int k, s; } a[5005]; void qksrt(P a[], int l, int r) { if (l >= r) return; int i = l, j = r; P pvt = a[(l + r) / 2]; while (i <= j) { while (a[i].s > pvt.s || (a[i].s == pvt.s && a[i].k < pvt.k)) i++; while (a[j].s < pvt.s || (a[j].s == pvt.s && a[j].k > pvt.k)) j--; if (i <= j) { swap(a[i], a[j]); i++; j--; } } qksrt(a, l, j); qksrt(a, i, r); } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i].k >> a[i].s; qksrt(a, 0, n-1); int x = m * 3 / 2; int ln = a[x-1].s; int cnt = 0; while (cnt < n && a[cnt].s >= ln) cnt++; cout << ln << ' ' << cnt << '\n'; for (int i = 0; i < cnt; i++) cout << a[i].k << ' ' << a[i].s << '\n'; return 0; } -
0
原题解:
//方法1: sort排序 #include <bits/stdc++.h> using namespace std; struct Student{ int name; int score; }; bool cmp(Student a,Student b){ if(a.score==b.score){ return a.name<b.name; }else{ return a.score>b.score; } } int main(){ int n, m, i, a, b, x, y=0; cin >> n >> m; struct Student t[10000]; for (i = 0; i < n; i++){ cin >> t[i].name >> t[i].score; } sort(t, t + n, cmp); x = floor(m * 3 / 2); for (i = 0; i < n; i++) { if (t[i].score>= t[x - 1].score) { y++; } } cout << t[x - 1].score << " " << y << endl; for (int i = 0; i < y; i++){ cout << t[i].name << " " << t[i].score; cout<<endl; } return 0; }
- 1
信息
- ID
- 660
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 19
- 已通过
- 3
- 上传者