Codeforces Round #496 (Div. 3)(A,B,C,D,E1,E2)题解

本文提供了CodeForces比赛1005A至1005E题目的详细解析及AC代码实现。涵盖楼梯攀登计数、字符串编辑距离、数字序列处理等算法问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/contest/1005

A题题意:Tanya每次爬不定数目的楼梯,每走一步喊一次,求出每次爬的数目。

A题题解:没碰见1就把上一次的数存进去最后输出即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 1e5+7;
typedef long long ll;
int n,m,a[maxn];
vector<int> S;
int main(int argc, char const *argv[])
{
	cin>>n;
	int now=1;
	_for(i,1,n)
	{
		cin>>m;
		if(m==1)
		{
			S.push_back(now);
			now=1;
		}
		else now = m;
	}
	S.push_back(now);
	cout<<S.size()-1<<endl;
	int l = S.size()-1;
	_for(i,1,l)cout<<S[i]<<" ";
	return 0;
}

B题题意:给两个字符串,每次可以选择删除其中一个的第一个字符,求最少删除次数使得两字符串相同。

B题题意:前面只要不相等就得删除 所以直接从后面一起遍历,第一个不相同的前面的所有字符都需要删除。

AC代码:

/*
* @Author: 王文宇
* @Date:   2018-07-09 23:44:46
* @Last Modified by:   王文宇
* @Last Modified time: 2018-07-09 23:49:13
*/
#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 2e5+7;
typedef long long ll;
int n,m,a[maxn];
char s1[maxn],s2[maxn];
int main(int argc, char const *argv[])
{
	cin>>s1>>s2;
	int l1 = strlen(s1);
	int l2 = strlen(s2);
	int ans = 0;
	int ok = 0;
	if(l1<=l2)
	{
		for(int i=1;i<=l1;i++)
		{
			if(s1[l1-i]!=s2[l2-i])
			{
				ans+=l1-i+1;
				ans+=l2-i+1;
				ok = 1;
				break;
			}
		}
		if(!ok)ans+=l2-l1;
	}
	else
	{
		for(int i=1;i<=l2;i++)
		{
			if(s1[l1-i]!=s2[l2-i])
			{
				ans+=l1-i+1;
				ans+=l2-i+1;
				ok = 1;
				break;
			}
		}
		if(!ok)ans+=l1-l2;		
	}
	cout<<ans<<endl;
	return 0;
}

C题题意:给定N个数,删除其中的数字使得每一个数都能在序列里找到一个数求和后是2的倍数。

C题题解:n<=120000,每个数最大是1e9,那么对于每个数预处理出每个2的倍数与他的差标记,然后遍历每个数找不到标记的就说明该数应该删除,复杂度是o(n*32),需要注意爆ll。

AC代码:

/*
* @Author: 王文宇
* @Date:   2018-07-09 23:51:28
* @Last Modified by:   王文宇
* @Last Modified time: 2018-07-10 00:12:06
*/
#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 120007;
typedef long long ll;
int n,a[maxn];
vector<ll> E[maxn];
map<ll,int> Q;
int main(int argc, char const *argv[])
{
	cin>>n;
	_for(i,1,n)
	{
		int x;
		cin>>x;
		a[i]=x;
		Q[x]++;
		ll k = 1;
		_for(j,1,31)
		{
			ll now = k-x;
			if(k>x)E[i].push_back(now);
			k*=2;
			//cout<<k<<endl;
		}
	}
	int ans = 0;
	_for(i,1,n)
	{
		//cout<<1<<endl;
		int ok = 0;
		int l = E[i].size()-1;
		_for(j,0,l)
		{
			ll k = E[i][j];
			if(Q[k]>0)
			{
				if(a[i]==k)
				{
					if(Q[k]>1)
					{
						ok=1;
						break;
					}
				}
				else
				{
					ok = 1;
					break;
				}
			}
		}
		//cout<<i<<endl;
		if(ok==0)ans++;
	}
	cout<<ans<<endl;
	return 0;
}

D题题意:给定一个数字序列,可以将他们任意分段,但是分后有前缀0的将删去前缀0,求最多能分成多少个整除3的数。

D题题解:因为不能有前缀0,所以从后往前分,如果当前这个数能整除3,那么肯定他自己一段,然后不能整除3的判断就可以了,因为3个连续的数肯定能分成一段能整除3的数。

AC代码:

/*
* @Author: 王文宇
* @Date:   2018-07-10 00:16:02
* @Last Modified by:   王文宇
* @Last Modified time: 2018-07-10 00:32:16
*/
#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 200006;
typedef long long ll;
int n,a[maxn];
char s[maxn];
int main(int argc, char const *argv[])
{
	cin>>s;
	int l = strlen(s)-1;
	int ans = 0;
	int x=0;
	int y=0;
	for(int i=l;i>=0;i--)
	{
		int now= s[i]-'0';
		if(now%3==0)
		{
			x=0;
			y=0;
			ans++;
			now = 0;
			continue;
		}
		else if(now%3==1)
		{
			if(y>=1)
			{
				ans++;
				y=0;
				continue;
			}
			else if(x<2)
			{
				x++;
			}
			else
			{
				x=0;
				ans++;
				continue;
			}
		}
		else
		{
			if(y==2)
			{
				y=0;
				ans++;
				continue;
			}
			else if(x>=1)
			{
				x=0;
				ans++;
				continue;
			}
			else y++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

E题题意:给一个数组,问有多少个区间满足中位数是m的(该题中位数m偶数的取左边 即2 4的中位数是2)E1和E2的区别就是E1是1-N的数,E2是任意的,包括相同的数。

E题题解:将数组中<=m的数设为1,>m的设为-1,那么前缀和>=0的就是中位数<=m的情况,那么再设一次<=m-1的尾1,>m-1的为-1,这时候前缀和>=0的就是中位数<m的情况,两者差就是中位数等于m的情况。求各个的结果用树状数组去维护即可。

AC代码:

/*
* @Author: 王文宇
* @Date:   2018-07-10 02:14:27
* @Last Modified by:   王文宇
* @Last Modified time: 2018-07-10 04:24:16
*/
#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 200006;
typedef long long ll;
int n,m,a[maxn],sum[maxn];
int lowbit(int x)
{
	return x=x&(-x);
}
struct node
{
	int m,c[maxn*3];
	int init(int now)
	{
		m=now;
		_for(i,1,m)c[i]=0;
	}
	void insert(int x)
	{
		while(x<=m)
		{
			c[x]++;
			x+=lowbit(x);
		}
	}
	ll getnum(int x)
	{
		ll ans = 0;
		while(x>0)
		{
			ans+=c[x];
			x-=lowbit(x);
		}
		return ans;
	}
}now;
ll getans(int s)
{
	_for(i,1,n)
	{
		if(a[i]<=s)sum[i]=1;
		else sum[i]=-1;
		sum[i]=sum[i-1]+sum[i];
	}
	now.init(2*n+1);
	now.insert(n+1);
	ll ans = 0;
	_for(i,1,n)
	{
		ans+=now.getnum(sum[i]+n+1);
		now.insert(sum[i]+n+1);
	}
	return ans;
}
int main(int argc, char const *argv[])
{
	cin>>n>>m;
	_for(i,1,n)cin>>a[i];
	cout<<getans(m)-getans(m-1)<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值