60 分思路
创建一个数组,下标表示队员编号,数据表示得分,每次只要从 到 遍历一边找出符合的分数输出即可,复杂度为 ,可以跑过前 的数据。
60 分代码
#include <iostream>
using namespace std;
int a[10005];
int main()
{
int t;
cin >> t;
while(t--){
int op;
cin >> op;
if(op == 1){
int x, y;
cin >> x >> y;
if(a[x] < y){//判断分数是否比之前的记录大
a[x] = y;
}
}
else{
int q, cnt = 0;
cin >> q;
for(int i = 1; i <= 10000; i++){//遍历一次
if(a[i] == q){
cout << i << " ";
cnt++;
}
}
if(cnt == 0){
cout << -1;
}
cout << endl;
}
}
return 0;
}100 分思路
当我们看 的数据范围时,我们会发现 变大到了 ,按照上面的方法,需要跑 次,一秒明显跑不完,所以我们需要开一个结构体,输入一个新的数据时,我们要从 到 遍历一遍结构体,而 就是表示现在输入了几个不同的编号,如果找到了,也就是说之前出现过这个编号,那么直接判断修改最高分数,否则 要加一,然后新建一个下标存进去,当我们要找一个数的时候,就直接从 到 遍历一遍,照出符合的分数,输出编号,注意不是下标。时间复杂度为 ,就可以通过 的数据。
100 分代码
#include <iostream>
using namespace std;
struct A{
int num, se;
} a[500005];
int main()
{
int t, ti = 1;
cin >> t;
while(t--){
int op;
cin >> op;
if(op == 1){
int x, y;
bool if_c = false;
cin >> x >> y;
for(int i = 1; i <= ti; i++){
if(a[i].num == x){
if_c = true;
if(a[i].se < y){
a[i].se = y;
}
break;
}
}
if(!if_c){
a[ti].num = x;
a[ti].se = y;
ti++;
}
}
else{
int q, cnt = 0;
cin >> q;
for(int i = 1; i <= ti; i++){
if(a[i].se == q){
cout << a[i].num << " ";
cnt++;
}
}
if(cnt == 0){
cout << -1;
}
cout << endl;
}
}
return 0;
}