那些年踩过的坑: 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 @ 2022-08-28 08:44  翔村亲亲鸟  阅读(30)  评论(0编辑  收藏  举报