小编Rub*_*nks的帖子

慢 printf:为什么 __USE_MINGW_ANSI_STDIO=0 使它更快?

我为 codeforces 中的一个问题编写了代码,尽管我相信我是在最佳时间复杂度下完成的,但它超出了第七个测试用例的时间限制。经过一些测试后,在我看来,大部分时间都被 printf 占用了,这似乎很奇怪,因为使用 printf 大约 3 * 10^5 次应该不是什么大问题。所以我搜索了很多,发现了这个:https://codeforces.com/blog/entry/105687#comment-940911
现在我得出的结论是,在代码顶部使用这一行将使 printf 更快:

#define __USE_MINGW_ANSI_STDIO 0
Run Code Online (Sandbox Code Playgroud)

因此,我运行了包含上述内容的代码,瞧,现在只包含一行代码就超过了 1 秒的时间限制,仅用了 62 毫秒就被接受了。我不理解链接中讨论的大部分其他内容,例如 MinGW 实现等。所以我的问题是,首先为什么它会这样工作?其次,从现在开始,我可以/应该在我在 codeforces 上的所有程序中继续使用上述代码行吗?

PS我还发现了这个博客:https://codeforces.com/blog/entry/47180 这对我来说太混乱了,暂时无法理解,但也许其他人可以理解它并阐明这个问题。

另外,这是codeforces问题:https://codeforces.com/contest/1774/problem/C 这是我的解决方案: https://codeforces.com/contest/1774/submission/185781891

我不知道整个输入,因为 codeforces 不共享它,而且它会非常非常大。但我知道输入到tests变量的值为3,输入到n[0]、n[1]、n[2]的值为100000、100000、100000

这是我的代码:

#define __USE_MINGW_ANSI_STDIO 0
#include <stdio.h>
#include <stdlib.h>
// #include <math.h>
// #include <string.h>

// #define lint long long int

// Function Declarations

int main(void)
{
    int tests;
    scanf("%i", &tests);
    int **answers = malloc(tests * sizeof(int*));
    int *n = malloc(sizeof(int) * tests);
    for …
Run Code Online (Sandbox Code Playgroud)

c performance printf

10
推荐指数
1
解决办法
818
查看次数

为什么向量比较结果不加括号时显示错误?

在下面的代码中,我遇到了很多错误。当我使用 cout << (v1 > v2) << endl 代替时,所有错误都消失了,我得到了正确的输出。为什么会这样呢?这是我收到的错误的第一部分(有很多错误,但如果需要,我将发布所有错误):

abc.cpp: In function 'int main()':

abc.cpp:18:10: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::vector<int>') 
Run Code Online (Sandbox Code Playgroud)

我的代码是:

/* #include <bits/stdc++.h> */

#include <iostream>
#include <algorithm>
#include <vector>

//using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::sort;

int main(void)
{
    vector <int> v1 = {1, 2, 3};
    vector <int> v2 = {1, 2, 5};
    cout << v1 > v2 << endl;

    // free variables
    return …
Run Code Online (Sandbox Code Playgroud)

c++ vector

0
推荐指数
1
解决办法
72
查看次数

标签 统计

c ×1

c++ ×1

performance ×1

printf ×1

vector ×1