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

用库函数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. python3 内置函数 filter()

    filter(function or None, iterable) --> filter object Return an iterator yielding those items of i ...

  2. mplayer 用法大全 转

    1,录音:mplayer       mms://202.***.***.***/test.asf     -dumpstream     -dumpfile  MyMovie.asf 可以把mms ...

  3. Android——学习笔记

    1.this注意 @Override            public void onItemSelected(AdapterView<?> parent, View view,     ...

  4. js的传值,table中tr的遍历,js中动态创建数组

    1.这里关键是对页面中的传值,其次是动态的创建一个数组,用来存值 $(val).css("background-color", "rgb(251, 248, 233)&q ...

  5. C/C++程序员面试易错题

    c部分::::::::::::::::::::::::::::::::::: . 关键字volatile有什么含意? 并给出三个不同的例 子. [参考答案]一个定义为volatile的变量是说这变量可 ...

  6. 《FPGA全程进阶---实战演练》第二十一章 电源常用类型:LDO和 DCDC

    高速电路中的电源设计 高速电路中的电源设计大概分为两种,一种是集总式架构,一种是分布式架构.集总式架构就是由一个电源输入,然后生成多种所需要的电压.如图1所示.这种架构会增加多个DC/DC模块,这样成 ...

  7. zepto的clone方法于textarea和select的bug修复

    (function($) { Zepto.fn.clone = function() { var result = $.fn.clone.apply(this, arguments), oldText ...

  8. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  9. dom4j操作xml对象

         // 获取Documen对象      public static Document getDocument(String path) throws Exception{           ...

  10. uitableviewcell高度自适应笔记

    今天看了几篇uitableviewcell高度自适应的文章,大体分为两种方式. 第一种方式,cell里面有label,在cellforrow绘制的时候计算Label的可能高度,并且在此时重新计算cel ...