2026.Max Sum (c++)

Problem Description

Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is
defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order.
Your task is to find the m-th one.

Input

The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20,
0< m<= the total number of the subset sequence of An ).

Output

For each test case, you should output the m-th subset sequence of An in one line.

Sample Input

1 1
2 1
2 2
2 3
2 4
3 10

Sample Output

1
1
1 2
2
2 1
2 3 1

题意:给定一个集合{1,2,3,4….n},求它内部的不同元素的字典序下的第m个字典序。

思路:

找规律吧

例:n=3,m=10时,有

{1}

{1, 2}

{1, 2, 3}

{1, 3}

{1, 3, 2}

{2}

{2, 1}

{2, 1, 3}

{2, 3}

{2, 3, 1}

{3}

{3, 1}

{3, 1, 2}

{3, 2}

{3, 2, 1}

n表示有多少组,g(n)表示每一组的个数,f(n)表示总的个数

g(n) = f(n) / n

->f(n) = n[f(n-1) + 1]

->g(n) = n[f(n-1) + 1] / n = f(n-1) + 1

->f(n-1) = (n-1) * g(n-1)

->g(n) = (n-1) * g(n-1) + 1

观察得:

是第几组则首元素是几,返回第一个元素;

第二元素通过判断在这一组的第几个,也就是第几组再输出;

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std; int main()
{
int n, a[25];
long long int m, c[21];
memset(c, 0, sizeof(c));
for (int i = 1; i <= 25; i++)
c[i] = (i - 1) * c[i - 1] + 1; // c[25]保存当n=i时,每一组的个数
while (cin >> n >> m)
{
for (int i = 1; i <= n; i++)
a[i] = i;
while (n > 0 && m > 0)
{
int temp = (m - 1) / c[n] + 1; // 第几组=输出的数字
if (temp > 0)
{
printf("%d", a[temp]);
for (int i = temp; i <= n; i++)
{ ///删掉这个数字
a[i] = a[i + 1];
}
m -= ((temp - 1) * c[n] + 1); //再该组的第几项
printf(m == 0 ? "\n" : " ");
}
n--;
}
}
return 0;
}

[HDOJ] 2026.Max Sum的更多相关文章

  1. HDOJ 3415 Max Sum of Max-K-sub-sequence(单调队列)

    因为是circle sequence,可以在序列最后+序列前n项(或前k项);利用前缀和思想,预处理出前i个数的和为sum[i],则i~j的和就为sum[j]-sum[i-1],对于每个j,取最小的s ...

  2. HDOJ 1024 Max Sum Plus Plus -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...

  3. Hdoj 1003.Max Sum 题解

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  4. 最大子序列和 HDOJ 1003 Max Sum

    题目传送门 题意:求MCS(最大连续子序列和)及两个端点分析:第一种办法:dp[i] = max (dp[i-1] + a[i], a[i]) 可以不开数组,用一个sum表示前i个数字的MCS,其实是 ...

  5. HDOJ(1003) Max Sum

    写的第一个版本,使用穷举(暴力)的方法,时间复杂度是O(N^2),执行时间超过限制,代码如下: #include <stdio.h> #define MAX_LEN 100000UL in ...

  6. HDOJ 1003 Max Sum(线性dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 思路分析:该问题为最大连续子段和问题,使用动态规划求解: 1)最优子结构:假设数组为A[0, 1 ...

  7. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  8. Max Sum of Max-K-sub-sequence(单调队列)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. POJ 3415 Max Sum of Max-K-sub-sequence (线段树+dp思想)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. Windows下获取逻辑cpu数量和cpu核数量

    代码可在Windows NT下正常运行 具体API说明请参照如下文档: GetLogicalProcessorInformation 点击打开链接 点击打开链接 点击打开链接 typedef BOOL ...

  2. 【web自动化测试】requests-html 这个解析库,能让你更轻松的获取网页内容

    1. 开始 Python 中可以进行网页解析的库有很多,常见的有 BeautifulSoup 和 lxml 等.在网上玩爬虫的文章通常都是介绍 BeautifulSoup 这个库,我平常也是常用这个库 ...

  3. asp.net core 系列之Startup

    这篇文章简单记录 ASP.NET Core中 ,startup类的一些使用. 一.前言 在 Startup类中,一般有两个方法: ConfigureServices 方法: 用来配置应用的 servi ...

  4. kubernetes实战篇之创建密钥自动拉取私服镜像

    系列目录 前面我们讲解了如何搭建nexus服务器,以及如何使用nexus搭建docker私有镜像仓库,示例中我们都是手动docker login登陆私服,然后通过命令拉取镜像然后运行容器.然而这种做法 ...

  5. 【java】SHA256加密工具

    SHA256: /** * 备用方案 SHA256加密 * @author zx */ public class SHA256Util { public static void main(String ...

  6. NumPy基础操作

    NumPy基础操作(1) (注:记得在文件开头导入import numpy as np) 目录: 数组的创建 强制类型转换与切片 布尔型索引 结语 数组的创建 相关函数 np.array(), np. ...

  7. 恢复云数据库MySQL的备份文件到自建数据库遇到的报错

    报错信息 : 恢复云数据库MySQL的备份文件到自建数据库,自建数据库版本5.6.36. 按照阿里云文档操作,启动数据库报错 -- :: [ERROR] /application/mysql/bin/ ...

  8. MySQL metalock的一些技巧(写大于读的案例,以及获得锁的顺序)

    前言:元数据锁不是锁定数据,而是锁定描述数据的元数据信息.就像很多装修工人(工作线程)在室内(对象上)装修(操作),不能有其他工人(线程)把屋子拆了(表删除了). MySQL 为了数据一致性使用元数据 ...

  9. Docker-Compose搭建单体SkyWalking

    SkyWalking简介 SkyWalking是一款高效的分布式链路追踪框架,对于处理分布式的调用链路的问题定位上有很大帮助 有以下特点: 性能好 针对单实例5000tps的应用,在全量采集的情况下, ...

  10. 配置Python虚拟环境

    最小化安装的centos7中并没有安装python3 1.安装python3 1)下载安装包: wget https://www.python.org/ftp/python/3.6.2/Python- ...