CF 1462 简单的题解

2024-05-31 15:18
文章标签 简单 题解 cf 1462

本文主要是介绍CF 1462 简单的题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • A. Favorite Sequence
  • B. Last Year’s Substring
  • C. Unique Number
  • D. Add to Neighbour and Remove
  • E1. Close Tuples (easy version)
  • E2. Close Tuples (hard version)

A. Favorite Sequence
Polycarp has a favorite sequence a[1…n] consisting of n integers. He wrote it out on the whiteboard as follows:

he wrote the number a1 to the left side (at the beginning of the whiteboard);
he wrote the number a2 to the right side (at the end of the whiteboard);
then as far to the left as possible (but to the right from a1), he wrote the number a3;
then as far to the right as possible (but to the left from a2), he wrote the number a4;
Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard.
The beginning of the result looks like this (of course, if n≥4).
For example, if n=7 and a=[3,1,4,1,5,9,2], then Polycarp will write a sequence on the whiteboard [3,4,5,2,9,1,1].
在这里插入图片描述

You saw the sequence written on the whiteboard and now you want to restore Polycarp’s favorite sequence.

Input
The first line contains a single positive integer t (1≤t≤300) — the number of test cases in the test. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤300) — the length of the sequence written on the whiteboard.

The next line contains n integers b1,b2,…,bn (1≤bi≤109) — the sequence written on the whiteboard.

Output
Output t answers to the test cases. Each answer — is a sequence a that Polycarp wrote out on the whiteboard.

Example
input
6
7
3 4 5 2 9 1 1
4
9 2 7 1
11
8 4 3 1 2 7 8 7 9 4 2
1
42
2
11 7
8
1 1 1 1 1 1 1 1
output
3 1 4 1 5 9 2
9 1 2 7
8 2 4 4 3 9 1 7 2 8 7
42
11 7
1 1 1 1 1 1 1 1
Note
In the first test case, the sequence a matches the sequence from the statement. The whiteboard states after each step look like this:

[3]⇒[3,1]⇒[3,4,1]⇒[3,4,1,1]⇒[3,4,5,1,1]⇒[3,4,5,9,1,1]⇒[3,4,5,2,9,1,1].

题意
给你一个序列,前后分别读入。

