Beginner ACM-Combinatorial Mathematics basic topic PKU 1833

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1833

Test instructions is very clear, is to find out the current arrangement of the first K-permutation.

Easily, you can use the STL's Next_permulation () function to write an answer:

#include  <iostream> #include  <cstdio> #include  <algorithm>using namespace  std;int data[1025];int main () {    int n,k,m;     scanf ("%d", &m);     while (m--)     {      &NBSP;&NBSP;&NBSP;SCANF ("%d%d", &n,&k);         for (int i=0 ;  i<n; i++)         {       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &data[i]);         }         while (k--)              next_permutation (data,data+n);         for (int  i=0; i<n; i++)         {             printf ("%d ", Data[i]);        }         putchar (' \ n ');    }     return 0;}

However submitted, will prompt timeout ...

Some can not touch the mind, because the online AC code is basically also called K-times next_permulation write.

And then saw this article:Poj 1833 arrangement--a messy water problem

Only to understand that the original number of calls to the printf function also times out:

So, just copy it to the output buffer at once.

#include  <iostream> #include  <cstdio> #include  <iterator> #include  < Algorithm>using namespace std;int data[1025];int main () {    int &NBSP;N,K,M;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &m);     while (m--)     &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d%d", &n,&k);         for (int i=0; i<n; i++)         &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &data[i]);         }        while (k--)              next_permutation (data,data+n);         copy (data,data+n-1,ostream_iterator<int> (cout, " "));         cout<<data[n-1]<<endl;    }    return 0;} 

At this point AC, only 500ms, indicating that the call to printf at least 500ms of time consumed. But the same code, the use of C + + mode of submission ran 985ms, almost the line ran out.

Run ID User problem Result Memory time Language Code Length Submit time

14813838 Arclabs001 1833 Accepted 696K 500MS g++ /c14>477b 2015-10-14 17:41:22

14813836 Arclabs001 1833 Accepted 204K 985MS C + + 477B 2015-10-14 17:40:55


Beginner ACM-Combinatorial Mathematics basic topic PKU 1833

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.