用了冒泡和插入排序 果然没有什么本质区别。。都是运行超时
用库函数sort也超时
The ranklist of PAT is generated from the status list, which shows the scores of the submittions. This time you are supposed to generate the ranklist for PAT.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 positive integers, N (≤10^4), the total number of users, K (≤5), the total number of problems, and M ≤10^5), the total number of submittions. It is then assumed that the user id's are 5-digit numbers from 00001 to N, and the problem id's are from 1 to K. The next line contains K positive integers p[i]
(i
=1, ..., K), where p[i]
corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submittion in the following format:
user_id problem_id partial_score_obtained
where partial_score_obtained
is either -1 if the submittion cannot even pass the compiler, or is an integer in the range [0, p[problem_id]
]. All the numbers in a line are separated by a space.
Output Specification:
For each test case, you are supposed to output the ranklist in the following format:
rank user_id total_score s[1] ... s[K]
where rank
is calculated according to the total_score
, and all the users with the same total_score
obtain the same rank
; and s[i]
is the partial score obtained for the i
-th problem. If a user has never submitted a solution for a problem, then "-" must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.
The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id's. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.
Sample Input:
7 4 20
20 25 25 30
00002 2 12
00007 4 17
00005 1 19
00007 2 25
00005 1 20
00002 2 2
00005 1 15
00001 1 18
00004 3 25
00002 2 25
00005 3 22
00006 4 -1
00001 2 18
00002 1 20
00004 1 15
00002 4 18
00001 3 4
00001 4 2
00005 2 -1
00004 2 0
Sample Output:
1 00002 63 20 25 - 18
2 00005 42 20 0 22 -
2 00007 42 - 25 - 17
2 00001 42 18 18 4 2
5 00004 40 15 0 25 -
#include <stdio.h> struct user
{
int id;
int score[];
int solved;
int sum;
int flag;
}users[]; int main()
{
int N, K, M, max = ;
scanf("%d %d %d", &N, &K, &M);
int p[] = {};
for(int i = ; i <= K; i++) {
scanf("%d",&p[i]);
max += p[i];
} //初始化
for(int i = ; i <= N;i++) {
users[i].id = i;
users[i].solved = ;
users[i].sum = ;
users[i].flag = ; for(int j = ; j < ; j++)
users[i].score[j] = -;
} int tmpUserId, tmpProId, tmpPSO;//user_id problem_id partial_score_obtained
for(int i = ; i < M; i++) {
scanf("%d %d %d", &tmpUserId, &tmpProId, &tmpPSO);
if(users[tmpUserId].score[tmpProId] < tmpPSO) {
if(tmpPSO > -)
users[tmpUserId].flag = ; //有提交 how about -1
users[tmpUserId].score[tmpProId] = tmpPSO; //更新最高分 if(tmpPSO == p[tmpProId]) //满分的问题数
users[tmpUserId].solved++;
}
} //统计sum
for(int i = ; i <= N; i++) {
for(int j = ; j < ; j++) {
if(users[i].score[j] > )
users[i].sum += users[i].score[j];
}
}
// for(int i = 1; i <= N; i++)
// printf("%d : %d\n",i,users[i].sum);
//冒泡排序
int sort[]; //存放顺序
for(int i = ; i <= N; i++)
sort[i] = i;
for(int i = N; i > ; i--) {
int flag = ;
for(int j = ; j < i; j++) {
if( users[sort[j]].sum < users[sort[j+]].sum ) {
int tmp = sort[j]; //交换次序
sort[j] = sort[j+];
sort[j+] = tmp;
flag = ;
// printf("j:%d sort[j]:%d %d\n",j,sort[j],sort[j+1]);
} else if(users[sort[j]].sum == users[sort[j+]].sum) { //如果总分一样
if(users[sort[j]].solved < users[sort[j+]].solved) { //根据完美解决问题数排序
int tmp = sort[j]; //交换次序
sort[j] = sort[j+];
sort[j+] = tmp;
flag = ;
} //由于是冒泡排序稳定,id小的在前面 不需要再判断
}
}
// for(int i = 1; i <= N; i++)
// printf("%d ",sort[i]);
// printf("\n");
if(flag == ) break;
} // for(int i = 1; i <= N; i++) {
// printf("%05d %d ", users[i].id, users[i].sum);
// for(int j = 1; j < K; j++) {
// if(users[i].score[j] == -1)
// users[i].score[j] = 0;
// if(users[i].score[j] >= 0)
// printf("%d ",users[i].score[j]);
// else printf("- ");
// }
// if(users[i].score[K] >= 0)
// printf("%d\n",users[i].score[K]);
// else printf("-\n");
// } //显示输出
int rank = ;
int currentSum = max;
for(int i = ; i <= N; i++) {
if(users[sort[i]].flag == )
continue; if(users[sort[i]].sum == currentSum)
;
else
rank = i; currentSum = users[sort[i]].sum;
printf("%d %05d %d ", rank, users[sort[i]].id, currentSum); for(int j = ; j < K; j++) {
if(users[sort[i]].score[j] == -)
users[sort[i]].score[j] = ;
if(users[sort[i]].score[j] >= )
printf("%d ",users[sort[i]].score[j]);
else printf("- ");
}
if(users[sort[i]].score[K] >= )
printf("%d\n",users[sort[i]].score[K]);
else printf("-\n");
} return ;
}
Bubble Sort
#include <stdio.h> struct user
{
int id;
int score[];
int solved;
int sum;
int flag;
}users[]; int main()
{
int N, K, M, max = ;
scanf("%d %d %d", &N, &K, &M);
int p[] = {};
for(int i = ; i <= K; i++) {
scanf("%d",&p[i]);
max += p[i];
} //初始化
for(int i = ; i <= N;i++) {
users[i].id = i;
users[i].solved = ;
users[i].sum = ;
users[i].flag = ; for(int j = ; j < ; j++)
users[i].score[j] = -;
} int tmpUserId, tmpProId, tmpPSO;//user_id problem_id partial_score_obtained
for(int i = ; i < M; i++) {
scanf("%d %d %d", &tmpUserId, &tmpProId, &tmpPSO);
if(users[tmpUserId].score[tmpProId] < tmpPSO ) {
if(tmpPSO > -)
users[tmpUserId].flag = ; //有提交
users[tmpUserId].score[tmpProId] = tmpPSO; //更新最高分 // if(tmpPSO == p[tmpProId]) //满分的问题数
// users[tmpUserId].solved++;
}
} //统计sum
for(int i = ; i <= N; i++) {
for(int j = ; j < ; j++) {
if(users[i].score[j] > ) {
// users[i].flag = 1; //有提交
users[i].sum += users[i].score[j];
if(users[i].score[j] == p[j]) //满分的问题数
users[i].solved++;
}
}
} //插入排序
int sort[]; //存放顺序
for(int i = ; i <= N; i++)
sort[i] = i;
int j;
for(int i = ; i <= N; i++) {
int tmp = sort[i];
for(j = i; j > ; j--) {
if(users[sort[j]].sum > users[sort[j-]].sum) {
sort[j] = sort[j-];
sort[j-] = tmp;
} else if(users[sort[j]].sum == users[sort[j-]].sum) {
if(users[sort[j]].solved > users[sort[j-]].solved) {
sort[j] = sort[j-];
sort[j-] = tmp;
}
} else //由于是插入排序稳定,id小的在前面 不需要再判断
break;
}
// for(int k = 1; k <= N; k++)
// printf("%d ",sort[k]);
// printf("\n");
} //显示输出
int rank = ;
int currentSum = max;
for(int i = ; i <= N; i++) {
if(users[sort[i]].flag == )
continue; if(users[sort[i]].sum == currentSum)
;
else
rank = i; currentSum = users[sort[i]].sum;
printf("%d %05d %d ", rank, users[sort[i]].id, currentSum); for(int j = ; j < K; j++) {
if(users[sort[i]].score[j] == -)
users[sort[i]].score[j] = ;
if(users[sort[i]].score[j] >= )
printf("%d ",users[sort[i]].score[j]);
else printf("- ");
}
if(users[sort[i]].score[K] >= )
printf("%d\n",users[sort[i]].score[K]);
else printf("-\n");
} return ;
}
Insertion sort
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std; struct user
{
int id;
int score[];
int solved;
int sum;
int flag;
}users[]; /*cmp函数的返回值为true和false或1和0,
若为true/1,则sort()函数为升序排列,
若为false/0,则sort()函数为降序排列。
*/
bool cmp(user u1, user u2) {
if (u1.sum != u2.sum){
return u1.sum > u2.sum;
} else{
if (u1.solved != u2.solved) {
return u1.solved > u2.solved;
} else{
return u1.id < u2.id;
}
}
} int main()
{
int N, K, M, max = ;
scanf("%d %d %d", &N, &K, &M);
int p[] = {};
for(int i = ; i <= K; i++) {
scanf("%d",&p[i]);
max += p[i];
} //初始化
for(int i = ; i <= N;i++) {
users[i].id = i;
users[i].solved = ;
users[i].sum = ;
users[i].flag = ; for(int j = ; j < ; j++)
users[i].score[j] = -;
} int tmpUserId, tmpProId, tmpPSO;//user_id problem_id partial_score_obtained
for(int i = ; i < M; i++) {
scanf("%d %d %d", &tmpUserId, &tmpProId, &tmpPSO);
if(users[tmpUserId].score[tmpProId] < tmpPSO ) {
if(tmpPSO > -)
users[tmpUserId].flag = ; //有提交
users[tmpUserId].score[tmpProId] = tmpPSO; //更新最高分 // if(tmpPSO == p[tmpProId]) //满分的问题数
// users[tmpUserId].solved++;
}
} vector<user> vec;
//统计sum
for(int i = ; i <= N; i++) {
for(int j = ; j < ; j++) {
if(users[i].score[j] > ) {
// users[i].flag = 1; //有提交
users[i].sum += users[i].score[j];
if(users[i].score[j] == p[j]) //满分的问题数
users[i].solved++;
}
}
if(users[i].sum >= )
vec.push_back(users[i]);
} sort(vec.begin(),vec.end(),cmp); //显示输出
int rank = ;
int currentSum = max;
for(int i = ; i < N; i++) {
if(vec[i].flag == )
continue; if(vec[i].sum == currentSum)
;
else
rank = i+; currentSum = vec[i].sum;
printf("%d %05d %d ", rank, vec[i].id, currentSum); for(int j = ; j < K; j++) {
if(vec[i].score[j] == -)
vec[i].score[j] = ;
if(vec[i].score[j] >= )
printf("%d ",vec[i].score[j]);
else printf("- ");
}
if(vec[i].score[K] >= )
printf("%d\n",vec[i].score[K]);
else printf("-\n");
}
return ;
}
sort