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 ...
随机推荐
- XBox360-双光盘游戏自制GOD
一直在找极限竞速4(Forza4),虽然这个版本比较老,但因为带体感.终于下到了,可惜是2个ISO.试着自己做GOD.用到两个软件:Iso2God和Xbox Backup Creator(俗称XBC) ...
- Spring的java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!异常处理方法
使用Spring提供的模板类HibernateDaoSupport,如果单纯的使用'命名参数'的形式编写HQL语句如: public class UserDaoImpl extends Hiberna ...
- Unity AssetBundles and Resources指引 (一)
本文内容主要翻译自下面这篇文章 https://unity3d.com/cn/learn/tutorials/topics/best-practices/guide-assetbundles-and- ...
- vb 取得桌面路径
txtPath.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
- ffmpeg视频格式转换(Java)
命令: 高品质: ffmpeg -i E:\input\a.wmv -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 4 -y E ...
- TCP/IP详解学习笔记(10)-- DNS:域名系统
1.DNS DNS 是计算机域名系统(Domain Name System 或Domain Name Service) 的缩写,它是由解析器以及域名服务器组成的.域名服务器是指保存有该网络中 ...
- Android开发如何去除标题栏title
虽然是一个小问题,今天遇到了,也就写下来吧.防止自己忘掉. 取消标题栏的方式有两种,一种是在代码添加,另一种是在AndroidManifest.xml里面添加. 1.在代码中实现:在此方法setCon ...
- ansible 访问内网服务器
ssh https://medium.com/@paulskarseth/ansible-bastion-host-proxycommand-e6946c945d30#.rauzlfv0z http: ...
- c++ builder ListView实现可编辑任意列(转)
// --------------------------------------------------------------------------- // Form的构造函数中填充StrinG ...
- Linux之文件系统的简单操作
df:列出文件系统整体硬盘使用量 将容量以易读方式显示: [root@zkero ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/s ...