Codeforces Round 856 (Div. 2)

本文作者回顾了一场编程竞赛,讨论了在解决字符串回文判断和数组处理问题上的策略。对于字符串问题,作者发现关键在于找到特定长度的子串并检查是否为回文。在数组问题中,涉及将1转换为2以及处理数组元素的相对关系。最后,作者提到了在处理单调数组时使用二分查找的优化方法。
摘要由CSDN通过智能技术生成

今天上午打了,自我感觉自己的状态不是很好,但奇了怪了...最后排名还可以..2w4人排5000名左右,还算可以吧,不是很差。

但AB两题出的确实太慢了,B题还wa了一发,不然可以更快..

A:Problem - A - Codeforces

这题我感觉比赛时人人都是很蒙的状态,所有人都出的都极的慢,我也是想了好久才想到怎么做的,对于字符串s的每一个长度为1到n-1的字串有且仅有两个,知道这一点就很简单了。比赛中我用的是(n+1)/2的字串,然后把他们拼接,然后判读是否回文

md,找bug找了半天

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
	if(b==0){
		x=1;
		y=0;
		return a;
	}

	ll d=ex_gcd(b,a%b,y,x);
	y=y-a/b*x;
	return d;
}
string s[50];
int t;
int n; 
int main(){
	scanf("%d",&t);
	while(t--){
		
		scanf("%d",&n);
		string s1,s2;
		int	flag=0;
		for(int i=1;i<=2*n-2;i++){
			cin>>s[i];
			if(s[i].length()==(n+1)/2){
				if(flag==0){
					s1=s[i];
					flag=1;
				}else{
					s2=s[i];
				}
			}
		}
		string a=s1+s2;
		string  b=s2+s1;
		a=" "+a;
		b=" "+b;
		
		flag=0;
		for(int i=1;i<=(n+1)/2;i++){
			if(a[i]==a[(n+1)/2*2-i+1]){
				continue;
			}else{
				flag=1;
				break;
			}
		}
		
		if(flag==0){
			printf("YES\n");
			continue;
		}
		flag=0;
		for(int i=1;i<=(n+1)/2;i++){
			if(b[i]==b[(n+1)/2*2-i+1]){
				continue;
			}else{
				flag=1;
				break;
			}
		}
		
		if(flag==0){
			printf("YES\n");
			continue;
		}
		printf("NO\n");		
	}


	return 0;
}


赛后:对于长度为n-1的子串,我们如果两个字串的其中一个反转与另一个相等,那么原字符串就是回文的,否则就不是回文的。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
	if(b==0){
		x=1;
		y=0;
		return a;
	}

	ll d=ex_gcd(b,a%b,y,x);
	y=y-a/b*x;
	return d;
}
string s;
int t;
int n; 
int main(){
	scanf("%d",&t);
	while(t--){
		vector<string>a;
		scanf("%d",&n);
		for(int i=1;i<=2*n-2;i++){
			cin>>s;
			if((int)s.length()==n-1){
				a.push_back(s);
			}
		}
		reverse(a[1].begin(),a[1].end());
		if(a[1]==a[0]){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	}

	return 0;
}


B:Problem - B - Codeforces

这题一开始读对了,md然后自己又读错题了,

其实对于数组如果有1我们把它变成2即可,如果a[i]%a[i-1]==0,我们把a[i]++即可

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
	if(b==0){
		x=1;
		y=0;
		return a;
	}

	ll d=ex_gcd(b,a%b,y,x);
	y=y-a/b*x;
	return d;
}
int t;
int n;
ll a[10005];
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
	
		for(int i=1;i<=n;i++){
			if(a[i]==1){
				a[i]=2;
			}
		}
		for(int i=2;i<=n;i++){
			if(a[i]%a[i-1]==0){
				a[i]=a[i]+1;
			}else{
				continue;
			}
		}
		
		for(int i=1;i<=n;i++){
			printf("%lld ",a[i]);
			
		}
		printf("\n");
	}


	return 0;
}


C:我感觉这题的题意我读了快半个小时,md终于读懂了,当时大脑晕的很...思路不是很清晰,但想了一会,因为数组是不下降的,所以我们应该从后往前操作,如果ans*a[j]/(i+1-j)>=ans,我们就继续,否则j+1就是答案了。

其实做法很对,就tm比赛脑子晕的很,找错误找了好久,虽然样例过了,然最后wa在了test3,比赛时候我看着是2.5s,我就写了一个O(n^n)的复杂度的....

正确思路:二分

对于数组 :a1<=a2<=a3....<=an

所以a1/n<a2/n-1<an-1/2<an/1

如果对于某一项 am/(n-m+1)<1的话就不行了

因为a数组是单调的,所以我们可以二分,我们二分的是从后往前的第项数mid;

即     a[i+1-mid]>=mid

那么从前往后数就是第i+1-mid项了。我们使得i+1-mid尽可能的小,所以我们等于号在left=mid+1;

我们最后输出mid,即left-1即可

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef  long  long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
	if(b==0){
		x=1;
		y=0;
		return a;
	}

	ll d=ex_gcd(b,a%b,y,x);
	y=y-a/b*x;
	return d;
}
int t,n;
int a[200005];
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		} 
		for(int i=1;i<=n;i++){
			int left=1;
			int right=i;
			while(left<=right){
				int mid=(left+right)/2;
				if(a[i+1-mid]>=mid){
					left=mid+1;//	 
				}else{
					right=mid-1;
				}
			}	
			printf("%d ",left-1);			
		}
		printf("\n");
		
	}



	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值