news 2026/6/10 18:30:58

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/10 11:58:06

该如何选择深圳进行算力服务器托管

在数字经济高速迭代的当下&#xff0c;算力已成为企业核心竞争力&#xff0c;而服务器托管作为保障算力稳定输出的关键载体&#xff0c;其选址与服务商选择直接影响业务连续性。深圳作为全球互联网骨干网核心节点、粤港澳大湾区数字枢纽&#xff0c;凭借得天独厚的网络资源、完…

作者头像 李华
网站建设 2026/6/9 20:15:00

i386 CPU页式存储管理深度解析

深入理解i386 CPU页式存储管理&#xff1a;原理、实现与核心思路 在x86架构的发展历程中&#xff0c;i386 CPU首次引入了完整的32位页式存储管理机制&#xff0c;为现代操作系统的虚拟内存、进程隔离、内存保护等核心功能奠定了硬件基础。与早期实模式的内存管理及286的段式保…

作者头像 李华
网站建设 2026/6/9 22:29:47

我的思维模型 -- 6.生物学篇

生物学自然选择 - 适者生存能活下来的&#xff0c;不是最聪明的&#xff0c;而是最能适应环境变化的《自私的基因》最好不要把自然选择的基本单位看作物种或者种群&#xff0c;甚至个体&#xff1b;最好把它看作遗传物质的某种小单位。为方便起见&#xff0c;简称为基因世界运行…

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

PyTorch torch.optim 优化器介绍与论文

目录概述常用优化器1. **SGD** (Stochastic Gradient Descent) - 随机梯度下降2. **Adam** (Adaptive Moment Estimation) ⭐ 最常用3. **AdamW** (Adam with Weight Decay) ⭐ PI0.5 使用4. **RMSprop** (Root Mean Square Propagation)5. **Adagrad** (Adaptive Gradient)6. …

作者头像 李华
网站建设 2026/6/10 12:00:04

2026现在这个时代,C语言真的不行了吗?

C语言在2026年&#xff08;以及可预见的未来&#xff09;绝对没有“不行了”&#xff0c;它依然至关重要且不可替代。 那些宣称C语言“不行”或“过时”的说法&#xff0c;往往忽略了它在现代计算基础设施中扮演的核心、底层、高性能角色。C语言在2026年依然强大且不可或缺的原…

作者头像 李华
网站建设 2026/6/10 11:57:44

人工智能治安管控系统

人工智能治安管控系统 基于大数据平台的人工智能治安管控系统&#xff0c;实现人脸识别和视频行为分析功能。通过人脸识别技术&#xff0c;实现实时监控、路人抓拍、人脸库检索、重点人员布控、路人检索、报警信息查询&#xff1b;采用视频行为分析技术&#xff0c;对非法闯入、…

作者头像 李华