YT14-HDU-找循环节 (关于std::ios::sync_with_stdio(false);的作用和疑问)

Problem Description

As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in the decomposition of permutations.
A permutation of a set S = {1, 2, ..., n} is a bijection from S to itself. In the great magician —— Cauchy‘s two-line notation, one lists the elements of set S in the first row, and then for each element, writes its image under the permutation below it in
the second row. For instance, a permutation of set {1, 2, 3, 4, 5} σ can be written as:

Here σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3, and σ(5) = 1.

Twilight Sparkle is going to decompose the permutation into some disjoint cycles. For instance, the above permutation can be rewritten as:

Help Twilight Sparkle find the lexicographic smallest solution. (Only considering numbers).

Input

Input contains multiple test cases (less than 10). For each test case, the first line contains one number n (1<=n<=10^5). The second line contains n numbers which the i-th of them(start from 1) is σ(i).

Output

For each case, output the corresponding result.

Sample Input

5
2 5 4 3 1
3
1 2 3

Sample Output

(1 2 5)(3 4)
(1)(2)(3)

原代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
const int NUM=100000;
using namespace std;
int main()
{
    int i,j,n,m;
    int a[NUM];
    while(cin>>n)
    {
        for(i=1; i<=n; i++)
            cin>>a[i];
        for(i=1; i<=n; i++)
        {
            while(a[i])
            {
                cout<<"("<<i;
                j=a[i];
                a[i]=0;
                while(a[j])
                {
                    cout<<" "<<j;
                    m=a[j];
                    a[j]=0;
                    j=m;
                }
                cout<<")";
            }
        }
        cout<<endl;
    }
    return 0;
}

AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
const int NUM=100000;
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    int i,j,n,m;
    int a[NUM];
    while(cin>>n)
    {
        for(i=1; i<=n; i++)
            cin>>a[i];
        for(i=1; i<=n; i++)
        {
            while(a[i])
            {
                cout<<"("<<i;
                j=a[i];
                a[i]=0;
                while(a[j])
                {
                    cout<<" "<<j;
                    m=a[j];
                    a[j]=0;
                    j=m;
                }
                cout<<")";
            }
        }
        cout<<endl;
    }
    return 0;
}

运行结果:

提交自己的代码之后经常是Time Limit Exceeded。

看网上找代码的时候别人用的通常都是C语言的scanf和printf输入输出,一直以为那是C语言的代码,但这次偶然看到了同学的代码(和我“抄”得居然是同一个。。。老师教导我们要”抄“之有道)但其用了


于是百度了一下:

cin,cout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低,而这段语句可以来打消iostream的输入输出缓存,可以节省许多时间,使效率与scanf与printf相差无几。

又学到了一手,最近做题总算出现时间超限,以后就不用担心了。不过不知道

std::ios::sync_with_stdio(false);

有什么弊端,

时间: 2024-07-30 16:06:16

YT14-HDU-找循环节 (关于std::ios::sync_with_stdio(false);的作用和疑问)的相关文章

杭电ACM 找循环节 std::ios::sync_with_stdio(false);

Problem Description As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in th

std::ios::sync_with_stdio和tie()——给cin加速

平时在Leetcode上刷题的时候,总能看到有一些题中最快的代码都有这样一段 static const auto init = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return nullptr; }(); 有时候偏偏算法是一样的,但是速度要比没有这段代码的快很多: 查了一下这段代码其实是给cin加速的,也就是说上面提到的题应该是碰到的大数据的输入,而cin cout要比scanf  printf慢很多,很

关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流

原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.html http://www.clanfei.com/2012/03/235.html 在网上查看别人的ACM代码时,发现别人输入输出语句用的总是scanf与printf,有点不解,还以为他们用的都是C语言,而非C++,但今天做的一道题(Sort): 发现与网上的其他高手使用完全相同的方法,使用sca

Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩溃的.(吐血......) 可是仔细观察,它指数大,可是mod小啊,它吓人,可是可以暴力搞啊!! 这个题目一个难点就是要向下取整求余,详解见传送门,本题是向下取整,也就是向上取整加一. 还有就是指数太大,要找到循环节,其实由于mod小,循环节并没有太大,暴力跑就ok啦!  此刻内心是崩溃的 1 #i

特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数学知识点:特征根法求递推数列通项公式. 方法是这样的: 对于这题的解法: 记λ1=5+2√6,λ2=5-2√6,则λ1λ2=1,λ1+λ2=10 根据韦达定理可以推导出:λ1,λ2的特征方程为 x^2-10x+1=0 再使用该特征方程反向推导出递推公式为:a[n]=10*a[n-1]-a[n-2] 再由特征根

2016&quot;百度之星&quot; - 初赛(Astar Round2A)1001 All X(HDU5690)——找循环节|快速幂

一个由m个数字x组成的新数字,问其能否mod k等于c. 先提供第一种思路,找循环节.因为每次多一位数都是进行(t*10+x)mod k(这里是同余模的体现),因为x,k都确定,只要t再一样得到的答案一定一样.所以在一步一步中进行时一旦出现了一个之前出现过的数字,那么很显然后面就要开始进行循环了.找出这个循环节,然后把m放到这个循环节里头就行(这里的说法有问题,见下文的第二点). 但是这里有两个要注意的地方,第一是只有当前出现的数一样才能保证下一个出现的数一样,而并不代表着,当前的数一样,得到它

hdu 3054 Fibonacci 找循环节的公式题

Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem Description We know the Fibonacci Sequence F1=1,F2=1,F3=2,F4=3,F5=5, ... Fx = Fx-1+Fx-2 We want to know the Mth number which has K consecutive "0&qu

hdu 5690(同余定理找循环节 / 快速幂)

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define lld I64d 5 #ifdef _WIN32 6 #define LLD "%I64d" 7 #else 8 #define LLD "%lld" 9 #endif 10 const int N = 10000 + 100; 11 ll x,m,c,k; 12 int pos[N]; 1

hud5451_求循环节加矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5451 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 找循环节解析链接:http://blog.csdn.net/ACdreamers/article/details/25616461 裸题链接:http://blog.csdn.net/chenzhenyu123456/article/details/48529039 1 #include <algorithm> 2 #i