Codeforces Round #843 (Div. 2)

题目链接

A

tag:构造,分类讨论
核心思路
我们其实可以发现我们要是分很多情况去讨论a b c,这题就不好做。所以我们可以假设a和b就是我们字符串的两个端点,这样题目就很好做了

  • (a='a'&&b'b')||(a'b'&&b=='a')
  • a'a'&&b'a'
  • a'b'&&b'a'
    这个题目也就解决了,所以当遇到情况很复杂的时候,我们可以抽离出最小的一种情况分类讨论,特别是这种构造类型的题目。
// Problem: A1. Gardener and the Capybaras (easy version)
// Contest: Codeforces - Codeforces Round #843 (Div. 2)
// URL: https://codeforces.com/contest/1775/problem/A1
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
void solve()
{
	string s;
	cin>>s;
	int last=s.size()-1;
	if((s[0]=='a'&&s[last]=='b')||(s[0]=='b'&&s[last]=='a'))
	{
		int idx=-1;
		for(int i=1;i<last;i++)
		{
			if(s[i]=='a')
			{
				idx=i;
				break;
			}
		}
		if(idx==-1)
		{
			for(int i=1;i<last;i++)
			{
				if(s[i]=='b')
				{
					idx=i;
					break;
				}
			}
		for(int i=0;i<idx;i++)
		cout<<s[i];
		cout<<" ";
		for(int i=idx;i<last;i++)
		cout<<s[i];
		cout<<" "<<s[last]<<endl;
		}
		else
		{
			for(int i=0;i<idx;i++)
			cout<<s[i];
			cout<<" "<<s[idx]<<" ";
			for(int i=idx+1;i<=last;i++)
			cout<<s[i];
			cout<<endl;	
		}
	}
	else if(s[0]=='a'&&s[last]=='a')
	{
		int idx=-1;
		for(int i=1;i<last;i++)
		{
			if(s[i]=='b')
			idx=i;
		}
		if(idx!=-1)
		{
			for(int i=0;i<idx;i++)
			cout<<s[i];
			cout<<" "<<s[idx]<<" ";
			for(int i=idx+1;i<=last;i++)
			cout<<s[i];
			cout<<endl;
		}
		else
		{
			cout<<s[0]<<" ";
		for(int i=1;i<last;i++)
		cout<<s[i];
		cout<<" "<<s[last]<<endl;
		}
	}
	else if(s[0]=='b'&&s[last]=='b')
	{
		int idx=-1;
		for(int i=1;i<last;i++)
		{
			if(s[i]=='a')
			idx=i;
		}
		if(idx!=-1)
		{
			for(int i=0;i<idx;i++)
			cout<<s[i];
			cout<<" "<<s[idx]<<" ";
			for(int i=idx+1;i<=last;i++)
			cout<<s[i];
			cout<<endl;
		}
		else
		{
			cout<<s[0]<<" ";
		for(int i=1;i<last;i++)
		cout<<s[i];
		cout<<" "<<s[last]<<endl;
		}
	}
}
int main()
{
int T;
cin>>T;
while(T--)
{
	solve();
}
}

B

tag:贪心
核心思路
首先可以发现一个很明显的性质,那就是只要一个序列是另外一个序列的子序列,那么这个就肯定是可以构造出来的。所以我们干脆直接构造出来最大的一个序列,这就需要我们使用mp存储每个数出现的次数了。
然后我们把所有的子序列拼装在一起,再for一边每一个最小的字串。如果出现了mp中某个数的次数为1,就说明肯定是不可以的。

// Problem: B. Gardener and the Array
// Contest: Codeforces - Codeforces Round #843 (Div. 2)
// URL: https://codeforces.com/contest/1775/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
int n,m;
const int N=1e6;
map<int,int> p;
int vis[N];
void solve()
{
	p.clear();
	cin>>m;
			vector<vector<int>> a(m);
	for(int i=0;i<m;i++)
	{
		cin>>n;
		for(int j=0;j<n;j++)
		{
			int x;
			cin>>x;
			a[i].push_back(x);
			p[x]++;
		}
		
	}
	for(int i=0;i<m;i++)
	{
			int flag=1;
		for(auto c:a[i])
		{
			if(p[c]==1)
			flag=0;
		}
		if(flag)
		{
			YES;
		}
	}
	NO;
	
}
int main()
{
int T;
cin>>T;
while(T--)
{
	solve();
}
}

C

tag:位运算
核心思路
首先总结下一个惨痛的教训,那就是一定要多看别人的题解,要不然就会像我一样在这上面浪费两三个小时。
这题数据很大,而且有&。所以我们首先想到位运算。所以我们接下来分析下每一位。假设n中的第k位为nk,x中的第k为xk.

  • nk=0,xk=0.我们可以知道这种情况对我们的m没有影响。
  • nk=0,xk=1.这个情况是不可能实现的。
  • nk=1.xk=0.所以m必须要等到这个数位出现0之后才可以出现,也就是[temp,正无穷].
  • nk=0,xk=1.所以m必须要等到某个数为0之前出现,也就是[n,temp).
    他妈的,终于懂了。
    temp的求法可以学习下。
// Problem: C. Interesting Sequence
// Contest: Codeforces - Codeforces Round #843 (Div. 2)
// URL: https://codeforces.com/contest/1775/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
void solve()
{
	LL n,x;
	cin>>n>>x;
	LL l=n,r=5e18;
	for(int i=0;i<4;i++)
	{
		int a=n>>i&1,b=x>>i&1;
		if(a==0&&b==1)
		{
			cout<<-1<<endl;
			return;
		}
		LL temp=(n/(1LL<<i)+1)*(1LL<<i);
		if(a==1&&b==0)
		{
			l=max(l,temp);
		}
		else if(a==1&&b==1)
		{
			r=min(r,temp-1);
		}
		
	}
	cout<<(l<=r?l:-1)<<endl;
}
int main()
{
int T;
cin>>T;
while(T--)
{
	solve();
}
}

E

tag:思维
核心思路:我们首先可以挖掘这个操作的性质,也就是他其实是对我们任选的一个区间,对于区间里面的不连续的数进行操作。因为我们最后就是然我们数组的前缀和变为0,所以我们也就是讨论前缀和,其实我们结合这个性质,可以得出来

  • 如果是正数,也就是求我们正数前缀和最大的变为了0的操作数
  • 如果是负数,也就是求我们负数的前缀和变为0的操作数。
// Problem: E. The Human Equation
// Contest: Codeforces - Codeforces Round #843 (Div. 2)
// URL: https://codeforces.com/contest/1775/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define endl "\n"
const int N=1e6+10;
LL a[N],sum[N];
void solve()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	LL l=0,r=0;
	for(int i=1;i<=n;i++)
	{
		l=max(l,sum[i]);
		r=min(r,sum[i]);
	}
	cout<<l-r<<endl;
}

int main()
{
int T;
cin>>T;
while(T--)
{
	solve();
}
}
posted @ 2023-01-11 19:49  努力的德华  阅读(47)  评论(0)    收藏  举报