• vijosP1115 火星人


    vijosP1115 火星人

    链接:https://vijos.org/p/1115

    【思路】

      排列组合。

      题目要求为求第下m个排列。

      这里有两种方法,首选的是调用algorithm中的next_permutation函数,其次就是手写生成函数。

    【代码1】53ms

     1 #include<iostream> 
     2 #include<algorithm>
     3 using namespace std;
     4 const int maxn = 10000+10;
     5 int p[maxn];
     6 int n,m;
     7 
     8 int main() {
     9     ios::sync_with_stdio(false);
    10     cin>>n>>m;
    11     for(int i=0;i<n;i++) cin>>p[i];
    12     
    13     while(m--) next_permutation(p,p+n);
    14     
    15     for(int i=0;i<n;i++) cout<<p[i]<<" ";
    16     
    17     return 0;
    18 }

    【代码2】106ms

     1 #include<iostream> 
     2 #include<algorithm>
     3 using namespace std;
     4 const int maxn = 10000+10;
     5 int p[maxn];
     6 int n,m;
     7 
     8 bool get_permutation() {
     9     int i=n-1;
    10     while(i>0 && p[i-1] >= p[i]) i--;
    11     if(i==0) return false;
    12     int mp=i;
    13     for(int j=i+1;j<n;j++) {
    14         if(p[j]<=p[i-1]) continue;
    15         if(p[j]<p[mp]) mp=j;
    16     }
    17     swap(p[mp],p[i-1]);
    18     sort(p+i,p+n);
    19     return true;
    20 }
    21 
    22 int main() {
    23     ios::sync_with_stdio(false);
    24     cin>>n>>m;
    25     for(int i=0;i<n;i++) cin>>p[i];
    26     
    27     while(m--) get_permutation();
    28     
    29     for(int i=0;i<n;i++) cout<<p[i]<<" ";
    30     
    31     return 0;
    32 }
  • 相关阅读:
    栈和队列
    数组的遍历查找
    字符串的子串
    两个字符串
    字符串的遍历
    字符串的替换
    数组和矩阵
    Django 自带的ORM增删改查
    what's the CRSF ??
    Rabbitmq -Publish_Subscribe模式- python编码实现
  • 原文地址:https://www.cnblogs.com/lidaxin/p/4873421.html
Copyright © 2020-2023  润新知