题解

132 条题解

  • -1
    @ 2016-11-07 16:26:05
    var a:array[0..10000] of longint;
        i,n,m,j,p:longint;
        ans:qword;
        
        procedure swap(var a,b:longint);
        var t:longint;
        begin
            t:=a;a:=b;b:=t;
        end;
        
        procedure kuai(l,r:longint);
    var i,j,x,y:longint;
    begin
    i:=l;j:=r;
    x:=a[(l+r) div 2];
    repeat
    while a[i]<x do inc(i);
    while a[j]>x do dec(j);
    if i<=j then
    begin
    y:=a[i];a[i]:=a[j];a[j]:=y;inc(i);dec(j);
    end;
    until i>j;
    if i<r then kuai(i,r);
    if l<j then kuai(l,j);
    end;
        
    begin
        {assign(input,'martian.in');assign(output,'martian.out');
        reset(input);rewrite(output);}
        readln(n,m);
        for i:=1 to n do read(a[i]);
        for m:=1 to m do begin
            for j:=n downto 1 do if a[j]>=a[pred(j)] then break;
            for p:=n downto 1 do if a[pred(j)]<=a[p] then break;
            swap(a[pred(j)],a[p]);
            kuai(j,n);
        end;
        for i:=1 to n do write(a[i],' ');
        //close(input);close(output);
    end.
    
  • -1
    @ 2016-09-18 20:56:27
    /*刚开始看还没理解题目意思,但是仔细一想才发现,其实 m的真正含义是讲n个数全排列第 m+1小的数 
    例如,m=1,样例中,那么输出的是第2小的,也可以说是除去本身之后全排列 第m小的排列组合*/
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    int n,m,num[10001];
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)scanf("%d",&num[i]);
        for(int i=1;i<=m;++i)
        {
        //详见于 “代码大全 -中 -算法- next_permutation函数 ” 的代码 
        
    /***********************************************************************************有可能看不懂 next_permutation函数
        附上 next_permutation函数的代码 正确理解 next_permutation函数 可见于网址:http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html 
        #include<iostream>
        #include<cstdio>
        #include<algorithm>
        using namespace std;
        int main()
        {
        int n;
     int a[n];
     scanf("%d",&n);
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
     do
    {
        for(int i=0;i<n;i++)
         cout<<a[i]<<" ";
         cout<<endl;
    } while (next_permutation(a,a+5));//表示对前5个数进行排列,后面的数不管 
    //next_permutation(a,a+3)  为对前面前三个数排列组合 
    return 0;
    }
        *******************************************************************************************************************/
        std::next_permutation(num+1,num+n+1);//第m次排列出来的 
        /*for(int i=1;i<=n;i++)printf("%d ",num[i]);
        printf("\n");*/
        }
    //    printf("\n");
        for(int i=1;i<=n;++i)printf("%d ",num[i]);
        return 0;
    }
    
  • -1
    @ 2016-05-29 23:22:44

    看你们都用STL大法,看来我只有手写生成字典序排列了。。。
    编译成功

    foo.c: In function 'main':
    foo.c:7:27: warning: 'min' may be used uninitialized in this function [-Wmaybe-uninitialized]
    int n, m, i, j, temp, t, min;
    ^

    测试数据 #0: Accepted, time = 0 ms, mem = 524 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 528 KiB, score = 10

    测试数据 #2: Accepted, time = 0 ms, mem = 524 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 524 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 528 KiB, score = 10

    测试数据 #5: Accepted, time = 0 ms, mem = 528 KiB, score = 10

    测试数据 #6: Accepted, time = 0 ms, mem = 524 KiB, score = 10

    测试数据 #7: Accepted, time = 0 ms, mem = 524 KiB, score = 10

    测试数据 #8: Accepted, time = 0 ms, mem = 528 KiB, score = 10

    测试数据 #9: Accepted, time = 0 ms, mem = 528 KiB, score = 10

    Accepted, time = 0 ms, mem = 528 KiB, score = 100

    代码

    include <stdio.h>

    define SWAP(a,b,t) t=a, a=b, b=t

    int a[10005];

    int main (void)
    {
    int n, m, i, j, temp, t, min;
    scanf("%d%d",&n,&m);
    for (i=0; i<n; i++) {
    scanf("%d",a+i);
    }
    while (m--) {
    for (i=n-1; i>0; i--) {
    if (a[i] > a[i-1]) {
    for (j=n-1; j>=i; j--) {
    if (a[j] > a[i-1]) {
    min = j;
    break;
    }
    }
    SWAP(a[i-1], a[min], temp);
    if (i < n-1) {
    t = (n-i)/2 + i;
    for (j=i; j<t; j++) {
    SWAP(a[j], a[n+i-1-j], temp);
    }
    }
    break;
    }
    }
    }
    for (i=0; i<n; i++) {
    printf(i==0 ? "%d" : " %d",a[i]);
    }
    putchar('\n');
    return 0;
    }

    • @ 2016-05-29 23:27:48

      吐槽一下为什么发出来就变这排版了。。。。。

    • @ 2016-08-12 18:31:24

      你发的方式不对,点一下“编辑器快速入门 ”看看

  • -1
    @ 2016-03-02 14:02:58

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>

    using namespace std;
    int a[10005];
    int n,m;
    int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++){
    scanf("%d",&a[i]);
    }
    for(int i = 1;i <= m;i ++){
    next_permutation(a + 1,a + n + 1);
    }
    for(int i = 1;i <= n;i ++){
    printf("%d ",a[i]);
    }
    return 0;
    }
    STL666

  • -1
    @ 2015-10-06 16:30:01

    #include<cstdio>
    #include<algorithm>
    int n,m,num[10001];
    int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)scanf("%d",&num[i]);
    for(int i=1;i<=m;++i)std::next_permutation(num+1,num+n+1);
    for(int i=1;i<=n;++i)printf("%d ",num[i]);
    }
    STL大法好!!!

  • -1
    @ 2015-08-08 19:47:16

    program p1115;
    var n,m,i,j,k:longint;
    a,b:array[0..10000] of longint;
    procedure work;
    var i,t,p:longint;
    begin
    for i:=n downto 1 do
    if a[i-1]<a[i] then break;
    t:=i-1;
    for i:=n downto t+1 do
    if a[i]>a[t] then break;
    p:=a[i];a[i]:=a[t];a[t]:=p;
    for i:=t+1 to n do b[i]:=a[n+t+1-i];
    for i:=t+1 to n do a[i]:=b[i];
    end;
    //main
    begin
    readln(n);
    readln(m);
    for i:=1 to n do read(a[i]);
    for k:=1 to m do work;
    for i:=1 to n-1 do write(a[i],' ');
    writeln(a[n]);
    end.

  • -1
    @ 2015-05-16 19:28:23

    std::next_permutation
    Rearranges the elements in the range [first,last) into the next lexicographically greater permutation.

    该函数定义在algorithm头文件中,可以将序列变换为下一个字典序的排列。

    细看此题之前的题解,“柳暗花明又一村”,同时也是时代变化的体现。

  • -1
    @ 2015-05-01 21:09:09

    /*************************************************************************
    > File Name: 1115.cpp
    > Author: Netcan
    > Blog: http://www.netcan.xyz
    > Mail: 1469709759@qq.com
    > Created Time: Fri 01 May 2015 21:02:36 CST
    ************************************************************************/

    #include <iostream>
    #include <vector>
    #include <string>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <ctime>
    #include <cstdio>
    #include <sstream>
    #include <deque>
    #include <functional>
    #include <iterator>
    #include <list>
    #include <map>
    #include <memory>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <utility>
    #include <cstring>
    using namespace std;

    int main()
    {
    int N,M;
    int a[10005];
    while(scanf("%d%d", &N, &M)==2) {
    for(int i=0; i<N; ++i)
    scanf("%d", &a[i]);
    for(int i=0; i<M; ++i)
    next_permutation(a, a+N);
    for(int i=0; i<N; ++i)
    printf("%d ", a[i]);
    printf("\n");

    }

    return 0;
    }

  • -1
    @ 2015-03-25 17:00:11

    我想多了,千万别用康托法!!

  • -1
    @ 2015-02-08 12:02:33

    我只知道用模拟算法能够通过。就从1加到100,挨个走。
    #include<iostream>
    #include<string.h>
    #include<math.h>
    using namespace std;
    int a[10003];
    int size;
    void sort(int to){
    int i, j;
    for (i = 0; i < to; i++){
    for (j = i + 1; j < to; j++){
    if (a[i] < a[j]){
    int temp = a[j];
    a[j] = a[i];
    a[i] = temp;
    }
    }
    }
    }
    void inc(){
    int i, j, k;
    for (i = 1; i < size; i++){
    if (a[i - 1]>a[i])break;
    }
    for (j = 0; j < i; j++){
    if (a[j]>a[i])break;
    }
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    sort(i);
    }
    int main(){
    freopen("in.txt", "r", stdin);
    int adding;
    int i, j, k;
    cin >> size >> adding;
    for (i = size - 1; i >= 0; i--)cin >> a[i];
    for (i = 0; i < adding; i++){
    inc();
    }
    for (j = size - 1; j >= 0; j--)cout << a[j] << " ";
    return 0;
    }

  • -1
    @ 2014-12-16 21:24:34

    全是神犇。。。**orzorz**

    测试数据 #0: Accepted, time = 15 ms, mem = 856 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 852 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 856 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 852 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 856 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 856 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 852 KiB, score = 10
    测试数据 #7: Accepted, time = 0 ms, mem = 852 KiB, score = 10
    测试数据 #8: Accepted, time = 0 ms, mem = 856 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 856 KiB, score = 10
    Accepted, time = 15 ms, mem = 856 KiB, score = 100

    program hxr;
    var
    a:array[1..10005] of longint;
    n,m,k:integer;
    i,j,p,p1,min:longint;
    procedure sort(l,r: longint);
    var
    i,j,x,y: longint;
    begin
    i:=l;
    j:=r;
    x:=a[(l+r) div 2];
    repeat
    while a[i]<x do
    inc(i);
    while x<a[j] do
    dec(j);
    if not(i>j) then
    begin
    y:=a[i];
    a[i]:=a[j];
    a[j]:=y;
    inc(i);
    j:=j-1;
    end;
    until i>j;
    if l<j then
    sort(l,j);
    if i<r then
    sort(i,r);
    end;
    begin
    readln(n);
    readln(m);
    for i:=1 to n do
    read(a[i]);
    for i:=1 to m do
    begin
    for j:=n-1 downto 1 do
    if a[j]<a[j+1] then begin
    p:=j;
    break;
    end;
    min:=maxint;
    for j:=p to n do
    if (a[j]>a[p])and(min>a[j]) then begin
    min:=a[j];
    p1:=j;
    end;
    k:=a[p];
    a[p]:=a[p1];
    a[p1]:=k;
    sort(p+1,n);
    end;
    for i:=1 to n do
    write(a[i],' ');
    end.

  • -1
    @ 2014-11-04 19:31:53

    var
    n,k,i,ans:longint;
    a:array[1..10000] of longint;

    procedure qsort(l,r:longint);
    var
    i,j,mid,t:longint;
    begin
    i:=l;
    j:=r;
    mid:=a[(l+r) div 2];
    repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
    begin
    t:=a[i];
    a[i]:=a[j];
    a[j]:=t;
    inc(i);
    dec(j);
    end;
    until i>j;
    if l<j then qsort(l,j);
    if i<r then qsort(i,r);
    end;

    procedure try(p:longint);
    var
    i,k,l,j,t:longint;
    t1:boolean;
    begin
    if ans=0 then
    begin
    for i:=1 to n-1 do
    write(a[i],' ');
    writeln(a[n]);
    halt;
    end;
    for i:=n-1 downto p+1 do
    begin
    t1:=true;
    while t1 do
    begin
    t1:=false;
    l:=maxlongint;
    k:=0;
    for j:=i+1 to n do
    if (a[j]>a[i]) and (a[j]<l) then
    begin
    l:=a[j];
    k:=j;
    end;
    if k<>0 then
    begin
    t:=a[i];
    a[i]:=a[k];
    a[k]:=t;
    t1:=true;
    qsort(i+1,n);
    dec(ans);
    try(i);
    end;
    end;
    end;
    end;

    begin
    readln(n);
    readln(k);
    for i:=1 to n do
    read(a[i]);
    ans:=k;
    try(0);
    end.

信息

ID
1115
难度
3
分类
组合数学 点击显示
标签
递交数
3228
已通过
1663
通过率
52%
被复制
24
上传者