思路
定义l,r,while(l<=r) 等于的时候特判一下就行

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;int n,m; 
int a[N]; void solve(){	scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]);int l = 1, r = n;while(l<=r){if(l==r) printf("%d ",a[l]);else printf("%d %d ",a[l],a[r]);l++,r--;}puts("");
}int main(){int t = 1;scanf("%d",&t);while(t--){solve();}return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

B. Last Year’s Substring
Polycarp has a string s[1…n] of length n consisting of decimal digits. Polycarp performs the following operation with the string s no more than once (i.e. he can perform operation 0 or 1 time):

Polycarp selects two numbers i and j (1≤i≤j≤n) and removes characters from the s string at the positions i,i+1,i+2,…,j (i.e. removes substring s[i…j]). More formally, Polycarp turns the string s into the string s1s2…si−1sj+1sj+2…sn.
For example, the string s=“20192020” Polycarp can turn into strings:

“2020” (in this case (i,j)=(3,6) or (i,j)=(1,4));
“2019220” (in this case (i,j)=(6,6));
“020” (in this case (i,j)=(1,5));
other operations are also possible, only a few of them are listed above.
Polycarp likes the string “2020” very much, so he is wondering if it is possible to turn the string s into a string “2020” in no more than one operation? Note that you can perform zero operations.

Input
The first line contains a positive integer t (1≤t≤1000) — number of test cases in the test. Then t test cases follow.

The first line of each test case contains an integer n (4≤n≤200) — length of the string s. The next line contains a string s of length n consisting of decimal digits. It is allowed that the string s starts with digit 0.

Output
For each test case, output on a separate line:

“YES” if Polycarp can turn the string s into a string “2020” in no more than one operation (i.e. he can perform 0 or 1 operation);
“NO” otherwise.
You may print every letter of “YES” and “NO” in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example
input
6
8
20192020
8
22019020
4
2020
5
20002
6
729040
6
200200
output
YES
YES
YES
NO
NO
NO
Note
In the first test case, Polycarp could choose i=3 and j=6.

In the second test case, Polycarp could choose i=2 and j=5.

In the third test case, Polycarp did not perform any operations with the string.

题意
给你一个字符串,你可以将中间的一部分去掉,只能操作一次,问你能不能搞出"2020"。

思路
前后特判一下就好

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;int n,m; 
int a[N]; 
string s;
void solve(){	scanf("%d",&n);cin>>s;if(s[0]=='2'&&s[1]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){puts("YES");return;}if(s[0]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){puts("YES");return;}if(s[n-4]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){puts("YES");return;}if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[3]=='0'){puts("YES");return;}if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[n-1]=='0'){puts("YES");return;}puts("NO");
}int main(){int t = 1;scanf("%d",&t);while(t--){solve();}return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

C. Unique Number
You are given a positive number x. Find the smallest positive integer number that has the sum of digits equal to x and all digits are distinct (unique).

Input
The first line contains a single positive integer t (1≤t≤50) — the number of test cases in the test. Then t test cases follow.

Each test case consists of a single integer number x (1≤x≤50).

Output
Output t answers to the test cases:

if a positive integer number with the sum of digits equal to x and all digits are different exists, print the smallest such number;
otherwise print -1.
Example
input
4
1
5
15
50
output
1
5
69
-1

题意
给你一个数,求最小的整数满足数字和等于这个数,且没有重复的数,如果没有输出-1

思路
打表,很快就能找出规律,因为只有45个数有值,所以开个数组记录下就行。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;int n,m; 
int a[N]; 
int vis[55]={0,1,2,3,4,5,6,7,8,9,19,29,39,49,59,69,79,89,189,289,389,489,589,689,789,1789,2789,3789,4789,5789,6789,16789,26789,36789,46789,56789,156789,256789,356789,456789,1456789,2456789,3456789,13456789,23456789,123456789,-1,-1,-1,-1,-1};
string s;
void solve(){	scanf("%d",&m);printf("%d\n",vis[m]);
}int main(){int t = 1;scanf("%d",&t);while(t--){solve();}return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

D. Add to Neighbour and Remove
Polycarp was given an array of a[1…n] of n integers. He can perform the following operation with the array a no more than n times:

Polycarp selects the index i and adds the value ai to one of his choice of its neighbors. More formally, Polycarp adds the value of ai to ai−1 or to ai+1 (if such a neighbor does not exist, then it is impossible to add to it).
After adding it, Polycarp removes the i-th element from the a array. During this step the length of a is decreased by 1.
The two items above together denote one single operation.

For example, if Polycarp has an array a=[3,1,6,6,2], then it can perform the following sequence of operations with it:

Polycarp selects i=2 and adds the value ai to (i−1)-th element: a=[4,6,6,2].
Polycarp selects i=1 and adds the value ai to (i+1)-th element: a=[10,6,2].
Polycarp selects i=3 and adds the value ai to (i−1)-th element: a=[10,8].
Polycarp selects i=2 and adds the value ai to (i−1)-th element: a=[18].
Note that Polycarp could stop performing operations at any time.

Polycarp wondered how many minimum operations he would need to perform to make all the elements of a equal (i.e., he wants all ai are equal to each other).

Input
The first line contains a single integer t (1≤t≤3000) — the number of test cases in the test. Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤3000) — the length of the array. The next line contains n integers a1,a2,…,an (1≤ai≤105) — array a.

It is guaranteed that the sum of n over all test cases does not exceed 3000.

Output
For each test case, output a single number — the minimum number of operations that Polycarp needs to perform so that all elements of the a array are the same (equal).

Example
input
4
5
3 1 6 6 2
4
1 2 2 1
3
2 2 2
4
6 3 2 1
output
4
2
0
2
Note
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):

[3,1,6,6,2] −→−−−−−−−i=4, add to left [3,1,12,2] −→−−−−−−−−i=2, add to right [3,13,2] −→−−−−−−−−i=1, add to right [16,2] −→−−−−−−−i=2, add to left [18]. All elements of the array [18] are the same.

In the second test case of the example, the answer can be constructed like this (just one way among other ways):

[1,2,2,1] −→−−−−−−−−i=1, add to right [3,2,1] −→−−−−−−−i=3, add to left [3,3]. All elements of the array [3,3] are the same.

In the third test case of the example, Polycarp doesn’t need to perform any operations since [2,2,2] contains equal (same) elements only.

In the fourth test case of the example, the answer can be constructed like this (just one way among other ways):

[6,3,2,1] −→−−−−−−−−i=3, add to right [6,3,3] −→−−−−−−−i=3, add to left [6,6]. All elements of the array [6,6] are the same.

题意
你可以将序列中的数向两个相邻的数依附,即加上这个数,然后删了这个数,求序列数字都相同的最小步数。

