pat A1080 Graduate Admission 测试点四为什么段错误?
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct student{
int ge,sum,rank,id;
vector<int> pre;
}s[40010];
bool cmp(student& a,student& b)
{
if(a.sum!=b.sum) return a.sum>b.sum;
else if(a.ge!=b.ge) return a.ge>b.ge;
}
bool cmp1(student& a,student& b)
{
return a.id<b.id;
}
int main()
{
int n,m,k,temp;
int quota[101];
vector<student> p[101];
scanf("%d %d %d",&n,&m,&k);
for(int i=0;i<m;i++)
scanf("%d","a[i]);
for(int i=0;i<n;i++)
{
s[i].id=i;
scanf("%d %d",&s[i].ge,&temp);
s[i].sum=s[i].ge+temp;
for(int j=0;j<k;j++)
{
scanf("%d",&temp);
s[i].pre.push_back(temp);
}
}
sort(s,s+n,cmp);
s[0].rank=0;
for(int i=0;i<n;i++)
{
if(s[i+1].sum==s[i].sum&&s[i+1].ge==s[i].ge)
s[i+1].rank=s[i].rank;
else
s[i+1].rank=i+1;
}
for(int i=0;i<n;i++)
{
vector<int>::iterator it=s[i].pre.begin();
while(it!=s[i].pre.end())
{
if(quota[*it]!=0)
{
p[*it].push_back(s[i]);
quota[*it]--;break;
}
else
{
vector<student>::iterator it1=p[*it].end()-1;
if(it1->rank==s[i].rank)
{ p[*it].push_back(s[i]);break;}
}
it++;
}
}
for(int i=0;i<m;i++)
{
vector<student>::iterator it=p[i].begin();
if(p[i].size()!=0)
{
sort(p[i].begin(),p[i].end(),cmp1);
while(it!=p[i].end())
{
printf("%d",it->id);
if(it!=p[i].end()-1)
printf(" ");
it++;
}
printf("\n");
}
else
printf("\n");
}
return 0;
}
是不是s[0].rank应该等于1?
请给出题目thanks
1080 Graduate Admission (30分)
It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.
Each applicant will have to provide two grades: the national entrance exam grade GE, and the interview grade GI. The final grade of an applicant is (GE+GI)/2. The admission rules are:
-
The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
-
If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE. If still tied, their ranks must be the same.
-
Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one's turn to be admitted; and if the quota of one's most preferred shcool is not exceeded, then one will be admitted to this school, or one's other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
-
If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.
Input Specification:
Each input file contains one test case.
Each case starts with a line containing three positive integers: N (≤40,000), the total number of applicants; M (≤100), the total number of graduate schools; and K (≤5), the number of choices an applicant may have.
In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.
Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant's GE and GI, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M−1, and the applicants are numbered from 0 to N−1.
Output Specification:
For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants' numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.
Sample Input:
11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4
Sample Output:
0 10
3
5 6 7
2 8
1 4
在pta上的advanced level
看不出来哪里错了,提供一下本人代码吧(不一定能AC)。
#include <bits/stdc++.h>
using namespace std;
struct stu {
int id, rank; double ge, gi, gf;
vector<int> like;
bool operator < (const stu &other) const {
if (this -> gf != other.gf) {
return this -> gf > other.gf;
} else {
return this -> ge > other.ge;
}
}
};
int main() {
int N, M, K; cin >> N >> M >> K;
int a[M]; vector<stu> b(N);
for (int i = 0; i < M; ++i) {
cin >> a[i];
}
for (int i = 0; i < N; ++i) {
cin >> b[i].ge >> b[i].gi;
b[i].id = i; b[i].gf = (b[i].ge + b[i].gi) / 2; b[i].like.resize(K);
for (int j = 0; j < K; ++j) {
cin >> b[i].like[j];
}
}
int res[N], cnt = 1;
sort(b.begin(), b.end());
b[0].rank = cnt++; res[b[0].id] = b[0].rank;
for (int i = 1; i < b.size(); ++i) {
if (b[i].gf == b[i - 1].gf && b[i].ge == b[i - 1].ge) {
b[i].rank = b[i - 1].rank;
} else {
b[i].rank = cnt;
}
res[b[i].id] = b[i].rank; ++cnt;
}
vector<vector<int> > school(M);
for (int i = 0; i < b.size(); ++i) {
for (int j = 0; j < K; ++j) {
int like = b[i].like[j];
if (school[like].size() < a[like] || res[school[like][school[like].size() -1]] == b[i].rank) {
school[like].push_back(b[i].id); break;
}
}
}
for (int i = 0; i < M; ++i) {
sort(school[i].begin(), school[i].end());
if (school[i].size()) {
for (int j = 0; j < school[i].size(); ++j) {
cout << school[i][j];
if (j < school[i].size() - 1) {
cout << ' ';
} else {
puts("");
}
}
} else {
puts("");
}
}
return 0;
}
大佬呀,全部ac了
不过我想知道我哪错了,我感觉没问题啊呀呀呀呀呀
我看看
我找到错了,因为cmp函数那里少了一个return a.sum>b.sum
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 100010;
struct Stu{
int id, score;
}stu[100];
bool cmp(Stu a, Stu b) {
if(a.score!=b.score) return a.score > b.score;
}
int main() {
int n;
cin >> n;
for(int i=0; i<n; i++) {
cin >> stu[i].id >> stu[i].score;
}
sort(stu, stu+n, cmp);
for(int i=0;i<n;i++) {
cout << stu[i].id << " " << stu[i].score << endl;
}
return 0;
}
输入:
3
1 100
2 100
3 100
输出:
3 100
2 100
1 100
而我们期望得到的是
1 100
2 100
3 100
(我觉得好坑,你觉得呢??)
鹅鹅鹅,好坑