Codeforces Global Round 7 题解(A-D2)

C o d e f o r c e s   G l o b a l   R o u n d   7 ( A D 2 ) \mathrm{Codeforces\ Global \ Round \ 7 (A-D2)} 题解

  • 啊啊!小号上分,大号被小号 s k i p skip ,我醉了。。。

A   B a d   U g l y   N u m b e r s \mathrm{A\ Bad \ Ugly \ Numbers}

  • 1 m i n 1min 就切完了,因为不能被序列的任何数字整除,那么输出 2333.. 2333.. 即可。因为这个数显然不能被 2 2 整除,而 333... 333... 能被 3 3 整除,而加了个二就不行了。然后特判 n = 1 n=1 即可

  • C o d e \mathcal{Code}

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

int T,n;

int main()
{
	T=read();
	for (;T--;)
	{
		n=read();
		if(n<=1) 
		{
			printf("-1\n");
			goto rp;
		}
		putchar('2');
		for ( int j=2;j<=n;j++ ) putchar('3');
		printf("\n");
		rp:;
	}
}

B   M a x i m u m s \mathrm{B\ Maximums}

  • 一道水水的递推, a n = max i = 1 n 1 a i + b n a_n=\max_{i=1}^{n-1} a_i+b_n
  • O ( n ) O(n) 递推即可
  • C o d e \mathcal{Code}
#include <bits/stdc++.h>
#define int long long
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int N=1e6+5;

int b[N];

signed main()
{
	int n,mx=0;
	n=read();
	for ( int i=1;i<=n;i++ ) b[i]=read();
	for ( int i=1;i<=n;i++ ) 
	{
		printf("%lld ",mx+b[i]); 
		mx=max(mx,b[i]+mx);
	}
	return 0;
}

C   P e r m u t a t i o n   P a r t i t i o n s \mathrm{C\ Permutation \ Partitions}

  • 对于第一问,由于是个排列,故只要 O ( 1 ) O(1) 等差数列公式即可。
  • 对于第二问,我们求方案数,因为相邻两个区间的间隔一定在两个选择的数中间,所以只要将位置差(那些第一问里的数的位置差)相乘即可。
  • C o d e \mathcal{Code}
#include <bits/stdc++.h>
#define int long long 
#define pb push_back
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

int n,m,ans,sum;

const int mo=998244353;

signed main()
{
	n=read();
	m=read();
	ans=1ll;
	int lp=0;
	for ( int i=1;i<=n;i++ )
	{
		int x=read();
		if(x>=n-m+1) 
		{
			sum+=x;
			if(lp)
				ans=ans*(i-lp)%mo;
			lp=i;
		}
	}
	printf("%lld %lld\n",sum,ans);
	return 0;
}
			

D 1 , D 2   P r e f i x S u f f i x P a l i n d r o m e ( E a s y + H a r d v e r s i o n ) \mathrm{D1,D2\ Prefix-Suffix Palindrome (Easy+Hard version)}

  • 又是一眼题,但是写了老半天。
  • 有一个很显然的贪心:先从两端向中间缩进,直至不能,这是两端的串相接就可以并成回文串。那么我们只要再截取中间那一段的字符串,在它的前缀或后缀找到一个最长的回文串即可。这可以用 m a n a c h a r \mathrm{manachar} 来做。不会 m a n a c h a r \mathrm{manachar} 右拐这里学习它
  • C o d e \mathcal{Code}
#include <bits/stdc++.h>
#define pb push_back
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int N=3e6+5;

int n,Q,m,ans,hw[N];
char s[N],t[N],c[N],b[N];

inline int get(char *s,int n)
{
	string cwy="##";
	for ( int i=1;i<=n;i++ ) cwy+=s[i],cwy+="#";
	int mx=0,mid=0,ans=1;
	for ( int i=1;i<cwy.length();i++ ) hw[i]=0;
	for ( int i=1;i<cwy.length();i++ ) 
	{
		if(i<mx) hw[i]=min(hw[mid*2-i],mx-i);
		else hw[i]=1;
		for (;cwy[i+hw[i]]==cwy[i-hw[i]]&&i+hw[i]<cwy.length();hw[i]++);
		if(i==hw[i]) ans=max(ans,hw[i]);
		if(i+hw[i]>mx) mx=i+hw[i],mid=i;
	}
	return ans-1;
}

inline void solve()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	int P=1;
	while(s[P]==s[n-P+1]&&P<=n) ++P;
	if(P==n+1) 
	{
		for ( int i=1;i<=n;i++ ) putchar(s[i]);
		printf("\n");
		return;
	}
	m=0;
	for ( int i=P;i<=n-P+1;i++ ) c[++m]=s[i];
	int l=get(c,m);
	reverse(c+1,c+m+1);
	int r=get(c,m);
	reverse(c+1,c+m+1);
	if(l<r)  reverse(c+1,c+m+1),swap(l,r);
	for ( int i=1;i<P;i++ ) putchar(s[i]);
	for ( int i=1;i<=l;i++ ) putchar(c[i]);
	for ( int i=P-1;i;i-- ) putchar(s[i]);
	puts(""); 
}

int main()
{
	Q=read();
	for (;Q--;) solve();
	return 0;
}

E G \mathrm{E-G} 的坑先挖着。。

猜你喜欢

转载自blog.csdn.net/wangyiyang2/article/details/105004983