踩的坑
这题不是用lua写的,因为lua必超时(有大哥能不超时麻烦分享一下代码谢谢)
我翻了翻FAQ,看到了这样一句话:
不同语言的时间限制和内存限制是相同的吗?
是相同的,我们认为选择合适的编程语言也是一项必备技能,所以没有为不同语言设置不同的限制条件。
…fine
(C++输出固定小数位:setiosflags(ios::fixed)<<setprecision(n))
我总结了一下,一般限制在400ms的都比较安全,可能没有卡时间。低于400ms就要小心了,尤其是题目看起来逻辑简单但NK的范围不小的时候。
最后id开头可能是0,老坑点了。
C++代码
C++随便写写都只要180ms,猛男落泪,C的map是世界上最好的map。
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct info
{
long int grade, total_rank, come_from, local_rank;
string id;
};
int main()
{
int N, total_student = 0;
cin >> N;
map<string, info> infos;
for(int Ni = 1; Ni <= N; Ni ++)
{
int K;
cin >> K;
total_student += K;
map<string, info> local_infos;
for (int Ki = 0; Ki < K; Ki++)
{
info newinfo;
cin >> newinfo.id >> newinfo.grade;
newinfo.come_from = Ni;
string key = to_string(100 - newinfo.grade) + newinfo.id;
if (100 - newinfo.grade < 100)
key = "0" + key;
if (100 - newinfo.grade < 10)
key = "0" + key;
local_infos[key] = newinfo;
infos[key] = newinfo;
}
int last_rank = 1, grade = -1, current_rank = 1;
for(auto& it: local_infos)
{
if (it.second.grade == grade)
infos[it.first].local_rank = last_rank;
else
{
infos[it.first].local_rank = current_rank;
last_rank = current_rank;
grade = it.second.grade;
}
current_rank ++;
}
}
int last_rank = 1, grade = -1, current_rank = 1;
cout<<total_student<<endl;
for(auto& it: infos)
{
if (it.second.grade == grade)
it.second.total_rank = last_rank;
else
{
it.second.total_rank = current_rank;
last_rank = current_rank;
grade = it.second.grade;
}
current_rank ++;
cout<<it.second.id<<" "<<it.second.total_rank<<" "<<it.second.come_from<<" "<<it.second.local_rank<<endl;
}
}
超时的lua代码
--IO
local IOfunc = io.read('*a'):gmatch('%w+')
local function readNumber()
return tonumber(IOfunc())
end
--para
local score_to_info = {}
--{id = NUMBER, score = NUMBER, total_rank = NUMBER, come_from = NUMBER, location_rank = NUMBER}
setmetatable(score_to_info, {__index = function(self, k) self[k] = {} return self[k] end})
local N = readNumber()
local student_count = 0
for index = 1, N do
local K = readNumber()
student_count = student_count + K
for _ = 1, K do
local id = readNumber() score = readNumber()
local rank = {
id = id,
score = score,
come_from = index
}
table.insert(score_to_info[score], rank)
end
end
print(student_count)
local current_rank = 1
local location_rank = {}
setmetatable(location_rank, {__index = function() return 1 end})
for score = 100, 0, -1 do
if #score_to_info[score] > 0 then
table.sort(score_to_info[score], function (a, b) return a.id < b.id end)
local rank = {}
setmetatable(rank, {__index = function() return 0 end})
for _, v in pairs(score_to_info[score]) do
print(v.id .. " " .. current_rank .. " " .. v.come_from .. " " .. location_rank[v.come_from])
rank[v.come_from] = rank[v.come_from] + 1
end
current_rank = current_rank + #score_to_info[score]
for k, v in pairs(rank) do
location_rank[k] = location_rank[k] + v
end
end
end