在做这题时 用cin cout的话 耗时5秒以上。
换成scanf和printf时 耗时立马降到4以下。。
好像cin cout 有个什么同步开关 关掉后 速度就会提升 见后面
#include <iostream>
#include <list>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
class mylist
{
public:
list<int> lst;
};
int main()
{
char cmd[10];
int n,tem;
mylist lst[10001];
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d",&n);
while(n--)
{
scanf("%s",cmd);
if(strcmp(cmd,"new") == 0)
{
scanf("%d",&tem);
}
else if(strcmp(cmd,"add") == 0)
{
int id,num;
scanf("%d%d",&id,&num);
lst[id].lst.push_back(num);
lst[id].lst.sort();
}
else if(strcmp(cmd,"merge") == 0)
{
int id1,id2;
scanf("%d%d",&id1,&id2);
lst[id1].lst.merge(lst[id2].lst);
}
else if(strcmp(cmd,"unique") == 0)
{
int id;
scanf("%d",&id);
lst[id].lst.unique();
}
else if(strcmp(cmd,"out") == 0)
{
int id;
scanf("%d",&id);
list<int>::iterator i;
for(i = lst[id].lst.begin(); i != lst[id].lst.end(); ++i)
cout<<*i<<" ";
//printf("%d ",*i);
printf("\n");
}
}
return 0;
}
好吧 原来list可以直接定义一个数组的 我想多了
#include <iostream>
#include <list>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
std::ios::sync_with_stdio(false);
//class mylist
//{
//public:
// list<int> lst;
//};
int main()
{
char cmd[10];
int n,tem;
// mylist lst[10001];
list<int> lst[10001];
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d",&n);
while(n--)
{
scanf("%s",cmd);
if(strcmp(cmd,"new") == 0)
{
scanf("%d",&tem);
}
else if(strcmp(cmd,"add") == 0)
{
int id,num;
scanf("%d%d",&id,&num);
// lst[id].lst.push_back(num);
// lst[id].lst.sort();
lst[id].push_back(num);
lst[id].sort();
}
else if(strcmp(cmd,"merge") == 0)
{
int id1,id2;
scanf("%d%d",&id1,&id2);
// lst[id1].lst.merge(lst[id2].lst);
lst[id1].merge(lst[id2]);
}
else if(strcmp(cmd,"unique") == 0)
{
int id;
scanf("%d",&id);
//lst[id].lst.unique();
lst[id].unique();
}
else if(strcmp(cmd,"out") == 0)
{
int id;
scanf("%d",&id);
list<int>::iterator i;
for(i = lst[id].begin(); i != lst[id].end(); ++i)
//cout<<*i<<" ";
printf("%d ",*i);
printf("\n");
}
}
return 0;
}
这个程序测试通过的时间是
3424kB | 1210ms | 1571 B | G++ |
3420kB | 2750ms | 1116 B | G++ |
std::ios::sync_with_stdio(false);
3420kB | 2000ms | 1154 B | G++ |
std::cin.sync_with_stdio(false);
std::cout.sync_with_stdio(false);
3420kB | 2040ms | 1236 B | G++ |
关于cin,cout与scanf,printf的速度到底相差多少看这篇文章和这篇还有这篇
#include <iostream>
#include <list>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
string cmd;
int n;
list<int> lst[10001];
// std::ios::sync_with_stdio(false);
std::cin.sync_with_stdio(false);
std::cout.sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
cin>>n;
while(n--)
{
cin>>cmd;
if(cmd == "new")
{
int tem;
cin>>tem;
}
else if(cmd == "add")
{
int id,num;
cin>>id>>num;
lst[id].push_back(num);
}
else if(cmd == "merge")
{
int id1,id2;
cin>>id1>>id2;
lst[id1].merge(lst[id2]);
}
else if(cmd == "unique")
{
int id;
cin>>id;
lst[id].sort();
lst[id].unique();
}
else if(cmd == "out")
{
int id;
cin>>id;
list<int>::iterator i;
lst[id].sort();
for(i = lst[id].begin(); i != lst[id].end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
}
return 0;
}
FC [/A] [/C] [/L] [/LBn] [/N] [/OFF[LINE]] [/T] [/U] [/W] [/nnnn]
[drive1:][path1]filename1 [drive2:][path2]filename2
FC /B [drive1:][path1]filename1 [drive2:][path2]filename2
/A 只显示每个不同处的第一行和最后一行。
/B 执行二进制比较。
/C 不分大小写。
/L 将文件作为 ASCII 文字比较。
/LBn 将连续不匹配的最大值设置为指定
的行数。
/N 在 ASCII 比较上显示行数。
/OFF[LINE] 不要跳过带有脱机属性集的文件。
/T 不要将制表符扩充到空格。
/U 将文件作为 UNICODE 文本文件比较。
/W 为了比较而压缩空白(制表符和空格)。
/nnnn 指定不匹配处后必须连续
匹配的行数。
[drive1:][path1]filename1
指定要比较的第一个文件或第一个文件集。
[drive2:][path2]filename2
指定要比较的第二个文件或第二个文件集。
1.在OJ上下载in.txt和out.txt, 然后假如你编译好的程序是a.exe
2.把in.txt,out.txt和a.exe放入同一个文件夹(或桌面)
3.在该文件夹的空白处按shift+右键,选择在此处打开命令窗口
4.在命令窗口里运行a.exe,并重定向输入输出(即在cmd里输入a.exe <in.txt >out1.txt,out1.txt为你要保存输出结果的文本)
5.在cmd里输入fc /a /n out.txt out1.txt,fc命令是比较两个文件的差异并输出到屏幕
> 清空文件在写
>>在文件后面追加