那些年踩过的坑: endl耗时比‘\n‘多
- 牛客打比赛做一道题,一直超时,加了c++加速的。结果把endl改成’\n’,就过了,以后打比赛还是用’\n’吧,wa哭了
题目链接
ac代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100010;
struct St
{
string name;
int G,id,se;
}st[N];
struct node
{
int G,id,se;
};
string str;
unordered_map<string,struct node> mp1;
unordered_map<int ,vector<string> > v;
int main()
{
int n;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0; i<n; i++)
{
string str;
int G,se,id;
cin>>str>>G>>se>>id;
mp1[str]={G,id,se};
v[G].push_back(str);
}
int t;
cin>>t;
while(t--)
{
int q;
cin>>q;
if(q==1)
{
string nm;
cin>>nm;
cout<<mp1[nm].G<<" "<<mp1[nm].id<<" "<<mp1[nm].se<<'\n';
}
else
{
int x;
cin>>x;
int L = v[x].size();
if(L)
{
sort(v[x].begin(),v[x].end());
for(int j=0; j<v[x].size(); j++)
{
cout<<v[x][j]<<'\n';
}
}
}
}
return 0;
}
TLE代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100010;
struct St
{
string name;
int G,id,se;
}st[N];
struct node
{
int G,id,se;
};
string str;
unordered_map<string,struct node> mp1;
unordered_map<int ,vector<string> > v;
int main()
{
int n;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0; i<n; i++)
{
string str;
int G,se,id;
cin>>str>>G>>se>>id;
//st[i]={str,G,id,se};
mp1[str]={G,id,se};
v[G].push_back(str);
}
int t;
cin>>t;
while(t--)
{
int q;
cin>>q;
if(q==1)
{
string nm;
cin>>nm;
cout<<mp1[nm].G<<" "<<mp1[nm].id<<" "<<mp1[nm].se<<endl;
}
else
{
int x;
cin>>x;
int L = v[x].size();
if(L)
{
sort(v[x].begin(),v[x].end());
for(int j=0; j<v[x].size(); j++)
{
cout<<v[x][j]<<endl;
}
}
}
}
return 0;
}
两者只有endl与’\n’不一样
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异