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

Codeforces Round 1049 (Div. 2)

时隔这么久 重温Div.2的难度的比赛还是变得有点棘手了 还是得一直练保持手感💪

比赛链接:点击此处进入

A题:

You are given a binary string∗^{\text{∗}} sss of length nnn and you are allowed to perform the following operation any number of times (including zero):

  • Choose 333 indices KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le i &̲lt; j < k \l… and right shift or left shift the values on sis_isi, sjs_jsj, sks_ksk cyclically.

For the binary string 110110, if we choose i=1i=1i=1, j=2j=2j=2, k=3k=3k=3 and perform a right shift cyclically, the string becomes 011110; if we choose i=4i=4i=4, j=5j=5j=5, k=6k=6k=6 and perform a left shift cyclically, the string becomes 110101.

Determine the minimum number of operations required to sort the given binary string.

∗^{\text{∗}}A binary string is a string that consists only of the characters 0 and 1.
Input

Each test contains multiple test cases. The first line contains the number of test cases ttt (1≤t≤1001 \le t \le 1001t100). The description of the test cases follows.

The first line of each test case contains a single integer nnn (3≤n≤1003 \le n \le 1003n100) — the length of the string.

The second line contains a binary string sss of length nnn.
Output

For each test case, output a single integer — the minimum number of operations required to sort the given binary string.
Note

For the first test case, the given string is already sorted. So, no operations are needed.

For the second test case, we can choose i=1i = 1i=1, j=2j = 2j=2, k=4k = 4k=4 and perform a right shift cyclically. The string becomes equal to 0011, which is sorted.

由于我们可以选择的三个数是可以不连续的,所以我们每次都可以选择不在应有位置上的元素进行操作,那么这个时候我们就要考虑对原字符串进行排序操作了,排序之后才能通过与元字符串进行对比找出不在应在位置的元素的个数。
然后就是考虑怎么对这些元素进行操作呢?我们可以手写几个例子就能看出规律,因为循环移位既可以向左又可以向右,所以这个时候任意三个元素都可以在一次操作内变得有序,那么到这里可能就会想,答案是不是让这些元素的个数除以3呢?
其实这里也有可以细节:我们不难发现,不在该在位置的元素的个数一定是偶数个,然后我们还能够发现,实质上,我们的每次操作,对其中的三个元素进行操作的时候,其实只有两个位置上的元素互换位置了,总会有一个元素的位置保持不变,所以每一次操作实质上只有两个元素在起作用,所以最终的答案就是cnt / 2!

// Problem: A. Shift Sort
// Contest: Codeforces - Codeforces Round 1049 (Div. 2)
// URL: https://codeforces.com/contest/2140/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int>
#define fi first
#define se second
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define lbt(x) ((x) & (-x)) const int INF = 0x3f3f3f3f;
const int inf = 1e18; void solve()
{int n;cin>>n;string s;cin>>s;string t = s;sort(t.begin(),t.end());int cnt = 0;// cout<<s<<endl<<t<<endl;for(int i=0;i<n;i++)if(s[i] != t[i]) cnt ++;// cout<<cnt<<endl;cout<<cnt / 2 <<endl;
//	cout<<fixed<<setprecision(x)<< ; 
}signed main()// Don't forget pre_handle!
{IOSint T=1;cin>>T;while(T--) solve(); return 0;
} 

B题

Alice and Bob are playing a game in which Alice has given Bob a positive integer KaTeX parse error: Expected 'EOF', got '&' at position 2: x&̲lt;10^8.

To win the game, Bob has to find another positive integer KaTeX parse error: Expected 'EOF', got '&' at position 2: y&̲lt;10^9 such that x#⁡yx \operatorname{\#} yx#y is divisible by x+yx + yx+y.

Here x#⁡yx\operatorname{\#}yx#y denotes the integer formed by concatenating the integers xxx and yyy in that order. For example, if x=835x = 835x=835, y=47y = 47y=47, then x#⁡y=83547x \operatorname{\#} y = 83\,547x#y=83547.

However, since Bob is dumb, he is unable to find such an integer. Please help him.

It can be shown that such an integer always exists.
Input

Each test contains multiple test cases. The first line contains the number of test cases ttt (1≤t≤1041 \le t \le 10^41t104). The description of the test cases follows.

The only line of each test case contains a single integer xxx (KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le x &̲lt; 10^8) — the integer that Alice has given to Bob.
Output

For each test case, print a single integer yyy (KaTeX parse error: Expected 'EOF', got '&' at position 9: 1 \le y &̲lt; 10^9) so that Bob can win the game.

If there are multiple answers, print any one of them.
Note

For the first test case, x=8x = 8x=8, we can choose y=1y = 1y=1, and we have x#⁡y=81x \operatorname{\#} y = 81x#y=81, which is divisible by x+y=9x + y = 9x+y=9.

For the second test case, x=42x = 42x=42, we can choose y=12y = 12y=12, and we have x#⁡y=4212x \operatorname{\#} y = 4212x#y=4212, which is divisible by x+y=54x + y = 54x+y=54.

