news 2026/4/16 14:22:32

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/4/16 14:22:17

win11卸载sql server express版本

1. 停止服务 步骤关键操作说明 / 注意1. 停止服务停止所有 “SQL Server (实例名)” 等相关服务。防止文件被占用导致无法删除。 2. 卸载程序 步骤关键操作说明 / 注意2. 卸载程序在“控制面板”-“程序和功能”中&#xff0c;卸载所有名称包含 “SQL Server” 和 “Native C…

作者头像 李华
网站建设 2026/4/8 15:22:54

基于VLAN标签的网络访问控制实验报告

实验配置步骤交换机LSW1配置交换机LSW2配置交换机LSW3配置路由器AR1配置访问控制验证&#xff08;ping测试&#xff09;pc1 ping pc6 pc3访问pc23456pc2访问pc456pc4可以访问pc5&#xff0c;不能访问pc6pc5不能访问pc6

作者头像 李华
网站建设 2026/4/12 23:25:37

双非本能搞智驾吗?座舱相关开发怎样?

前言 注&#xff1a;文中所说观点&#xff0c;系个人胡扯观点。观看如有不适&#xff0c;既可私信阿甘本人删文。 星球同学提问 halo甘哥&#xff0c;想问问 1⃣️双非本适合走自动驾驶的开发方向吗 2⃣️如果想去一个比较知名的车企实习&#xff0c;开发岗如果进不去&#xff…

作者头像 李华
网站建设 2026/4/15 12:28:55

项目实战: LAMP-电商平台-iwebshop

LAMP-电商平台-iwebshop # 配置服务器流程&#xff1a; # 1. 安装软件包 # 2. 准备相关材料 # 3. 修改配置文件 # 4. 启用并启动服务 # 5. 设置防火墙 # 6. 客户端测试####################################################### 10.1.8.11 client.migaomei.cloud client 10.1.…

作者头像 李华
网站建设 2026/4/14 8:43:56

Matplotlib可视化语法深度解析:从API设计到高级定制

Matplotlib可视化语法深度解析&#xff1a;从API设计到高级定制 引言&#xff1a;超越plt.plot()的Matplotlib世界 当开发者提起Matplotlib时&#xff0c;脑海中首先浮现的往往是plt.plot(x, y)这样的简单调用。然而&#xff0c;这个看似简单的可视化库背后隐藏着一个精心设计…

作者头像 李华