news 2026/6/12 16:38:17

Codeforces Round 1068 (Div. 2) D,E 题解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Codeforces Round 1068 (Div. 2) D,E 题解

D. Taiga’s Carry Chains

Miracles don’t happen to those who just wait.

— Toradora!

After classes at Ohashi High School, Ryuuji hands Taiga a positive integern nnand sets a simple challenge.

They will play for exactlyk kkmoves. In a single move, Taiga chooses a non-negative integerℓ \elland setsn ← n + 2 ℓ n \gets n + 2^{\ell}nn+2.

Ryuuji defines the score of one move as the number of binary carries that occur when adding2 ℓ 2^{\ell}2to the current number in base2 22. The total score is the sum of score over allk kkmoves.

Taiga wants the total score to be as large as possible afterk kkmoves. What is the maximum total score she can achieve?

大桥高中放学后,龙寺递给大河一个正整数n并设定了一个简单的挑战。

他们将精确执行 k 步。在一次移动中,Taiga 选择一个非负整数 ℓ 并设置n ← n + 2 ℓ n \gets n + 2^{\ell}nn+2.

Ryuuji 将一步棋的分数定义为将 2ℓ 添加到基数 2 中的当前数字时发生的二进制进位数。总得分是所有 k 动作的得分总和。

大河希望 k 移动后总得分尽可能大。她能达到的最高总分是多少?

题解:

可以发现一些性质,就是如果步骤足够大,可以把所有的二进制中的 0 全部填写为 1,这就是一个贪心上的最优解,但是我们往往没有那么大的步骤数,则此时可以有一些转化。

下面介绍两种思路:

voidsolve(){intn,k;cin>>n>>k;intB=__lg(n)+1,z=B-__popcount(n);if(k>z){cout<<k-z+B-1<<endl;return;}vector<int>g,num;for(inti=__lg(n);i>=0;i--){if((n>>i&1)==0){intj=i-1;while(j>=0&&((n>>j&1)==0))j--;j++;g.push_back(i-j+1);num.push_back((1LL<<(i+1))-1-((1LL<<j)-1));i=j;}}intans=0LL;intM=1LL<<(int)g.size();for(intmask=0;mask<M;mask++){intm=n,co=0LL;for(intbit=0;bit<(int)g.size();bit++){if(mask>>bit&1){m|=num[bit];co+=g[bit];if(co>=k){break;}}}if(co>=k)continue;intnum=k-co,res=0;vector<int>tem;for(inti=__lg(m);i>=0;i--){if(m>>i&1){intj=i-1;while(j>=0&&(m>>j&1))j--;j++;tem.push_back(i-j+1);i=j;}}sort(range(tem),greater<int>());for(inti=0;i<min((int)tem.size(),num);i++){res+=tem[i];}res+=max(0LL,num-(int)tem.size());ans=max(ans,res);}cout<<ans<<endl;}
#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintinf=1e9+7;constintmxb=64,mxk=32;intdp[mxb+2][mxk+2][2];voidMin(int&x,inty){x=min(x,y);}voidsolve(){intn,k;cin>>n>>k;intpc=__builtin_popcount(n);if(n==0){cout<<max(0ll,k-1)<<'\n';return;}if(k>=32){cout<<k+pc-1<<'\n';return;}for(inti=0;i<=mxb;i++)for(intj=0;j<=k;j++)dp[i][j][0]=dp[i][j][1]=inf;dp[0][0][0]=0;for(inti=0;i<mxb;i++){intni=(n>>i)&1ll;for(intu=0;u<=k;u++)for(intc=0;c<=1;c++){intcur=dp[i][u][c];if(cur>=inf)continue;{intsum=ni+c,bit=sum&1,nc=sum>>1;Min(dp[i+1][u][nc],cur+bit);}if(u+1<=k){intsum=ni+1+c,bit=sum&1,nc=sum>>1;Min(dp[i+1][u+1][nc],cur+bit);}}}intbst=inf;for(intu=0;u<=k;u++)for(intc=0;c<=1;c++){intval=dp[mxb][u][c];if(val<inf)Min(bst,val+c);}intans=k+pc-bst;cout<<ans<<'\n';}signedmain(){ios_base::sync_with_stdio(false);cin.tie(0),cout.tie(0);intT;cin>>T;for(;T--;)solve();return0;}

这里 dp 表示最小的 1 的个数,从最低位 -> 最高位进行转移,d p [ i ] [ j ] [ c ] dp[i][j][c]dp[i][j][c]表示从低位开始处理到了第 i 位,进行了 j 次操作,且当前位置有没有从上一位进行进位,的最小1的个数。

sum 表示当前位考虑进位,考虑 +1 之后的值,然后 bit 表示本位最后是啥,nc 表示进位没有。

然后就转移就行了。

E. Shiro’s Mirror Duel

time limit per test: 3 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

There’s no such thing as luck in this world. The victor is decided before the game even starts.

— No Game No Life

This is an interactive problem.

