10-排序5 PAT Judge
用了冒泡和插入排序 果然没有什么本质区别。。都是运行超时
用库函数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的更多相关文章
- PAT 1075 PAT Judge[比较]
1075 PAT Judge (25 分) The ranklist of PAT is generated from the status list, which shows the scores ...
- 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 ...
- A1075 PAT Judge (25)(25 分)
A1075 PAT Judge (25)(25 分) The ranklist of PAT is generated from the status list, which shows the sc ...
- PAT 甲级 1075 PAT Judge (25分)(较简单,注意细节)
1075 PAT Judge (25分) The ranklist of PAT is generated from the status list, which shows the scores ...
- PAT_A1075#PAT Judge
Source: PAT A1075 PAT Judge (25 分) Description: The ranklist of PAT is generated from the status lis ...
- PAT A1075 PAT Judge (25 分)——结构体初始化,排序
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- PAT甲题题解-1075. PAT Judge (25)-排序
相当于是模拟OJ评测,这里注意最后输出:1.那些所有提交结果都是-1的(即均未通过编译器的),或者从没有一次提交过的用户,不需要输出.2.提交结果为-1的题目,最后输出分数是03.某个题目从没有提交过 ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PATA1075 PAT Judge (25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
随机推荐
- 【LOB】使用USER_LOBS视图获得当前用户包含LOB字段的表
包含LOB类型字段的表往往需要特殊关照,如何快速的获得包含LOB对象的数据库表?使用DBA_LOBS.ALL_LOBS和USER_LOBS视图可以很方便地获得包含BLOB或CLOB字段的表. 简单看一 ...
- 使用U盘在X230上安装Mavericks/Win7-黑苹果之路
新笔记本x230,毫不犹豫继续开始黑苹果之路,这次当然是上最新版本了,谁知道这条道路真是曲折艰难啊,从年前开始,直到前天才算安装成功,还有一堆硬件没驱动上,现记录过程以备以后查看: 1.准备机器.本来 ...
- 【LeetCode】7. Reverse Integer 整型数反转
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...
- jQuery 的随机密码生成 .
$.extend({ password: function (length, special) { var iteration = 0; var password = ""; va ...
- MSP430F149学习之路——按键
代码一: /********************************** 程序功能:用按键控制LED灯熄灭 ***********************************/ #incl ...
- Can't locate Switch.pm in @INC
the perl version (5.14) shipped with 12.10 does not include the Switch.pm module needed while buildi ...
- No.001 Two Sum
Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...
- Android layout属性大全
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 ...
- Mac 平台下功能强大的Shimo软件使用指南
年初自从换了MAC工作站后,彻底享受了Apple产品给我们带来的完美体验,可能是刚转过来不适应,在访问网络设备时觉得远程连接不方便,例如ssh,vpn登陆都不是很方便,后来又安装了openvpnfor ...
- java 进制相互转换
public class test{ public static void main(String[]args){ //十进制转二进制. public static void toBin(int nu ...