当前位置:网站首页>Codeworks round 649 (Div. 2) ABC problem solution
Codeworks round 649 (Div. 2) ABC problem solution
2022-07-25 00:33:00 【Think-killer】
A. XXXXX
Topic link :A. XXXXX
The question : Find the array a[ ] The largest subarray of , And this subarray can only Delete a few from the beginning ( It could be zero or all ) Element or Delete a few from the end ( It could be zero or all ) Elements ., Make the subarray unusable x to be divisible by .
Answer key **: We can know if a number can be x to be divisible by , Then subtract one that cannot be x This number cannot be divided by x Divided by .**
There are three situations :1.a[ ] Array sum cannot be x to be divisible by , The answer for n;
2.a[ ] The sum of arrays cannot be x Divide by whole and there is something in the array that cannot be x Divisible number , The answer for n Subtract the smallest element that includes this number , namely n-min(i,n-1+1)(i Subscript for this number );
3. The answer for 0.
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100010;
int n,x;
int a[N];
long long int sum;
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&x);
int f=0,b=100000,i,sum=0;
for(i=1;i<=n;i++) {
scanf("%d",&a[i]);
sum+=a[i];// Finding the sum of arrays
if(a[i]%x!=0){
b=min(i,b);
// Record the minimum distance from the beginning of such a number
b=min(b,n-i+1);
// Record the minimum distance from the end of such a number
f=1;
}
}
if(sum%x!=0) cout<<n<<endl;
// Case one
else if(f) cout<<n-b<<endl;
// The second case
else cout<<-1<<endl;
// The third case
}
return 0;
}
B. Most socially-distanced subsequence
Topic link :B. Most socially-distanced subsequence
The question : Find the array p[ ] The subsequence , This subsequence can delete some ( May be zero or all ) Elements .|s1−s2|+|s2−s3|+…+|sk−1−sk| And as large as possible .p At least the length 2.
In all these subsequences , Choose a length ,k, The smaller the better. .
Answer key : It's not hard to see In a monotonic array , The difference between the beginning and the end of the array is equal to the sum of the differences of the elements in this array .
So this problem can be regarded as finding the beginning and end of each monotonic interval in the whole array .
You can use it here Difference to judge monotony . Greater than 0 Single increase , Less than 0 Is a single minus .
Code ;
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100010;
int n,x;
int a[N],b[N],c[N];
int check(int x){
if(x>0) return 1;
else return 0;
}// Use the difference array to judge the monotonicity of elements
int main()
{
int t;
scanf("%d",&t);
while(t--){
x=0;
scanf("%d",&n);
scanf("%d",&a[0]);
b[0]=a[0];
for(int i=1;i<n;i++) {
scanf("%d",&a[i]);
b[i]=a[i]-a[i-1];
// Differential array calculation
}
int t=1;
c[x++]=a[0];
for(int i=2;i<n;i++){
if(check(b[i])==check(b[i-1])) continue;
// Judge whether it is a monotonous beginning or end
else t++,c[x++]=a[i-1];
// If so, save it
}
t++,c[x++]=a[n-1];
// Add the end of the monotone interval
cout<<t<<endl;
for(int i=0;i<x;i++) cout<<c[i]<<" ";
// Output the answer
cout<<endl;
}
return 0;
}
C. Ehab and Prefix MEXs
Answer key : hold a The number appearing in is recorded with another array subscript , Then fill in the numbers that have never appeared and b Array , because a It must be an ordered array , So we have to make a special judgment , If a Array a[i]>a[i-1], Need to put a[i-1] Put it in b[i] It's about , The purpose is to guarantee b[i] Array MEX Will not be a[i-1]!!!
#include <bits/stdc++.h>
using namespace std;
int a[100005],b[100005];
bool ex[100005];
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(b,-1,sizeof(b));
for (int i=1;i<=n;i++)
{
if (a[i]!=a[i-1])
{
b[i]=a[i-1];
ex[b[i]]=1;
}
}
ex[a[n]]=1;
int m=0;
for (int i=1;i<=n;i++)
{
while (ex[m])
m++;
if (b[i]==-1)
{
b[i]=m;
ex[m]=1;
}
printf("%d ",b[i]);
}
}
边栏推荐
- GUI basic application
- [leetcode weekly replay] 303rd weekly 20220724
- LeetCode_ 6124_ The first letter that appears twice
- Fast development board for Godson solid state drive startup (burning system to solid state) - partition
- The use of where condition in MySQL is not equal to! = The problem that null values are filtered out occurs when in, etc
- [mindspore ascend] [user defined operator] graph_ In mode, customize how to traverse tensor
- Codeworks round 650 (Div. 3) ABCD solution
- The troubleshooting process of a segment error (disassembly address troubleshooting)
- Docker container Django + MySQL service
- Lambda&Stream
猜你喜欢

ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration

Moonpdflib Preview PDF usage record

数组中只出现一次的两个数字

自动化测试系列-Selenium三种等待详解

在混合云中管理数据库:八个关键注意事项

The font changes with the change of the form

The model needs to use two losses_ FN, how to operate?
![[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU](/img/b5/c02ef57526d208b02dfa00189e9ecb.jpg)
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU

NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided

Dynamic programming-01 knapsack rolling array optimization
随机推荐
LeetCode_ 6124_ The first letter that appears twice
BGP machine room and BGP
Detailed explanation of alexnet of paddlepaddle paper series (with source code)
[Bert] transformer/bert/attention interview questions and answers
Verification of Kirchhoff's law and Multisim Simulation (engineering documents attached)
Detailed usage of iperf
Two numbers that appear only once in the array
Multi merchant mall system function disassembly Lecture 14 - platform side member level
How to use measurement data to drive the improvement of code review
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现
This visual is not connected to the presentationsource.
EF core: self referencing organizational structure tree
Simple operation K6
Nodejs package
R language plot visualization: plot to visualize the residual analysis diagram of the regression model, the scatter diagram of the predicted value and residual corresponding to the training set and th
Vscode installation and configuration
UXDB在不知道明文密码的情况下重置密码
Basic functions of tea
Grafana connection tdengine reports an error 535