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. jquery 中substring,substr,split的用法

    substring 方法 返回位于 String 对象中指定位置的子字符串. strVariable.substring(start, end) 参数 start 指明子字符串的起始位置,该索引从 0 ...

  2. funny_python 00 The Zen of Python

    # 打算每天多动的时候尽量搜索一些和coding相关的funny stuff Day 00 - PEP 20 The Zen of Python 在shell里面输入python -m this 回车 ...

  3. keepalived+nginx高可用负载均衡环境搭建

    上篇说道keepalived的环境搭建,本来keepalived结合lvs更有优势,但是也可以结合nginx来使用.下面接着说下nginx的环境搭建 环境信息: nginx(master)  192. ...

  4. Pow 算法

    #include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...

  5. VBA Excel 对比两列数据

    Sub Md() ' ' Macro1 Macro ' 宏由 BX 录制,时间: 2012-6-8 ' 宏中的列数可以输入 A - IV 也可以输入 1-256 ' Dim i%, j%, i1%, ...

  6. KMP匹配算法

    先来说一下回溯法匹配字符串: 对于主字符串有一个target_index,以target_index(不动)为起点,匹配字符串pattern的长度+target_index为终点,逐个进行比较,当发现 ...

  7. python模块:base64

    base64模块是用来作base64编码解码的,在电子邮件中常见.它可以把不能作为文本显示的二进制数据编码为可显示的文本信息,编码后文本大小增加1/3.常用方法有: b64encode & b ...

  8. cannot find module 'xml2js'

    运行nodejs网站报类似错误,缺少相应的报 在NodeJs安装目录,运行Node.js command prompt ,跳转到网站所在目录,运行npm install xml2js,安装缺少的模块, ...

  9. Android开发工具全面转向Android Studio(1)——准备开发环境

    工欲善其事必先利其器,本文适合Android新手以及用过Eclipse而没用过Android Studio开发Android的老手,众所周知,谷歌是不会再维护和开发ADT了,旧的ADT已经是完全不能渲 ...

  10. servlet总结

    什么是Servlet Tomcat容器等级 手工编写第一个Servlet 使用MyEclipse编写Servlet Servlet生命周期 Servlet常用对象,且与Jsp九大内置对象的关系 Ser ...