编程竞赛的快速I

编程竞赛的快速I/O

在竞争性编程中,尽快读取输入很重要,这样可以节省宝贵的时间。

您肯定已经看到各种问题陈述:“警告:大型I/O数据,请谨慎使用某些语言(尽管如果算法设计得当,大多数应该可以)”。解决此类问题的关键是使用更快的I/O技术。

通常建议使用scanf / printf代替cin / cout以获得快速的输入和输出。 但是,您仍然可以使用cin / cout并通过在main()函数中包含以下两行来达到与scanf / printf相同的速度:

<code>ios_base::sync_with_stdio(false);/<code>

如果在程序执行其第一个输入或输出操作之前调用它,则将打开或关闭所有C++标准流与其对应的标准C流的同步。添加ios_base :: sync_with_stdio(false); (默认情况下为true)(在任何I / O操作中避免此同步之前)。它是std :: ios_base函数的静态成员。

<code>cin.tie(NULL);/<code>

tie()是一种仅在std :: cin接受输入之前保证刷新std :: cout的方法。这对于交互式控制台程序很有用,因为交互式控制台程序需要不断更新控制台,但同时也会降低大型I / O的速度。NULL部分仅返回NULL指针。

此外,您可以将标准模板库(STL)包含在单个include中:

<code>#include <bits>/<code>

因此,用于竞争性编程的模板可能如下所示:

<code>#include <bits>using namespace std;int main(){    ios_base::sync_with_stdio(false);    cin.tie(NULL);    return 0;}/<bits>/<code>

建议使用cout <

我们可以在问题INTEST – SPOJ(http://www.spoj.com/problems/INTEST/)上的巨大输入测试中测试我们的输入和输出方法。在继续阅读之前,建议您先解决该问题。

C ++ 4.9.2中的解决方案

普通I/O:下面的代码使用cin和cout。 该解决方案的运行时间为2.17秒。

<code>// A normal IO example code #include <bits> using namespace std; int main() {     int n, k, t;     int cnt = 0;     cin >> n >> k;     for (int i=0; i> t;         if (t % k == 0)             cnt++;     }     cout << cnt << "\\n";     return 0; } /<bits>/<code>

快速的I/O,我们可以通过添加两行来做得更好,并大大减少运行时间。 下面的程序运行时间为0.41秒。

<code>// A fast IO program #include <bits> using namespace std;   int main() {     // added the two lines below     ios_base::sync_with_stdio(false);     cin.tie(NULL);              int n, k, t;     int cnt = 0;     cin >> n >> k;     for (int i=0; i> t;         if (t % k == 0)             cnt++;     }     cout << cnt << "\\n";     return 0; } /<bits>/<code>

现在谈论诸如ACM ICPC,Google CodeJam,TopCoder Open之类的竞赛,这是一种以最快的方式读取整数的专有代码。

<code>void fastscan(int &number) {     //variable to indicate sign of input number     bool negative = false;     register int c;       number = 0;       // extract current character from buffer     c = getchar();     if (c=='-')     {         // number is negative         negative = true;           // extract the next character from the buffer         c = getchar();     }       // Keep on extracting characters if they are integers     // i.e ASCII Value lies from '0'(48) to '9' (57)     for (; (c>47 && c<58); c=getchar())         number = number *10 + c - 48;       // if scanned input has a negative sign, negate the     // value of the input number     if (negative)         number *= -1; }   // Function Call int main() {     int number;     fastscan(number);     cout << number << "\\n";     return 0; } /<code> 

getchar_unlocked()用于在C语言中更快地输入以进行竞争性编程。


分享到:


相關文章: