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. 一顶博士帽能带来什么——访GOOGLE公司中国区总裁李开复

      在读了博士生远潇给本报的来信后,GOOGLE公司中国区总裁李开复说,有这些困惑和担心,实际上是很多博士生们在读博士之前并没有认真地想过,自己是不是能耐得住寂寞做学问,是不是能抵御来自物质世界的诱惑 ...

  2. VS2010使用c++、gSOAP创建WebService 图文教程

    VS2010使用c++.gSOAP创建的WebService 图文教程 环境 操作系统:Windows 7gsoap版本:2.8.32C++编译器/开发环境:Visual Studio 2010 gS ...

  3. 虚拟机安装 ubuntu 后,更新源无效,以及无法联网安装软件的问题

    问题: 虚拟机安装 ubuntu 后,更新源无效,以及无法联网安装软件: 错误提示: Err http://security.ubuntu.com/ubuntu/ trusty-security/un ...

  4. Z Order of Controls in Delphi VCL

    Get and set the Z Order of controls at runtime in Delphi VCL. If you are looking for a FireMonkey so ...

  5. Sentinel2A影像监测家乡油菜长势!!

    首先当然得为我的家乡打一个广告啊,湖南省衡南县宝盖镇双河口村,非常有名的油菜花种植基地,从下面的图就可以看出来,欢迎各位童鞋前往观光旅游,家乡人民非常nice,非常热情.... 我的老家就住在双河口村 ...

  6. devexpress GridView按条件给行号上色

    private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDra ...

  7. webpack-simple之vagrant热加载

    "dev": "cross-env NODE_ENV=development webpack-dev-server --host 192.168.2.10 --port ...

  8. 你需要知道的c# Timer 的垃圾回收机制。

    通常我们需要定时执行一段任务的时候,我们就需要定时器,这时我们就可以使用c# System.Threading空间中的 Timer定时器;他是个异步定时器,时间到时每次都是在线程池中分配一个线程去执行 ...

  9. CSS3 入门级

    从刚开始学习的选择器总共有十三种: id class 标签 子代 后代 交集 并集 通配符 伪类 结构 属性 相邻 兄弟 (全当复习,如果有用的话那就正好) div[name=zhang]  这是属性 ...

  10. 【转载】一起来学Spring Cloud | Eureka Client注册到Eureka Server的秘密

    LZ看到这篇文章感觉写得比较详细,理解以后,便转载到自己博客中,留作以后回顾学习用,喝水不忘挖井人,内容来自于李刚的博客:http://www.spring4all.com/article/180 一 ...