用了冒泡和插入排序 果然没有什么本质区别。。都是运行超时

用库函数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

 

10-排序5 PAT Judge的更多相关文章

  1. PAT 1075 PAT Judge[比较]

    1075 PAT Judge (25 分) The ranklist of PAT is generated from the status list, which shows the scores ...

  2. PTA 10-排序5 PAT Judge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PA ...

  3. A1075 PAT Judge (25)(25 分)

    A1075 PAT Judge (25)(25 分) The ranklist of PAT is generated from the status list, which shows the sc ...

  4. PAT 甲级 1075 PAT Judge (25分)(较简单,注意细节)

    1075 PAT Judge (25分)   The ranklist of PAT is generated from the status list, which shows the scores ...

  5. PAT_A1075#PAT Judge

    Source: PAT A1075 PAT Judge (25 分) Description: The ranklist of PAT is generated from the status lis ...

  6. PAT A1075 PAT Judge (25 分)——结构体初始化,排序

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  7. PAT甲题题解-1075. PAT Judge (25)-排序

    相当于是模拟OJ评测,这里注意最后输出:1.那些所有提交结果都是-1的(即均未通过编译器的),或者从没有一次提交过的用户,不需要输出.2.提交结果为-1的题目,最后输出分数是03.某个题目从没有提交过 ...

  8. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  9. PATA1075 PAT Judge (25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

随机推荐

  1. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  2. NSSet、NSMutableSet基本用法

    NSSet.NSMutableSet基本用法 在Foundation框架中,提供了NSSet类,它是一组单值对象的集合,且NSSet实例中元素是无序,同一个对象只能保存一个. 一.不可变集合NSSet ...

  3. NSArray、NSMutableArray基本用法

    NSArray.NSMutableArray基本用法 一.基本操作 初始化方法:1.init返回一个空数组 2.initWithArray从已有数组初始化 3.initWithContentsOfFi ...

  4. 05-UIKit绘图演练

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  5. unique踢出相同元素

    unique函数的功能是:去除相邻的重复元素(只保留一个). 函数参数:unique(first,last,compare); //first为容器的首迭代器,last为容器的末迭代器,compare ...

  6. 如何设置修改WPS批注上的用户信息名称

    http://jingyan.baidu.com/article/6b18230953cec7ba59e1596b.html 点击左上角的“WPS文字”:   选择“选项”:   点击“用户信息”: ...

  7. CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录

    最近准备学习使用Tez,因此从官网下载了最新的Tez 0.8.3源码,按照安装教程编译使用.平时使用的集群环境是离线的,本打算这一次也进行离线编译,无奈一编译就开始报缺少jar包的错,即使手动下载ja ...

  8. LA3902 Network

    给出一棵树,对于每一个叶子节点要使得在它的k距离内至少一个节点被打了标记,(叶节点不能打标记,非叶结点也不必满足这个条件),现在已经有一个节点s被打了标记,问至少还要打几个标记(这表达能力也是捉急.. ...

  9. jQuery Mask

    <script type="text/javascript" src="/assets/mask/jquery.mask.min.js"></ ...

  10. gulp.spriteSmith使用

    var gulp = require('gulp'); var spritesmith = require('gulp.spritesmith'); gulp.task('sprite', funct ...