codeforces 1348D (思维+贪心)
题意:一天分为白天和黑夜,白天可以选择让任意份细胞分解,一份细胞可分解为两份,重量平分。晚上每一份细胞重量+1。给一个n,初始有一份细胞,重量为1。问最少多少次可使得细胞总重量为n。
思路:倘若每天都让所有的细胞分裂,则每天加的cha值都会×2,定义一个cun数组,用来存每天新加的重量(既是cha值),sum表示当前这天细胞总质量为多少。
当sum大于n,则表示这天的重量超过了,则需要前面抽出某一天k,让这一天cha值为sum-n。因为cha值是递增的,所以只需要在尾部添加然后sort即可。
cun数组用来存新加的cha值,所以cun数组的差分数组是cha值的增加值即答案。
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; int cun[100]; int main() { //freopen("input.txt", "r", stdin); int _; scanf("%d",&_); while(_--) { ll n; cin>>n; int tail = 0; int cha = 1; ll sum = 1; cun[++tail] = 1; while(1) { cha *= 2; if(sum + cha == n){ cun[++tail] = cha; break; } else if(sum + cha > n){ cun[++tail] = n-sum; break; } cun[++tail] = cha; sum += cha; } /*rep(i,1,tail) cout<<cun[i]<<sp; cout<<endl;*/ sort(cun+1,cun+1+tail); cout<<tail-1<<endl; rep(i,2,tail){ cout<<cun[i]-cun[i-1]<<sp; } cout<<endl; } }