A. Split it!
Example
input
7
5 1
qwqwq
2 1
ab
3 1
ioi
4 2
icpc
22 0
dokidokiliteratureclub
19 8
imteamshanghaialice
6 3
aaaaaa
output
YES
NO
YES
NO
YES
NO
NO
题解:
注意:
1、k=0时,s即为a[1]
2、不需要讨论n的奇偶性,只需要判断n为偶数时是否有2*k==n
3、镜像串的判断不需要循环到n/2,只用循环到k,因为中间的k~n-k可作为a[k+1]
code:
#include <string>
#include <string.h>
#include <math.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--){
int n, k;
cin>>n>>k;
string s;
cin>>s;
bool ok=1;
if( 2*k==n ) { cout<<"NO"<<endl; continue; }
for(int i=0; i<k; i++) {
if( s[i]!=s[n-1-i] ) {
ok=0; break;
}
}
if( ok || k==0 ) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
B. Max and Mex
Example
input
5
4 1
0 1 3 4
3 1
0 1 4
3 0
0 1 4
3 2
0 1 2
3 2
1 2 3
output
4
4
3
5
3
题解:
multist和set一样,是按照一定次序存储元素的容器,元素的值均不能在容器中进行修改,但可以插入和删除。
区别是multiset中元素可以重复,而set是不重复的。因此本题不必用multiset而用set。
注意:t最大可取到100,k最大可取到1e9,若用循环写对每个k操作,必会超时。
求出初始的mex后。将它与max比较,若mex>max,则每一次k操作都会得到一个更大的值,最终的不同的元素个数为s.size()+k。若mex<max,则每一次k操作都会则到一个位于[mex,max]之间的值,只需要判断这个值是否已存在。
注意:s.end()位置不是元素值,可用s.rbegin()来表示max。
code:
#include <string.h>
#include <string>
#include<algorithm>
#include <set>
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--){
set <int> s;
int n, k, me=0;
cin>>n>>k;
for(int i=0; i<n; i++){
int x;
cin>>x;
s.insert(x);
}
for( set<int>::iterator it = s.begin(); it != s.end(); it++ ){
if( *it != me ) break;
else me++;
}
if ( k==0 ) cout<<s.size()<<endl;
else if( me>*s.rbegin() ) cout<<s.size()+k<<endl;
else {
s.insert( (me+*s.rbegin()+1)/2 );
cout<<s.size()<<endl;
}
}
return 0;
}
C. Diamond Miner![在这里插入图片描述](https://img-blog.csdnimg.cn/20210318130407422.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MDkzMTQwMQ==,size_16,color_FFFFFF,t_70)
Example
input
3
2
0 1
1 0
0 -1
-2 0
4
1 0
3 0
-5 0
6 0
0 3
0 1
0 2
0 4
5
3 0
0 4
0 -3
4 0
2 0
1 0
-3 0
0 -10
0 -2
0 -10
output
3.650281539872885
18.061819283610362
32.052255376143336
code:
#include <string>
#include <string.h>
#include <math.h>
#include<algorithm>
#include <iostream>
#include<iomanip>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
ll a[n+10], b[n+10];
int tot1=0, tot2=0;
for(int i=0; i<2*n; i++){
ll x, y;
cin>>x>>y;
if( x==0 ) b[tot2++]=abs(y);
else a[tot1++]=abs(x);
}
sort(a, a+tot1);
sort(b, b+tot2);
double ans=0;
for(int i=0; i<n; i++){
ans+=sqrt( a[i]*a[i]+b[i]*b[i] );
}
cout<<fixed<<setprecision(15)<<ans<<endl;
}
return 0;
}
D. Let’s Go Hiking
Examples
input
5
1 2 5 4 3
output
1
input
7
1 2 4 6 5 3 7
output
0
题解:
形象一点理解:A只能下山,B只能上山。
用两个数组l和r表示位置i左右可走的最大步数。用前缀和计算。
A只能在最高峰(左或右两边的可走的步数最大)的峰顶。倘若在半山腰,B可以截胡。
倘若最高峰不唯一,B从不同的山走必胜。
倘若A的左右可走步数不相等,B一定可以找到一个位置使步数最大且为偶数,偶数则A必输。所以只有左右可走步数相等且为奇数,A才会赢。
综上,答案只有0或1。坑比较多。
code:
#include <string.h>
#include <math.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin>>n;
int p[n+10], l[n+10], r[n+10];
for(int i=0; i<n; i++) cin>>p[i];
memset(l, 0, sizeof(l));
memset(r, 0, sizeof(r));
//前缀和
for(int i=1; i<n; i++) {
if( p[i]>p[i-1] ) l[i]=l[i-1]+1;
}
for(int i=n-2; i>=0; i--) {
if( p[i]>p[i+1] ) r[i]=r[i+1]+1;
}
//返回数组最大值对应下标
int tl=max_element(l, l+n)-l;
int tr=max_element(r, r+n)-r;
bool ok=0;
if( tl==tr && l[tl]%2==0 && r[tr]%2==0 && l[tl]==r[tr] ) ok=1;
for(int i=0; ok && i<n; i++) {
if( l[i]==l[tl] && i!=tl ) ok=0;
if( r[i]==r[tr] && i!=tr ) ok=0;
}
if(ok) cout<<1<<endl;
else cout<<0<<endl;
return 0;
}