D. Phoenix and Science

本文探讨了一种关于细菌分裂的算法问题,细菌通过白天分裂和夜晚质量增长的方式,达到目标总质量。文章提供了一个解决方案,通过每日分裂最大化质量增长速度,最终实现目标总质量。

D. Phoenix and Science

题意
刚开始有一个细菌质量是1,然后从第一天开始,白天你可以选择让任意个细菌分裂,质量为m的细菌可以分裂为两个质量为m / 2的细菌,到了每天晚上每个细菌的质量都会加1,问最少要多少天,细菌的质量总和恰好可以达到n,输出每天分裂的细菌的个数。

思路
这是一个构造题。构造的方法应该挺多的。
考虑一下,对于细菌分裂而言,只是个数增加了,总质量是不变的,那么质量是怎么增加的呢?靠每天晚上的所有细菌的质量自增一。很容易知道,如果当前细菌的总个数为x,那么经过一天后,质量的增量为
[x,2x],为什么?如果白天不分裂,晚上自增质量就是x,如果白天都分裂,x个细菌变为2x个细菌,那么晚上2x细菌每个都质量加1。所以既然要快速得到总质量为n,我们考虑让细菌每天都一直分裂。
那么总质量就是sum=1+2+4+8+16…把每个数字加入数组中,我们计算出离n最近的总质量。
比如n为8,我们得到1+2+4=7,还少1怎么办呢,把n-sum也加入数组。
排序一下。数组中的每个ai的含义是,到第i天为止,有i个细菌。
那么差分输出一下即可。差分得到就是每天新增的细菌数,也就是分裂的个数

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        vector<int> a;
        int sum=0;
        for(int i=1;sum+i<=n;i<<=1){
            sum+=i;
            a.push_back(i);
        }
        if(sum<n) a.push_back(n-sum);
        sort(a.begin(),a.end());
        cout<<a.size()-1<<endl;
        for(int i=1;i<a.size();i++){
            cout<<a[i]-a[i-1]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
java.sql.SQLException: ERROR 726 (43M10): Inconsistent namespace mapping properties. Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server. at org.apache.phoenix.exception.SQLExceptionCode$Factory$1.newException(SQLExceptionCode.java:607) at org.apache.phoenix.exception.SQLExceptionInfo.buildException(SQLExceptionInfo.java:217) at org.apache.phoenix.query.ConnectionQueryServicesImpl.checkClientServerCompatibility(ConnectionQueryServicesImpl.java:1731) at org.apache.phoenix.query.ConnectionQueryServicesImpl.ensureTableCreated(ConnectionQueryServicesImpl.java:1460) at org.apache.phoenix.query.ConnectionQueryServicesImpl.createTable(ConnectionQueryServicesImpl.java:1939) at org.apache.phoenix.schema.MetaDataClient.createTableInternal(MetaDataClient.java:3090) at org.apache.phoenix.schema.MetaDataClient.createTable(MetaDataClient.java:1117) at org.apache.phoenix.compile.CreateTableCompiler$CreateTableMutationPlan.execute(CreateTableCompiler.java:420) at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:443) at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:425) at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53) at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:424) at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:412) at org.apache.phoenix.jdbc.PhoenixStatement.executeUpdate(PhoenixStatement.java:1990) at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:3312) at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:3266) at org.apache.phoenix.util.PhoenixContextExecutor.call(PhoenixContextExecutor.java:76) at org.apache.phoenix.query.ConnectionQueryServicesImpl.init(ConnectionQueryServicesImpl.java:3266) at org.apache.phoenix.jdbc.PhoenixDriver.getConnectionQueryServices(PhoenixDriver.java:255) at org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.createConnection(PhoenixEmbeddedDriver.java:144) at org.apache.phoenix.jdbc.PhoenixDriver.connect(PhoenixDriver.java:221) at sqlline.DatabaseConnection.connect(DatabaseConnection.java:135) at sqlline.DatabaseConnection.getConnection(DatabaseConnection.java:192) at sqlline.Commands.connect(Commands.java:1364) at sqlline.Commands.connect(Commands.java:1244) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sqlline.ReflectiveCommandHandler.execute(ReflectiveCommandHandler.java:38) at sqlline.SqlLine.dispatch(SqlLine.java:730) at sqlline.SqlLine.initArgs(SqlLine.java:410) at sqlline.SqlLine.begin(SqlLine.java:515) at sqlline.SqlLine.start(SqlLine.java:267) at sqlline.SqlLine.main(SqlLine.java:206) sqlline version 1.9.0
03-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值