C++ 提高cpp中竞争编码的i/o速度

C++ 提高cpp中竞争编码的i/o速度,c++,performance,input,io,C++,Performance,Input,Io,我不熟悉竞争性编程。我在解决这个问题()时提到这个问题需要快速的输入输出。我测试了我的函数f的时间,它用了1微秒,I/o用了44505微秒。因此,我认为I/O是瓶颈 我的解决办法如下 int fastscan() { int x{}; bool neg = false; register int c; x = 0; c = char(); // for(;(c<48||c>57);c=char()); if (c == '-')

我不熟悉竞争性编程。我在解决这个问题()时提到这个问题需要快速的输入输出。我测试了我的函数f的时间,它用了1微秒,I/o用了44505微秒。因此,我认为I/O是瓶颈

我的解决办法如下

int fastscan()
{
    int x{};
    bool neg = false;
    register int c;
    x = 0;
    c = char();
    // for(;(c<48||c>57);c=char());
    if (c == '-') {
        neg = true;
        c = char();
    }
    for (; (c > 47 && c < 58); c = char())
        x = (x << 1) + (x << 3) + c - 48;
    if (neg)
        x *= -1;
    return x;
}

long long unsigned int f(array<int, 10005>& arr, long long int max, long long int cur, long long int n)
{
    if (cur + n >= max)
        return arr[n];
    long long int count{ 0 }, count1{ 0 };
    count = f(arr, max, cur + 1, n + cur);
    count1 = f(arr, max, cur + 1, n + cur + 1);
    count = count > count1 ? count : count1;
    return count + arr[n];
}

int main(int argc, char const* argv[])
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    long long int t{};
    std::cin >> t;
    int n{};
    array<int, 10005> arr;
    while (t--) {
        n = fastscan();
        int temp = n * (n + 1) / 2;
        int max = temp;
        while (temp--) {
            arr[max - temp - 1] = fastscan();
        }
        return 0;
        long long int c{};
        c = f(arr, max, 1, 0);
        std::cout << c << '\n';
    }
    return 0;
}
#define mod 1000000007
#define ll long long
#define sc(x) scanf("%d",&x)
#define scll(x) scanf("%lld",&x)
#define pr(x) printf("%d\n",x)
#define prll(x) printf("%lld\n",x)
#define gc getchar_unlocked
#define pc putchar_unlocked    
inline int scan()
    {
        register int c = gc();
        int x = 0;
        for (; (c < 48 || c > 57); c = gc())
            ;
        for (; (c > 47 && c < 58); c = gc()) {
            x = (x << 1) + (x << 3) + c - 48;
        }
        return x;
    }
    
    inline void print(int a)
    {
        char snum[20];
        int i = 0;
        do {
            snum[i++] = a % 10 + 48;
            a = a / 10;
        } while (a != 0);
        i = i - 1;
        while (i >= 0)
            pc(snum[i--]);
        pc('\n');
    }
    int a[100][100];
    int sum(int n)
    {
        int i, j;
        for (i = 0; i < n; i++) {
            for (j = 0; j <= i; j++) {
                a[i][j] = scan();
            }
        }
        for (i = 1; i < n; i++) {
            for (j = 0; j <= i; j++) {
                if (j == 0)
                    a[i][j] += a[i - 1][j];
                else if (j == i)
                    a[i][j] += a[i - 1][j - 1];
                else {
                    a[i][j] += max(a[i - 1][j], a[i - 1][j - 1]);
                }
            }
        }
        int mm = 0;
        for (j = 0; j < n; j++) {
            if (a[n - 1][j] > mm)
                mm = a[n - 1][j];
        }
    
        return mm;
    }
    
    int main()
    {
        int t, n;
        t = scan();
        while (t--) {
    
            n = scan();
            print(sum(n));
        }
    }
int fastscan()
{
int x{};
布尔负=假;
寄存器INTC;
x=0;
c=char();
//对于(;(c57);c=char());
如果(c=='-'){
负=真;
c=char();
}
对于(;(c>47&&c<58);c=char())
x=(x count1?计数:count1;
返回计数+arr[n];
}
int main(int argc,char const*argv[]
{
ios::将_与_stdio同步(0);
cin.tie(空);
长整型t{};
标准:cin>>t;
int n{};
阵列arr;
而(t--){
n=快速扫描();
内部温度=n*(n+1)/2;
int max=温度;
while(临时--){
arr[max-temp-1]=fastscan();
}
返回0;
长整型c{};
c=f(arr,max,1,0);
std::cout 47&&c<58);c=gc(){

x=(x
fastscan
始终返回相同的值,并且
返回0
会导致外部while循环只执行一次?考虑到您的代码与接受的代码之间存在明显的算法差异(只需将
f
函数与接受的
sum
函数进行比较即可)为什么你认为问题出在I/O速度上?竞争性的编码网站通常是选择最好的算法,而不是最大化I/O速度。@AlanBirtles虽然可怕,但我猜
char()
实际上是一个字符读取宏(就像另一个代码中的
gc()
)。如果您需要调整I/O以使代码足够快,那么几乎总会有一种算法在不这样做的情况下速度更快。令人高兴的是,公认的解决方案同时使用了
寄存器
内联
,这两种方法几十年来都没有对执行时间产生任何影响。这是纯粹的货物崇拜编程.