题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1075
/*
* 最后一个测试点错误原因:对成绩初始化时,错误。。。见具体代码处解释
*
* 需要解决的几个问题:
* 1. student应包含哪些成员,什么类型
* 2. 如何实现排序
* 3. 输入的成绩如何映射到student上,显然Hash。
* 本次使用vector,先将n个初始化的student入。
* 见其他人有使用数组
* 4. 输出控制,不合条件者不能输出,此处使用flag。
* 5. 某项成绩为-1,输出0
* 6. 如何实现,信息的更新
*/
#include <stdio.h>
#include <vector>
#include <algorithm>
#define MAX_N 10000+10
#define MAX_K 5+1
#define MAX_M 100000+10
#define NEVER -10 // 任意一个不同于[-1, max_score]的数
#define INF 1<<30
using namespace std;
struct Stu
{
int id;
int s[MAX_K];
int total;
int ac; // ac的题目数
bool flag; // 是否可显示
int final;
};
vector<Stu> student;
int problem[MAX_K];
int n, k, m;
bool cmp(Stu a, Stu b)
{
if(a.total != b.total)
return a.total > b.total;
else if(a.ac != b.ac)
return a.ac > b.ac;
else
return a.id < b.id;
}
void init()
{
Stu t;
for(int i=0; i<= n; i++)
{
t.ac = 0;
t.flag = false;
t.id = i;
for(int j=1; j<=k; j++)
{
t.s[j] = NEVER;
}
// t.s[1] = t.s[2] = t.s[3] = t.s[4] = NEVER;
// SB, 你怎么知道4道题目的
// 最奇怪的是,仅有最后一个测试点的题目数超过了4个,即5道题目
t.total = NEVER;
student.push_back(t);
}
}
void refresh(int id)
{
student[id].ac = student[id].total=0;
for(int i=1; i<=k; i++)
{
if(student[id].s[i] >= 0)
{
student[id].total += student[id].s[i];
}
if(student[id].s[i] == problem[i])
{
student[id].ac++;
}
}
return ;
}
void sort_output()
{
sort(student.begin()+1, student.end(), cmp);
int i;
int pre_total = -100;
// set rank
for(i=1; i<= n; i++)
{
if(pre_total == student[i].total)
{
student[i].final = student[i-1].final;
}
else
{
student[i].final = i;
pre_total = student[i].total;
}
// output
if(student[i].flag == true)
{
printf("%d %05d %d", student[i].final, student[i].id, student[i].total);
int j;
for(j=1; j<=k; j++)
{
if(student[i].s[j] != NEVER)
{
if(student[i].s[j] == -1)
{
student[i].s[j] = 0; //没有-1分
}
printf(" %d", student[i].s[j]);
}
else
{
printf(" -");
}
}
printf("\n");
}
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r", stdin);
#endif
scanf("%d%d%d", &n, &k, &m);
init();
int i;
for(i=1; i<=k; i++)
{
scanf("%d", &problem[i]);
}
// input submit and processing it
while(m-- > 0)
{
int user_id, problem_id, score;
scanf("%d%d%d", &user_id, &problem_id, &score);
// processing
if(student[user_id].s[problem_id] < score)
{
student[user_id].s[problem_id] = score;
if(score >= 0 && student[user_id].flag == false)
{
student[user_id].flag = true;
}
refresh(user_id); // 更新其他信息:total,ac
}
}
// sorting
sort_output();
return 0;
}