CCF 消息传递接口
本来很简单的一道题,愣是被我弄复杂了,半天弄不出来。
这不就是用个队列吗······有什么难的?
这里面有一个重要的事情就是关于C++输入输出的效率问题。
std::ios::sync_with_stdio(false);
在c++中之所以cin,cout效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入和输出缓存,可节省时间,使效率与scanf与printf相差无几,还有应注意的是scanf与printf使用的头文件应是stdio.h而不是 iostream。
tie 函数
tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。
在ACM里,经常出现 数据集超大造成 cin TLE的情况。我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要同时混用cout和printf 之类。
在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
#include <iostream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
return 0;
}
特别值得注意的是,VS2019同时可以使用getchar()和getchar()读入回车,而devC++只能使用cin.get()
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <sstream>
#define MAX 10001
#define INF 0x3f3f3f3f
using namespace std;
int m, n;
struct Order {
char type;
int num;
};
queue<Order>orders[MAX];
int toNum(const string& in)
{
int ans = 0;
for (auto c : in)
{
ans = ans * 10 + c - '0';
}
return ans;
}
int main()
{
/
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("sb.txt", "r", stdin);
cin >> m >> n; getchar();
string str;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
string t;
getline(cin, t);
//cout << t << endl;
stringstream c(t);
string str;
while (c >> str)
{
// cout << str;
orders[j].push({ str[0],toNum(str.substr(1)) });
}
}
bool flag = true;
while (flag)
{
flag = false;
for (int i = 0; i < n; i++)
{
if (orders[i].empty())
continue;
Order top = orders[i].front();
if (!orders[top.num].empty())
{
Order top1 = orders[top.num].front();
if (top1.num == i && (top1.type+top.type)=='R'+'S')
{
orders[top.num].pop();
orders[i].pop();
flag = true;
}
}
}
}
int dead = false;
for (int k = 0; k < n; k++)
{
if (!orders[k].empty())
dead = true;
orders[k] = queue<Order>();//注意要清空,queue没有提供clear函数
}
cout <<dead<<endl;
}
return 0;
}
满分代码