One day, Sora and Shiro feel bored again, so they decide to settle it with a game.

At the beginning, Sora gives Shiro a permutation∗ ^{\text{∗}}p 1 , p 2 , … , p n p_1,p_2,\ldots,p_np1,p2,,pnof lengthn nn. In each operation, Shiro may select two distinct indicesx xxandy yy(1 ≤ x ≠ y ≤ n 1\le x\ne y\le n1x=yn). Then Sora flips a fair coin:

After the operation, Sora replies with the actual pair of indices that were swapped, so that Shiro can update her local permutation accordingly.

Shiro’s goal is to sort the permutationp ppin ascending order by using at most⌊ 2.5 n + 800 ⌋ \lfloor 2.5n+800\rfloor2.5n+800operations. Help her!

∗ ^{\text{∗}}A permutation of lengthn nnis an array consisting ofn nndistinct integers from1 11ton nnin arbitrary order. For example,[ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4][2,3,1,5,4]is a permutation, but[ 1 , 2 , 2 ] [1,2,2][1,2,2]is not a permutation (2 22appears twice in the array), and[ 1 , 3 , 4 ] [1,3,4][1,3,4]is also not a permutation (n = 3 n=3n=3but there is4 44in the array).

题解写的很清楚的,主要思路来就是在一种看似随机的过程中找到一个等式,使得最后一定能完成,代码:

PIIquery(intx,inty){cout<<"? "<<x<<' '<<y<<endl;PII res;cin>>res.first>>res.second;returnres;}voidsolve(){intn;cin>>n;vector<int>p(n+1),pos(n+1);automirror=[&](intx)->int{returnn-x+1;};for(inti=1;i<=n;i++){cin>>p[i];pos[p[i]]=i;}if(n&1){while(pos[(n+1)/2]!=(n+1)/2){auto[u,v]=query(pos[(n+1)/2],(n+1)/2);swap(pos[p[u]],pos[p[v]]);swap(p[u],p[v]);}}for(inti=1;i<=n;i++){if(pos[i]+pos[mirror(i)]!=n+1){auto[u,v]=query(pos[i],mirror(pos[mirror(i)]));swap(pos[p[u]],pos[p[v]]);;swap(p[u],p[v]);}}for(inti=1;i<=n;i++){while(pos[i]!=i||pos[mirror(i)]!=mirror(i)){if(pos[i]!=i){auto[u,v]=query(i,pos[i]);swap(pos[p[u]],pos[p[v]]);;swap(p[u],p[v]);}else{auto[u,v]=query(mirror(i),pos[mirror(i)]);swap(pos[p[u]],pos[p[v]]);;swap(p[u],p[v]);}}}cout<<"!"<<endl;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/4 3:08:50

SharedPreferences

SharedPreferencesSharedPreferences是一个接口&#xff0c;需通过Context提供的getSharedPreferences(String name,int mode)方法来获取SharedPreferences实例。SharedPreferences数据总是保存在/datta/data/<package name>/shared_prefs目录下。SharedPreferences数据总…

作者头像 李华
网站建设 2026/5/28 8:46:33

vue.3

1.局部注册只能在注册的组件内使用&#xff08;1&#xff09;创建vue文件&#xff08;2&#xff09;在使用的组件内导入并注册2.全局注册所有组件内都能使用&#xff08;1&#xff09;创建vue文件&#xff08;2&#xff09;main.js中进行全局注册3.组件的样式冲突scoped全局注册…

作者头像 李华
网站建设 2026/6/9 11:42:46

centos7-nvidia驱动安装及简单测试

centos7-nvidia驱动安装类别信息服务器型号Rack Mount Chassis NF5280M6CPUIntel Xeon Silver 4310 CPU 2.10GHz * 2系统版本Centos 7系统内核版本3.10.0-1160.el7.x86_64GPU型号NVIDIA A100&#xff08;40G&#xff09;*4Nvidia版本525.85.05CUDA版本12.0.0docker版本20.10.9…

作者头像 李华
网站建设 2026/6/12 0:18:19

BUCK-BOOST升降压电路Multisim电路仿真分析

目录 0 概述 工作过程简述 Q1 导通阶段 Q1 关断阶段 稳态输出 主要元件作用 电路特点 1 过程分析: 第一次开关闭合时 第二次Q1闭合时C1放电 2 Multisim仿真分析 占空比D=25% 占空比D=60% 摘要 本文分析了三极管型BUCK-BOOST升降压开关电源的工作原理。该电路结…

作者头像 李华
网站建设 2026/6/10 14:38:31

内存固态等大幅涨价的优势是什么?

原本电子垃圾又有了些许价值升华。 排名上升 现在有部分旧电脑重新启用。看大数据的排名结果&#xff1a; https://blog.csdn.net/ZhangRelay/article/details/155345614 这是去年&#xff0c;2025年11月28日测试的结果&#xff0c;2%排位&#xff0c;非常低端了。 预料之中的…

作者头像 李华