Codeforces D. Phoenix and Science (贪心 / 思维) (Round #638 Div.2)

传送门

题意: 最开始有一个权重为1的细胞,每个细胞可以在白天选择分裂(或不分裂)成两个等权重的小细胞,但晚上每个细胞都必会增长一个权重。
试问最少多少天所以细胞的权重和可以正好等于n?并输出每天进行分裂的细胞数目(满足条件的方案即可)。若不能正好等于n,则直接输出-1。
在这里插入图片描述

思路:

  • 在第一天细胞可选择不分裂,则第二天总权重变成2;若选择分裂,则第二天总权重变成3。
  • 依次类推知要想尽快达到权重n,则应让每天晚上能增重的细胞数最多(即尽可能的分裂)。
  • 所以我们可以构造出每天分裂的细胞数列:2 ^ 0,2 ^ 1,2 ^ 3 ……2 ^ x(满足sum <= n,x表示第几天)。这其实也相当于每天增重的序列,这样就可得到每天细胞增重的做大值。
  • 若序列和sum == n,则直接输出x即可。若序列和sum != n ,那么最小天数就是x + 1;再将n - sum加入序列,将序列进行排序,第 i 天进行分裂的细胞数即为a[i] - a[i - 1]。

代码实现:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n, a[N];

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        cin >> n;
        int tt = 0;
        memset(a, 0, sizeof a);
        //开始构造序列
        for(int i = 1; i <= n; i <<= 1){
            a[tt ++] = i;
            n -= i;
        }
        //这里若n != 0的话,那么现在的n即为初值的n 减去sum
        if(n) a[tt ++] = n;
        sort(a, a + tt);
        cout << tt - 1 << endl;
        for(int i = 1; i < tt; i ++)
            cout << a[i] - a[i - 1] << " ";
        cout << endl;
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值