思路
从结果来考虑,我们将序列中最大的数到所有的和所有能整除和的数判断一边就行,模拟看下对不对,对就输出
3000的数据量,n^2随便过!

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 1e5 + 5;int n,m,x; 
int a[N]; 
string s;
void solve(){	scanf("%d",&n);int maxx = 0,sum = 0;for(int i=1;i<=n;i++){scanf("%d",&x);a[i]=x;sum += x;maxx = max(maxx, x);}for(int i=maxx;i<=sum;i++){if(sum%i==0){bool flag = true;for(int j=1;j<=n;j++){if(a[j]<i){int summ = 0;for(int k=j;k<=n;k++,j++){summ+=a[k];if(summ == i) break;if(summ > i){flag = false;break;} }} }if(flag){printf("%d\n",n-sum/i);return;}}}
}int main(){int t = 1;scanf("%d",&t);while(t--){solve();}return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

E1. Close Tuples (easy version)
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on k and m (in this version k=2 and m=3). Also, in this version of the problem, you DON’T NEED to output the answer by modulo.

You are given a sequence a of length n consisting of integers from 1 to n. The sequence may contain duplicates (i.e. some elements can be equal).

Find the number of tuples of m=3 elements such that the maximum number in the tuple differs from the minimum by no more than k=2. Formally, you need to find the number of triples of indices i<j<z such that

max(ai,aj,az)−min(ai,aj,az)≤2.
For example, if n=4 and a=[1,2,4,3], then there are two such triples (i=1,j=2,z=4 and i=2,j=3,z=4). If n=4 and a=[1,1,1,1], then all four possible triples are suitable.

Input
The first line contains a single integer t (1≤t≤2⋅105) — the number of test cases. Then t test cases follow.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the sequence a.

The next line contains n integers a1,a2,…,an (1≤ai≤n) — the sequence a.

It is guaranteed that the sum of n for all test cases does not exceed 2⋅105.

Output
Output t answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than 2. Note that in difference to the hard version of the problem, you don’t need to output the answer by modulo. You must output the exact value of the answer.

Example
input
4
4
1 2 4 3
4
1 1 1 1
1
1
10
5 6 1 3 2 9 8 1 2 4
output
2
4
0
15

思路
基础组合数…

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 2e5 + 5;
int n,m,x; 
int a[N]; 
int vis[N],v[N];
string s;
void solve(){	scanf("%d",&n);for(int i=0;i<=n+5;i++) vis[i]=v[i]=0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i];vis[a[i]]++;}ll res = 0;for(int i=1;i<=n;i++){if(!v[a[i]]){ll t1 = vis[a[i]];ll t2 = vis[a[i]+1];ll t3 = vis[a[i]+2];res += t1 * t2 * t3; res += t1*(t1-1)*t2/2;res += t2*(t2-1)*t1/2;res += t1*(t1-1)*t3/2;res += t3*(t3-1)*t1/2;res += t1*(t1-1)*(t1-2)/6;}v[a[i]]=1;}printf("%lld\n",res);
}int main(){int t = 1;scanf("%d",&t);while(t--){solve();}return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

E2. Close Tuples (hard version)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 +7;
const double esp = 1e-6;
const double PI = acos(-1);
const int N = 2e5 + 10;
int n,m,k; 
int a[N];
ll f[N]; 
ll qpow(ll a,ll b){ll ans = 1,base = a;while(b){if(b&1) ans = ans * base % mod;base = base * base % mod;b>>=1; }return ans;
}
void init(){f[0]=1;for(int i=1;i<=2e5;i++){f[i]=f[i-1]*i%mod;} 
}
ll cal(ll n,ll m){if(n<m) return 0; return 1ll*f[n]*qpow(f[m],mod-2)%mod*qpow(f[n-m],mod-2)%mod;
}
void solve(){	scanf("%d %d %d",&n,&m,&k);for(int i=1;i<=n;i++) scanf("%d",&a[i]);sort(a,a+n+1);ll res = 0;int l =1;for(int i=1;i<=n;i++){while(a[i]-a[l]>k) l++;//cout<<l<<" "<<i<<" "<<m-1<<endl; res += cal(i-l,m-1);res%=mod; }printf("%lld\n",res);
}int main(){int t = 1;init();scanf("%d",&t);while(t--){solve();}return 0;
}
/*
5 0 3 0 4
5 5 3 5 4
*/ 

这篇关于CF 1462 简单的题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018291

相关文章

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

使用IntelliJ IDEA创建简单的Java Web项目完整步骤

《使用IntelliJIDEA创建简单的JavaWeb项目完整步骤》:本文主要介绍如何使用IntelliJIDEA创建一个简单的JavaWeb项目,实现登录、注册和查看用户列表功能,使用Se... 目录前置准备项目功能实现步骤1. 创建项目2. 配置 Tomcat3. 项目文件结构4. 创建数据库和表5.

使用PyQt5编写一个简单的取色器

《使用PyQt5编写一个简单的取色器》:本文主要介绍PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16进制颜色编码,一款跟随鼠标刷新图像的RGB和16... 目录取色器1取色器2PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16

四种简单方法 轻松进入电脑主板 BIOS 或 UEFI 固件设置

《四种简单方法轻松进入电脑主板BIOS或UEFI固件设置》设置BIOS/UEFI是计算机维护和管理中的一项重要任务,它允许用户配置计算机的启动选项、硬件设置和其他关键参数,该怎么进入呢?下面... 随着计算机技术的发展,大多数主流 PC 和笔记本已经从传统 BIOS 转向了 UEFI 固件。很多时候,我们也

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个