AtCoder Regular Contest 146 B Plus and AND
Plus and AND#
贪心
从高位开始逐位枚举,如果当前位能够在 的代价内将所有数字都变成 ,则认为结果的当前位是 ,如果凑这一位利用了低位的 ,则要将其全部置为
重复这个过程
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, m, k;
cin >> n >> m >> k;
vector<ll>a(n);
for(int i=0; i<n; i++) cin >> a[i];
vector<ll>num(n, 0), temp(n);
ll ans = 0;
for(int i=31; i>=0; i--)
{
for(int j=0; j<n; j++)
{
temp[j] = num[j];
if(a[j] >> i & 1) temp[j] += 0;
else temp[j] += (1ll << i) - (a[j] % (1ll << i));
}
sort(temp.begin(), temp.end());
ll sum = 0;
int f = 0;
for(int j=0; j<k; j++) sum += temp[j];
if(sum <= m) f = 1;
if(f)
{
ans |= 1ll << i;
for(int j=0; j<n; j++)
{
if(a[j] >> i & 1) num[j] = num[j];
else
{
num[j] += (1ll << i) - (a[j] % (1ll << i));
a[j] = 0;
}
}
}
}
cout << ans << endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】