这道题是和数学相关的一道题,这里给大家分享一个小技巧:
我们对于这类题目可以用数学表达式来将过程模拟出来,就拿这道题来说:

  • 首先题目中所给定的元素x,我们在与y进行拼接操作之后的数学表达式为:x*10^len+y
  • 其中len表示x的位数
  • 然后,我们要想使之能够被x + y整除,那么这时候的数学表达式就是:(x*10^len+y) / (x+y)
  • y的取值肯定不是随便乱取的,一定和x有着某种倍数关系才能整除
  • 那我们就开始想,到底是多少倍才合适?
  • 看到10^len,我们就能想到对3作除法,因为不管是10的几次方,对3作除法都是余1!
  • 因为x被的10的k次方对3倍的x作除法少两个x才能整除,所以y就直接取的2倍的x即可!
    代码:
// Problem: B. Another Divisibility Problem
// Contest: Codeforces - Codeforces Round 1049 (Div. 2)
// URL: https://codeforces.com/contest/2140/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int>
#define fi first
#define se second
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define lbt(x) ((x) & (-x)) const int INF = 0x3f3f3f3f;
const int inf = 1e18; void solve()
{int x;cin>>x;cout<<2LL * x<<endl;
//	cout<<fixed<<setprecision(x)<< ; 
}signed main()// Don't forget pre_handle!
{IOSint T=1;cin>>T;while(T--) solve(); return 0;
} 

文章转载自:

http://AowgqDNu.mqLsf.cn
http://oTGM9s3P.mqLsf.cn
http://ASVBdgWU.mqLsf.cn
http://AlfQICoS.mqLsf.cn
http://BYCYThKH.mqLsf.cn
http://Mkrv6208.mqLsf.cn
http://zBNewrXc.mqLsf.cn
http://K0MKymjJ.mqLsf.cn
http://M85JZ2nf.mqLsf.cn
http://6HuZuZCN.mqLsf.cn
http://fUKN67sS.mqLsf.cn
http://S8UJdcPw.mqLsf.cn
http://DHCNTiz8.mqLsf.cn
http://d3a8vBtD.mqLsf.cn
http://J1gK8Xtt.mqLsf.cn
http://wQ2CfjUC.mqLsf.cn
http://E52k7ucF.mqLsf.cn
http://DzbAeVwj.mqLsf.cn
http://nr6UTtfC.mqLsf.cn
http://MO1bJsht.mqLsf.cn
http://NRwjvcjU.mqLsf.cn
http://sFvC6CY4.mqLsf.cn
http://8glcHM5u.mqLsf.cn
http://dav3swZK.mqLsf.cn
http://V4t5VWff.mqLsf.cn
http://49hPQKjL.mqLsf.cn
http://3zZlGV0G.mqLsf.cn
http://dFnRCYBK.mqLsf.cn
http://iTeFNxMj.mqLsf.cn
http://KUwrtLCe.mqLsf.cn
http://www.dtcms.com/a/377390.html

相关文章:

  • Eclipse下载安装图文教程(非常详细,适合新手)
  • vue2迁移到vite[保姆级教程]
  • 基于webpack的场景解决
  • Vite 中的 import.meta.env 与通用 process.env.NODE_ENV 的区别与最佳实践
  • 除了Webpack,还有哪些构建工具可以实现不同环境使用不同API地址?
  • sklearn聚类
  • I.MX6UL:汇编LED驱动实验
  • 计算机毕设 java 高校机房综合管控系统 基于 SSM+Vue 的高校机房管理平台 Java+MySQL 的设备与预约全流程系统
  • 设计模式-建造者观察者抽象工厂状态
  • 第5讲 机器学习生态构成
  • JAVA秋招面经
  • LVS群集
  • 半导体功率器件IGBT工艺全流程
  • Q3.1 PyQt 中的控件罗列
  • 深入解析ReentrantLock:可重入锁
  • ARM处理器总线架构解析:iCode、D-code与S-Bus
  • Qoder 前端UI/UE升级改造实践:从传统界面到现代化体验的华丽蜕变
  • Flutter多线程
  • 如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
  • JavaScript 对象说明
  • CMake目标依赖关系解析
  • 小型企业常用的元数据管理工具
  • 论文AI写作哪个软件好?实测对比5款热门AI写作工具
  • PostgreSQL 内机器学习的关键智能算法研究
  • 12公里无人机图传模组:从模糊到超高清的飞跃,抗干扰能力全面升级
  • GitHub Actions中steps下面的Setup environment设置的环境变量不能在后面步骤使用问题处理
  • YOLOv5实战-GPU版本的pytorch虚拟环境配置
  • 苍穹外卖项目实战(day7-2)-购物车操作功能完善-记录实战教程、问题的解决方法以及完整代码
  • 【VsCode】离线状态下安装插件
  • 浏览器开发CEFSharp (十七)网页自定义下载—仙盟创梦IDE