yt14-hdu-Find the Loop section (about Std::ios::sync_with_stdio (false); the role and questions)

Source: Internet
Author: User

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 are 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 E Lement, writes its image under the permutation below it in the second row. For instance, a permutation of set {1, 2, 3, 4, 5}σcan is 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 is rewritten as:


Help Twilight Sparkle Find the lexicographic Smallest solution. (only considering numbers).Inputinput 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). Outputfor each case, output the corresponding result. Sample Input
52 5 4 3 131 2 3
Sample Output
(1 2 5) (3 4) (1) (2) (3)

The original code is as follows:

#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;}

The AC code is as follows:

#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;}

Operation Result:



It is often time Limit exceeded after submitting your own code .

Looking at the code on the Internet when others use is usually C language scanf and printf input and output, always thought that is the C language code, but this happened to see the classmate's code (and I "Copy" is incredibly the same ...) The teacher taught us to "copy" the proper way) but it used

So Baidu a bit:

Cin,cout is inefficient because the first thing to output to the buffer, and then output, resulting in reduced efficiency, and this statement can be used to eliminate the iostream input and output cache, can save a lot of time, so that efficiency and scanf and printf are comparable.

Also learned a hand, recently do the problem finally appeared time overrun, later do not have to worry about. But I don't know.

Std::ios::sync_with_stdio (FALSE);

What are the drawbacks,


yt14-hdu-Find the Loop section (about Std::ios::sync_with_stdio (false); the role and questions)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.