zoukankan      html  css  js  c++  java
  • POJ Parliament

    Parliament

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
    Total Submission(s) : 2   Accepted Submission(s) : 1
    Problem Description
    New convocation of The Fool Land's Parliament consists of N delegates. According to the present regulation delegates should be divided into disjoint groups of different sizes and every day each group has to send one delegate to the conciliatory committee. The composition of the conciliatory committee should be different each day. The Parliament works only while this can be accomplished. You are to write a program that will determine how many delegates should contain each group in order for Parliament to work as long as possible.
     
    Input
    The input file contains a single integer N (5<=N<=1000 ).
     
    Output
    Write to the output file the sizes of groups that allow the Parliament to work for the maximal possible time. These sizes should be printed on a single line in ascending order and should be separated by spaces.
     
    Sample Input
    7
     
    Sample Output
    3 4
     
    Source
    PKU
     
     

    题意:给定一个正整数N。现需将N分成不相等的多份。问如何分,使得每份的数的总乘积最大。

    思路:中小学曾经做过类似的数学题:将一个数分成2份,如何分,使得这两个数乘积最大。答案是将这个数平分,证明是求x*(n-x)的最大值。基于这种思路,将N分成乘积最大的不相等的多份,应使得其中每份的数相差尽量少,即差值为1的等差数列为最理想状态。构造了一个等差数列以后,再根据剩余值对整个数列的值进行调整。使得相邻元素差值达到最小。这里注意,等差数列的构造应以2为首项,1为首项的话,对乘积没影响。。

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int res[1010];
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        while(~scanf("%d",&n)){
            int cnt=0;
            res[++cnt]=2;
            n-=2;
            while(res[cnt]<n){
                int tmp=res[cnt];
                res[++cnt]=tmp+1;
                n-=res[cnt];
            }
            for(int i=cnt;i>0;i--){
                res[i]+=(n+i-1)/i;
                n=n-(n+i-1)/i;
            }
            for(int i=1;i<cnt;i++)
                printf("%d ",res[i]);
            printf("%d\n",res[cnt]);
        }
        return 0;
    }
  • 相关阅读:
    缓存
    判断空对象的方法
    Vue响应式系统如何操作运用?本文详解
    2020最新中级web前端面试题库(含详细答案,15k级别)你会几道呢?
    如何用JavaScriptJ封装拖动验证滑块?本文教你
    Nodejs中ES Modules如何操作运用?本文详解
    Vue学习总结之Vue的生命周期是怎么运用操作的?本文详解
    Vue项目如何部署?实战教你
    canvas绘制简单的霓虹灯效果
    canvas绘制五角星详细过程
  • 原文地址:https://www.cnblogs.com/jackge/p/3035835.html
Copyright © 2011-2022 走看看