那些年踩过的坑: 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’不一样

posted @   翔村亲亲鸟  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示