[UCSD白板题] Pairwise Distinct Summands
Problem Introduction
This is an example of a problem where a subproblem of the corresponding greedy algorithm is slightly distinct from the initial problem.
Problem Description
Task.The goal of this problem is to represent a given positive integer \(n\) as a sum of as many pairwise distinct positive integers as possible. That is, to find the maximum \(k\) such that \(n\) can be written as \(a_1+a_2+\cdots+a_k\) where \(a_1, \cdots, a_k\) are positive integers and \(a_i \neq a_j\) for all \(1 \leq i < j \leq k\).
Input Format.The input consists of a single integer \(n\).
Constraints.\(1 \leq n \leq 10^9\).
Output Format.In the first line, output the maximum number \(k\) such that \(n\) can be represented as a sum of \(k\) pairwise distinct positive integers. In the second line, output \(k\) pairwise distinct positive integers that sum up tp \(n\)(if there are many such representation, output any of them).
Sample 1.
Input:
6
Output:
3
1 2 3
Sample 2.
Input:
8
Output:
3
1 2 5
Sample 3.
Input:
2
Output:
1
2
算法分析
引理: 整数\(k\)由\(p\)个不重复的被加数组成,每一项至少为\(l\),令\(k>2l\)并让这样的\(p\)取最大值。那么存在一个最佳的表示方式\(k=a_1+a_2+\cdots+a_p\)(每一项都不小于\(l\)并且两两不同)使得\(a_1=l\)。
证明:考虑某种最佳的表示方式\(k=b_1+b_2+\cdots+b_p\)。不失一般性,不妨假设\(b_1<b_2<\cdots<b_p\),已知\(p\geq2\)(因为\(k>2l\))。如果\(b_1=l\),那么结论成立。否则,令\(\Delta=b_1-l \geq 1\),考虑以下的表示方式:\(n=(b_1-\Delta)+b2+\cdots+(b_p+\Delta)\),不难发现,这是一个最佳的表示方式(包括p个被加数并且两两不同)。
Solution
# Uses python3
import sys
def optimal_summands(n):
summands = []
k, l = n, 1
while k > 2 * l:
summands.append(l)
k, l = k-l, l+1
summands.append(k)
return summands
if __name__ == '__main__':
input = sys.stdin.read()
n = int(input)
summands = optimal_summands(n)
print(len(summands))
for x in summands:
print(x, end=' ')
[UCSD白板题] Pairwise Distinct Summands的更多相关文章
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
- [UCSD白板题] Maximum Pairwise Product
Problem Description Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-1}\),find the ma ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
- [UCSD白板题] Number of Inversions
Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...
随机推荐
- java 的SPI机制
今天看到spring mvc 使用Java Validation Api(JSR-303)进行校验,需要加载一个 其具体实现(比如Hibernate Validator), 本来没有什么问题,但是突然 ...
- HTTP认证机制(翻译)
发现一篇介绍HTTP认证的好文章,就尝试翻译了一下,记录在下面.(翻译的很挫,哈哈哈) 原文: http://frontier.userland.com/stories/storyReader$215 ...
- WPF简单导航框架(Window与Page互相调用)
相当多的WPF程序都有着丰富的页面和功能,如何使程序在不同页面间转换并降低资源占用,选择适合自己的导航框架就很重要了.最近花了一点时间做了一个简单的导航框架,并在这个过程中对Window.Page.U ...
- char、varchar、nchar、nvarchar的区别
http://www.cnblogs.com/mekong/archive/2009/04/17/1437996.html
- Windows 2012 R2图标以及字体颜色发生变化更改成默认设置
1. 在桌面按"Win+R",然后输出regedit.2. 定位到HKEY_CURRENT_USER\Control panel\Colors3. 对照下面提供给您的初始化颜色的注 ...
- Android资源文件简介
Android资源文件简介 1. Android应用资源的作用 (1) Android项目中文件分类 在Android工程中, 文件主要分为下面几类 : 界面布局文件, Java src源文件, 资源 ...
- 根据UIColor对象,获取对应的RGBA值
- (NSArray *)getRGBWithColor:(UIColor *)color { CGFloat red = 0.0; CGFloat green = 0.0; CGFloat blue ...
- Reset CSS:只选对的,不选"贵"的
玉伯和正淳一起整理的一份 reset.css: /* KISSY CSS Reset 理念:清除和重置是紧密不可分的 特色:1.适应中文 2.基于最新主流浏览器 维护:玉伯(lifesinger@gm ...
- ajax 异步加载显示等待效果
css: #loading { width:170px; height:25px; border:3px solid #C3DAF9; position:absolute; top:300px; le ...
- Ubuntu 15.10 x64 安装 Android SDK
操作系统:Ubuntu 15.10 x64 目标:安装 Android SDK 本文最后更新时间:2015-11-3 安装32位库文件 2013年9月的iPhone 5s是第一款64位手机,而Andr ...