当前位置: 首页 > news >正文

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;}
http://www.jsqmd.com/news/311363/

相关文章:

  • Educational Codeforces Round 129 (Rated for Div. 2) F. Unique Occurrences 线段树分治
  • Android System UI/Launcher开发工程师职位深度解析
  • 2026年1月电缆生产厂家推荐:电缆生产厂家排名、知名的电缆生产厂家推荐盘点名单
  • 深入解析:Android开发工程师的核心能力与实战面试指南
  • 2026年质量好的数码组合柔版印刷机/标签柔版印刷机信誉优质供应参考(可靠)
  • 亿航智能安卓工程师岗位深度解析与面试指南
  • 2026年评价高的亚克力浴缸/浴缸厂家口碑推荐汇总
  • 2026年知名的卫星式轮转印刷机/标签轮转印刷机厂家推荐与选择指南
  • 铁路地铁电力电缆生产厂家推荐:中低压、变频、聚乙烯绝缘、聚氯乙烯绝缘电缆生产厂家名单(2026年)
  • 2026年1月矿山煤矿电力电缆生产厂家推荐:中低压、变频、聚乙烯绝缘电缆
  • 2026年1月中国电缆一线品牌推荐:轨道交通、石油石化、矿山煤矿电缆国内一线品牌盘点
  • 2026年1月轨道交通电力电缆生产厂家推荐:中低压、变频、变频、聚乙烯绝缘电缆生产厂家
  • 2026年靠谱的酒店照明系统/酒店照明设计优选服务榜
  • 2026年口碑好的工程浮箱钢模板/圆柱钢模板厂家选购参考汇总
  • 2026年专业的酒店灯具工程采购/酒店灯具主流品牌排行榜
  • px4+ubuntu22.04+ros2开发记录
  • 2026年比较好的建筑3D打印构件/建筑3D打印技术厂家选择参考建议
  • 2026年热门的办公设备塑料齿轮/汽车塑料齿轮厂家推荐与选择指南
  • 2026年靠谱的大连考公线下课/大连考公线上课学员好评榜
  • OCR文字识别组件的深度解析:从传统方法到Vision Transformer与Diffusion的融合
  • 2026年评价高的安全气囊发生器外壳钢管/钢管厂家专业度参考(精选)
  • 2026年知名的大连公考银行编/大连公考省考班学员选择榜
  • 2026年热门的五金冲压弹片成型/精密加工五金冲压优质供应商推荐参考
  • 2026年质量好的气泡轻质土/高强度气泡轻质土厂家热销推荐
  • 2026年知名的全屋净水设备/全屋净水过滤系统用户口碑认可参考(高评价)
  • 2026年知名的反渗透净水器/净水器制造厂家
  • 2026年比较好的高压水阻起动柜/高压励磁起动柜值得信赖厂家推荐(精选)
  • 2026年口碑好的拉伸弹簧/触指弹簧高评价厂家推荐
  • 2026年评价高的路基回填泡沫混凝土/泡沫混凝土厂家选购完整指南
  • 2026年知名的手表盒/塑胶手表盒厂家信誉综合参考