[HDOJ] 2026.Max Sum
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的更多相关文章
- 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 ...
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...
- 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 ...
- 最大子序列和 HDOJ 1003 Max Sum
题目传送门 题意:求MCS(最大连续子序列和)及两个端点分析:第一种办法:dp[i] = max (dp[i-1] + a[i], a[i]) 可以不开数组,用一个sum表示前i个数字的MCS,其实是 ...
- HDOJ(1003) Max Sum
写的第一个版本,使用穷举(暴力)的方法,时间复杂度是O(N^2),执行时间超过限制,代码如下: #include <stdio.h> #define MAX_LEN 100000UL in ...
- HDOJ 1003 Max Sum(线性dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 思路分析:该问题为最大连续子段和问题,使用动态规划求解: 1)最优子结构:假设数组为A[0, 1 ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- 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 ...
- 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 ...
随机推荐
- VPS用来配置上网外,还可以做一个同步盘
我曾经在一个活动的博文里说过,男人必须要有一个VPS和一个树莓派,VPS这个东西,以后会是中国男人的一种必备技能,今天又有一个小伙伴请教我VPS的用法,我就简单说说我目前使用的情况.首先我希望你能有点 ...
- 发布Qt Widgets桌面应用程序的方法(自定义进程步骤,用QT Creator直接生成)
发布Qt Widgets桌面应用程序的方法 Qt是一款优秀的跨平台开发框架,它可以在桌面.移动平台以及嵌入式平台上运行.目前Qt 5介绍程序发布的文章帖子比较少.大家又非常想要知道如何发布Qt应用程序 ...
- 抓取报表ALV GRID上的数据
在项目开发过程中需要从标准报表MB5B中获取数据,以下是本人实例中的相关部分,程序同样适用于获取其他标准报表的数据. CL_SALV_BS_RUNTIME_INFO=>SET( DISPL ...
- 请给出linux中查看系统已经登录用户的命令?
w命令 第一行:当前系统运行了多久和系统负载 谁正在远程登录系统并且在干什么 [root@martin ~]# w 11:30:33 up 4 days, 18:10, 2 users, load a ...
- 如何使用VS Code编写Spring Boot (第二弹)
本篇文章是续<如何使用VS Code编写Spring Boot> 之后,结合自己.net经验捣鼓的小demo,一个简单的CRUD,对于习惯了VS操作模式的.net人员非常方便,强大的智能提 ...
- asp.net core 系列之Startup
这篇文章简单记录 ASP.NET Core中 ,startup类的一些使用. 一.前言 在 Startup类中,一般有两个方法: ConfigureServices 方法: 用来配置应用的 servi ...
- Spring Cloud全链路追踪实现(Sleuth+Zipkin+RabbitMQ+ES+Kibana)
简介 在微服务架构下存在多个服务之间的相互调用,当某个请求变慢或不可用时,我们如何快速定位服务故障点呢?链路追踪的实现就是为了解决这一问题,本文采用Sleuth+Zipkin+RabbitMQ+ES+ ...
- java的equals与==的区别
看了网上关于equal与==的区别,感觉很多有些片面,不仔细,这里我来说说我对equal与==的理解 首先要了解基本类型与引用类型 1.int,char,boolean之类的就是基本类型,我们只要使用 ...
- 解决IE8placeholder属性问题
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 【翻译】Keras.NET简介 - 高级神经网络API in C#
Keras.NET是一个高级神经网络API,它使用C#编写,并带有Python绑定,可以在Tensorflow.CNTK或Theano上运行.其关注点是实现快速实验.因为做好研究的关键是:能在尽可能短 ...