跳到正文

题解

题解:P10911 [蓝桥杯 2024 国 B] 数位翻转

Heart_of_Iron约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年12月8日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 2 分钟
题目
P10911

版本

  1. v1当前

这题正解就是暴力,不会超时。首先模拟翻转之后会增加的 aia_i 然后打上标记,记录翻转后的值,最后做一个区间合并再排序就行了。

构造的特例如下:

代码
5 1
11 11 15 11 11

处理后的数组为:

代码
13 13 15 13 13

这里就是把整个数组整体处理一遍,所以打标记的时候需要将相等的也要打上。最后做一个区间合并就行了,细节比较多。

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1010;
int n,m;LL a[N],w[N];bool st[N];
bool cmp(int a,int b)
{
  return a>b;
}
LL get_val(LL x)
{
  LL res=0;
  while(x)
  {
    res=(res<<1)|(x&1);x>>=1;
  }
  return res;
}
int main()
{
  cin>>n>>m;
  LL sum=0;
  for(int i=1;i<=n;++i)
  {
    cin>>a[i];
    sum+=a[i];
  }
  for(int i=1;i<=n;++i)
    w[i]=get_val(a[i]);
  for(int i=1;i<=n;i++)
    if(w[i]>=a[i])
      st[i]=true;
  int res=0;
  vector<int>ans;
  for(int i=1;i<=n;i++)
  {
    if(st[i]&&!st[i+1])
    {
      res+=w[i]-a[i];
      ans.push_back(res);
      res=0;
    }
    else if(st[i]&&st[i+1])
      res+=w[i]a[i];}sort(ans.begin(),ans.end(),cmp);
    for(int i=0;i<m&&i<ans.size();i++)
      sum+=ans[i];
    cout<<sum;//输出
    return 0;
}

求过审。

原文
存档评论3 条抓取于