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的更多相关文章

  1. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

  2. [UCSD白板题] Maximum Pairwise Product

    Problem Description Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-1}\),find the ma ...

  3. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  4. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  5. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  6. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  7. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. js 添加事件 attachEvent 和 addEventListener 的用法

    一般我们在JS中添加事件,是这样子的 ? 1 obj.onclick=method 这种绑定事件的方式,兼容主流浏览器,但如果一个元素上添加多次同一事件呢? ? 1 2 3 obj.onclick=m ...

  2. ZooKeeper设置ACL权限控制

    ZK的节点有5种操作权限:CREATE.READ.WRITE.DELETE.ADMIN 也就是 增.删.改.查.管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)注:这5种权限中,del ...

  3. c++接口实现与分离(转载)

    原文地址:http://www.360doc.com/content/13/0218/10/8363527_266294787.shtml 良好的设计应该只暴露接口给用户,所有的实现细节对用户来说应该 ...

  4. spark 获取applicationID

    在编写spark 程序时,有时需要获取job id 进行记录. 只需在程序中加入: sc.applicationId res0: String = app-- 每个job提交后, 均有自己的监控页面. ...

  5. (转载)IE6支持透明PNG图片解决方案:DD_belatedPNG.js

    DD_belatedPNG.js 是一个能是IE6支持p显示ng透明图片,而且还支持背景循环(background-repeat)和定位(backgrond-position) ,支持focus,Ho ...

  6. 微信小程序 wx.getUserInfo 解密 C# 代码

    花了6小时,弄出来的代码.网上的是PHP代码 public static string DecodeUserInfo(string raw, string signature,string encry ...

  7. Git相关知识

    一些有用的链接: https://www.git-scm.com/ http://nvie.com/posts/a-successful-git-branching-model/ Git开发模式: 建 ...

  8. 编码UTF-8

    ☯,首先,这并不是图片,这是一个unicode字符,Yin Yang,即阴阳符,码点为U+262F.如果你的浏览器无法显示,可以查看这个链接http://www.fileformat.info/inf ...

  9. C# 科学计数法转换成数字

    /// <summary> /// 判断输入的数是否是科学计数法.如果是的话,就会将其换算成整数并且返回,否则就返回false. /// </summary> /// < ...

  10. ng-repeat产生的对象会带有$$hashkey属性处理方法

    angularJS在ng-repeat的时候会产生一个$$hashkey的属性向后台发送请求的时候需要转成JSON的string格式(如果是使用ng自带的$http服务的话可以无视,$http服务会自 ...