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. Vim命令合集

    1.模式切换 三种模式:命令模式,输入模式,底行模式 命令模式与输入模式之间的切换:i esc 命令模式与底行模式的切换:shift + :  esc 2. 插入 i:在当前字符的左边插入 I:在当前 ...

  2. lucene历史版本地址

    http://archive.apache.org/dist/lucene/java/

  3. 关于C# DataTable 的一些操作

    经常操作DATATABLE  对于一些不需要再通过sql 来重复操作的   可以通过操作datatable来达到同样的效果 方法一: 也是广为人知的一种: YourDataTable.Columns. ...

  4. 如何将数据库中的表导入到PowerDesigner中

    1.        打开PowerDesigner12,在菜单中按照如下方式进行操作file->Reverse Engineer->DataBase 点击后,弹出 New Physical ...

  5. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  6. ocfs2: 搭建环境

    OCFS2是基于共享磁盘的集群文件系统,它在一块共享磁盘上创建OCFS2文件系统,让集群中的其它节点可以对磁盘进行读写操作.OCFS2由两部分内容构成,一部分实现文件系统功能,位于VFS之下和Ext4 ...

  7. exe文件放在其他位置

    set PATH=%PATH%;%UGII_ROOT_DIR%call "E:\ZY\exe\uds_rename_parts_mohao.exe"E:\ZY\exe\uds_re ...

  8. int main(int argc,char* argv[])详解

    argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数, 比如:       int   main(int   argc,   ...

  9. ibatis 参数错误,无效字符

    --- The error occurred in EmptyMapping.xml. --- The error occurred while applying a parameter map. - ...

  10. c# dotfuscator 混淆后无法使用

    在实体类中忘记给字段加上 get ;set ;导致编译后程序无法使用. 下面这个(A代码)是可以正常混淆的.     public class PhoneUsedStatus     {        ...