[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 ...
随机推荐
- 什么是SCADA Viewer
SCADA Viewer 什么是SCADA Viewer SCADA Viewer是一个基于Web的软件框架(基于Web的HMI/SCADA/M2M工业和楼宇自动化,支持Modbus,BACnet,O ...
- spark streaming 对接kafka记录
spark streaming 对接kafka 有两种方式: 参考: http://group.jobbole.com/15559/ http://blog.csdn.net/kwu_ganymede ...
- Activity之间传递参数(二)
------siwuxie095 传递数据包 1.传递数据包要用到Bundle,MainActivity.java中: package com.siwuxie095.sendargs; import ...
- Struts2 验证框架 validation.xml 常用的验证规则
validation.xml 的命名规则和放置路径: 文件名:<ActionClassName>-validation.xml <ActionClassName>就是要验证的A ...
- AFN框架内部结构
AFN结构体 - NSURLConnection + AFURLConnectionOperation + AFHTTPRequestOperation + AFHTTPRequestOperatio ...
- mysql中字符集的比较
Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢? 在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8 ...
- UISegmentedControl 控件
一.创建 UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil]; 是不是很奇怪没 ...
- Android开发工具全面转向Android Studio(2)——AS project/module的CRUD
本文有些地方可能需要衔接Android开发工具全面转向Android Studio(1)——准备开发环境,读起来效果会更好. 这个世界很奇妙,所有的东西离不开CRUD,即增删改查.即使人本身也遵循这个 ...
- CyclicBarrier在多线程同步运行后相互访问的问题。
CyclicBarrier的介绍 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相 ...
- android中回调函数机制完全解析
1.在要调用的业务操作中,创建一个接口,在接口中创建方法,这个方法表示的是我们原先要在业务类中执行的操作 public interface BackUpSmsListener { /** * 设置总进 ...