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

相关文章

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.