当前位置:网站首页>Codeworks round 650 (Div. 3) ABCD solution
Codeworks round 650 (Div. 3) ABCD solution
2022-07-25 00:32:00 【Think-killer】
Topic link :
A. Short Substrings
B. Even Array
C. Social Distance
D. Task On The Board
A. Short Substrings
The question : The string a All the lengths of are 2 The substring of is connected to form a new string , Calculate the original string a
Answer key : Just skip the repeated
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int t;
int main()
{
cin>>t;
while(t--){
string a;
cin>>a;
cout<<a[0];
for(int i=1;i<a.size();i++){
cout<<a[i];
if(i%2==1) i++;// Remove duplicate
}
cout<<endl;
}
return 0;
}
B. Even Array
The question : Swap element positions in the array , Make subscripts and elements parity , If you can output the number of exchanges , Otherwise output -1.
Answer key : Record the number of odd elements with even subscripts (q) And subscripts are even numbers with odd elements (w), If q=w, The output q, Anyway -1.
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a;
int t,n;
int main()
{
cin>>t;
while(t--){
cin>>n;
int q=0,w=0;
for(int i=0;i<n;i++){
cin>>a;
if(i%2==0&&a%2==1) q++;
else if(i%2==1&&a%2==0) w++;
}
if(q==w) cout<<q<<endl;
else if(q!=w) cout<<-1<<endl;
}
return 0;
}
C. Social Distance
The question : Given a binary string , Make sure that each 1 Interval between k individual 0, Ask how many can be put 1.
Answer key : Talk about four situations :
1、 At present k+1 All for 0 when , Add... At the first position 1;
2、 If there is continuity in the middle 2k+1 individual 0 when , Add 1;
3、 If there is continuity at the end of the string k+1 And above individual 0 when , Add... At the end 1;
4、 If the string length is equal to k, The answer is 1;
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a,x;
int t,n,k;
int main()
{
cin>>t;
while(t--){
cin>>n>>k;
int q=0,w=0,c=0;
char x;
for(int i=0;i<n;i++){
cin>>x;
if(x=='0') w++,q++;
else if(x=='1') q=0;
if(q==k+1&&i==k) c++,q=k;
// Case one
else if(q==2*k+1) c++,q=k;
// The second case
}
if(q>=k+1) c++;
// The third case
if(w==n&&c==0) c++;
// The fourth situation
cout<<c<<endl;
}
return 0;
}
D. Task On The Board
The question :For example, if t = “abzb”, then:
since t1=‘a’, all other indices contain letters which are later in the alphabet, that is: b1=|1−2|+|1−3|+|1−4|=1+2+3=6;
since t2=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b2=|2−3|=1;
since t3=‘z’, then there are no indexes j such that tj>ti, thus b3=0;
since t4=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b4=|4−3|=1.
Thus, if t = “abzb”, then b=[6,1,0,1].
Answer key : Be careful b[j]=0 The point of , Because of 0, So he should put the maximum value .
We just need to put the largest letter in b[j]==0 It's about , Other places should be smaller than him , So the numbers in other places will become b[i]-=abs(i-j), And so on , Every time for 0 The points of all the remaining letters are the largest and the number is enough to be letters , Just keep cycling until the end .
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int q,m,x,b[60],v[27];
string a;
int vis[60];
char da[60];
int main()
{
cin>>q;
while(q--){
memset(v,0,sizeof v);
memset(vis,0,sizeof vis);
memset(da,'0',sizeof da);
cin>>a>>m;
for(int i=1;i<=m;i++) scanf("%d",&b[i]);
for(int i=0;i<a.size();i++) v[a[i]-'a'+1]++;
// Subscripts indicate the size of letters , The value represents the number
int num=0,zero=0,k=27;
while(num!=m){
// Judge m Whether all positions have been placed
zero=0;
int c=0;
for(int i=1;i<=m;i++) if(b[i]==0&&vis[i]==0) zero++;
// Record is currently 0 Number of
for(int i=k-1;i>0;i--)
if(v[i]>=zero) {
k=i;break;}
// Look for the largest and sufficient number of letters
for(int i=1;i<=m;i++) {
if(b[i]==0&&vis[i]==0){
vis[i]++;num++;
// Record the location and number of storage
da[i]=k+'a'-1;
// Store letters in
}
}
for(int i=1;i<=m;i++){
if(b[i]==0&&da[i]==k+'a'-1)
for(int j=1;j<=m;j++)
if(b[j]) b[j]-=abs(j-i);
// Other positions should be smaller than the currently added letters ,
// So the position is poor
}
}
for(int i=1;i<=m;i++) cout<<da[i];
cout<<endl;// Output
}
return 0;
}
边栏推荐
- [nuxt 3] (x) runtime configuration
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- 剖析kubernetes集群内部DNS解析原理
- QT project - security monitoring system (function realization of each interface)
- Usage of atomicinteger (counter)
- [help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
- Heap and stack in embedded development
- Daily question 1 · 1260. Two dimensional network migration · simulation
- 自动化测试系列-Selenium三种等待详解
- [leetcode weekly replay] game 83 biweekly 20220723
猜你喜欢

Branch and loop statements in C language learning

Dpdk based basic knowledge sorting-01

Soft test --- fundamentals of programming language (Part 2)

Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现

Quartus: install cyclone 10 LP device library for quartus version 17.1

Heap and stack in embedded development

UART

Which automation tools can double the operation efficiency of e-commerce?

Automated test series selenium three kinds of waiting for detailed explanation

C语言学习之分支与循环语句
随机推荐
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
Leetcode 0125. validate palindrome string
Redis memory analysis tool RMA usage
Dynamic programming-01 knapsack rolling array optimization
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
Live broadcast preview | online seminar on open source security governance models and tools
采坑记录:TypeError: 'module' object is not callable
Regular expression learning
Two numbers that appear only once in the array
Internal network mapping port to external network
LeetCode_392_判断子序列
GUI basic application
C language: deep analysis function stack frame
R language uses ISNA function to check whether the list and dataframe contain missing values, marks abnormal values in data columns in dataframe as missing values Na, and uses na.omit function to dele
Which automation tools can double the operation efficiency of e-commerce?
Promtool Check
[leetcode weekly replay] 303rd weekly 20220724
Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现
[mindspore ascend] [user defined operator] graph_ In mode, customize how to traverse tensor
Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库