Codeforces Round 940 (Div. 2)题解

A. Stickogon

我们可以发现贪心构成正三角形最优。

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
void solve(){
      
    int n;
    cin>>n;
    map<int,int>mp;
    for(int i=0,t;i<n;i++){
   
        cin>>t;
        mp[t]++;
    }
    int res=0;
    for(auto [i,j]:mp)res+=j/3;
    cout<<res<<'\n';
    return;
}
int main(){
   
    ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
   
        solve();
    }
    return 0;
}

B. A BIT of a Construction

当n=1时输出k就行。
当n>1时设k的最高位为x则 2 x ≤ k < 2 x + 1 2^x\leq k<2^{x+1} 2xk<2x+1
只有当 k = 2 x + 1 − 1 k=2^{x+1}-1 k=2x+11时此时1的个数为x+1,
否则我们1的个数只能达到x,可以拆成 2 x − 1 2^x-1 2x1 k − 2 x + 1 k-2^x+1 k2x+1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
   
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    if (n == 1) {
   
        a[0] = k;
    }
    else {
   
        int msb = 0;
        for (int i = 0; i < 31; i++) {
   
            if (k & (1 << i)) {
   
                msb = i;
            }
        }
        a[0] = (1 << msb) - 1;
        a[1] = k - a[0];
        for (int i = 2; i < n; i++) {
   
            a[i] = 0;
        }
    }
    for (int i = 0; i < n; i++) {
   
        cout << a[i] << " ";
    }
    cout << "\n";
    return;
}
int main() {
   
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
   
        solve();
    }
    return 0;
}

C.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值