您的位置:首页 > 其它

POJ 1833 排序 解题报告

2011-01-09 22:21 316 查看
晕。没想到用cin,cout的效率这么低,而一定要改用scanf和printf。另外,还一定要用C++而不能用G++才不会TLE。

用C++ STL的next_permutation很简单。



#include <algorithm>
#include <cstdio>

using namespace std;

/*通过int数组读入序列,而不使用string。
 *因为,如果遇到两位数,三位数,则很难将它们
 *看成一个整体了。
 */
int arr[1024];

int main()
{
    int nCases;
    scanf("%d", &nCases);
    for(int i = 0; i < nCases; i++) {
        int n, k;
        scanf("%d%d", &n, &k);

        for(int j = 0; j < n; j++) {
            scanf("%d", &arr[j]);
        }

        int count = 0;
        while(count != k) {
            /*next_permutation实在强大,它也可以取int型数组的下一个排列。
             *若到达了最后一个,则自动返回第一个元素。
             */
            next_permutation(arr, arr + n);
            count++;
        }

        for(int j = 0; j < n; j++) {
            printf("%d ", arr[j]);
        }
        printf("/n");
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: