Educational Codeforces Round 105 (Rated for Div. 2) 补题

题目链接

A题

  • 题意

在这里插入图片描述

一个字符串只包含A,B,C;你可以把同样的字母换成’(‘或者’)’.。问存不存在一种状态,使得字符串称为括号匹配的字符串。

  • 思路
    官方思路:先去判断首尾,如果相同,则输出”NO“;然后继续判断。如果第一个字母的数量在整个串中是一般的话,那么另外两个字母必须都是反括号;否则另外一个字母得是正括号。因为只有三个数1、2、3,所以知道其中两个可以利用 3^ x^y获得。然后判断是否是合法的匹配括号时,利用前缀和(正括号记为1,反括号记为-1),看是否过程中有小于0的情况出现,或者最后不等于0.
  • 代码
#include <bits/stdc++.h>

using namespace std;

bool solve() {
  string s;
  cin >> s;
  vector<int> d(3);
  int x = s[0] - 'A';
  int y = s.back() - 'A';
  if (x == y)
    return false;
  d[x] = 1; d[y] = -1;
  if (count(s.begin(), s.end(), 'A' + x) == s.length() / 2)
    d[3 ^ x ^ y] = -1;
  else
    d[3 ^ x ^ y] = 1;
  int bal = 0;
  for (char c : s) {
    bal += d[c - 'A'];
    if (bal < 0) return false;
  }
  return bal == 0;
}

int main() {
  int t; cin >> t;
  while (t--) {
    cout << (solve() ? "YES\n" : "NO\n");
  }
}

B题

  • 题意
    在这里插入图片描述
    给你 n ∗ n n * n nn 的矩阵,原来都是白色的格子,然后给你URLD分别代表最边上一列/行,看看能否实现。
    在这里插入图片描述
  • 思路
    本蒟蒻的思路是枚举不符合情况的情况,wa了5、6次才过。主要就是考虑0,1,n-1,n这四种情况。
  • 蒟蒻代码
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=2e5+10,mod=1e9+7;
int a[N],cnt[N];
#define PII pair<int,int>
#define x first
#define y second
#define PB push_backec


void solve()
{
	int n;int p[5];
    cin>>n;
    for(int i=1;i<=4;i++) cin>>p[i];
    p[0]=p[4];
    for(int i=1;i<=4;i++){
        if(p[i]==n){
            if(p[(i+1)%4]==0||p[(i-1+4)%4]==0){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    for(int i=1;i<=4;i++){
        if(p[i]==n-1){
            if(p[(i+1)%4]==0&&p[(i-1+4)%4]==0){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    for(int i=1;i<=4;i++){
        if(p[i]==1){
            if(p[(i+1)%4]==n&&p[(i-1+4)%4]==n){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    for(int i=1;i<=4;i++){
        if(p[i]==0){
            if(p[(i+1)%4]>=n-1&&p[(i-1+4)%4]>=n-1&&p[(i+2)%4]==1){
                cout<<"NO"<<endl;
                return;
            }
            if(p[(i+1)%4]>=n-1&&p[(i-1+4)%4]>=n-1&&p[(i+2)%4]==0){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    for(int i=1;i<=4;i++){
        if(p[i]==1){
            if(p[(i+1)%4]==n-1&&p[(i-1+4)%4]==n&&p[(i+2)%4]==1){
                cout<<"NO"<<endl;
                return;
            }
            if(p[(i+1)%4]==n&&p[(i-1+4)%4]==n-1&&p[(i+2)%4]==1){
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    cout<<"YES"<<endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
}
  • 官方思路
    枚举四个角的情况,放与不放,然后放的话,就是两边各贡献一个1.
  • 官方python代码
for _ in range(int(input())):
    n, U, R, D, L = map(int, input().split())
    for mask in range(16):
        rU, rR, rD, rL = U, R, D, L
        if mask & 1:
            rU -= 1
            rL -= 1
        if mask & 2:
            rL -= 1
            rD -= 1
        if mask & 4:
            rD -= 1
            rR -= 1
        if mask & 8:
            rR -= 1
            rU -= 1
        if min(rU, rR, rD, rL) >= 0 and max(rU, rR, rD, rL) <= n - 2:
            print("YES")
            break
    else:
        print("NO")

C题

  • 题意
    在这里插入图片描述
    给你n个箱子,然后m个特殊位置,每次推动箱子,注意不能跨过箱子,所以就和推箱子游戏是一样的,问你最多能够使得多少个箱子在特殊位置。

注意一开始处于位置0,所以推箱子是累积的。

  • 思路

  • 代码

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)

using namespace std;

int calc(const vector<int> &a, const vector<int> &b){
    int n = a.size();
    int m = b.size();
    vector<int> c(n), res;
    vector<int> su(n + 1);
    int r = m - 1;
    for (int i = n - 1; i >= 0; --i){
        su[i] = su[i + 1];
        while (r >= 0 && b[r] > a[i]) --r;
        if (r >= 0 && b[r] == a[i]) ++su[i];
    }
    int ans = 0;
    int j = 0;
    r = 0;
    forn(l, m){
        while (j < n && a[j] <= b[l] + j) ++j;
        while (r < m && b[r] - b[l] < j) ++r;
        ans = max(ans, r - l + su[j]);
    }
    return ans;
}

int main() {
    int t;
    scanf("%d", &t);
    forn(_, t){
        int n, m;
        scanf("%d%d", &n, &m);
        vector<int> a(n), b(m);
        forn(i, n) scanf("%d", &a[i]);
        forn(i, m) scanf("%d", &b[i]);
        vector<int> al, bl, ar, br;
        forn(i, n){
            if (a[i] < 0) al.push_back(-a[i]);
            else ar.push_back(a[i]);
        }
        forn(i, m){
            if (b[i] < 0) bl.push_back(-b[i]);
            else br.push_back(b[i]);
        }
        reverse(al.begin(), al.end());
        reverse(bl.begin(), bl.end());
        printf("%d\n", calc(al, bl) + calc(ar, br));
    }
    return 0;
}
  • 一位大佬做法
    正数部分和负数部分分开来考虑。
    对于每一个特殊位置(该位置上没有箱子),我们都尝试将箱子正好推到这个位置,这样这个特殊点前面的所有箱子会从这个点向前延伸出去,然后看这部分延伸会碰到多少特殊位置即可,最后加上特殊点上本来就有箱子的个数(可用二分来找,具体看代码)。维护一个最大值。
    负数部分和正数部分相加即是答案。
  • 代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define N 200005
int n,m;
int input[N],a1[N],b1[N],a2[N],b2[N],cnt11,cnt12,cnt21,cnt22;
map<int,int> mp;

int solve(int a[],int b[],int lena,int lenb)
{
	int res = 0;
	mp.clear();
	int mh = 0;
	for(int i=1;i<=lena;i++) mp[a[i]] = 1;
	for(int i=1;i<=lenb;i++) if(mp[b[i]] == 1) mh++;
	
	res = mh;
	for(int i=1;i<=lenb;i++)
	{
		if(mp[b[i]] == 1)
		{
			mh--;
			continue;
		}
		int sum = upper_bound(a + 1, a + 1 + lena, b[i]) - a - 1;
		int pos = lower_bound(b + 1, b + 1 + lenb, b[i] - sum + 1) - b;
		res = max(res, mh + i - pos + 1);
	}
	return res;
}

int main()
{
	int Case = 1;
	scanf("%d",&Case);
	while(Case--)
	{
		cnt11 = cnt12 = cnt21 = cnt22 = 0;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&input[i]);
			if(input[i] > 0) a1[++cnt11] = input[i];
		}
		for(int i=n;i>=1;i--)
		{
			if(input[i] < 0) a2[++cnt12] = -1 * input[i];
		}
		for(int i=1;i<=m;i++)
		{
			scanf("%d",&input[i]);
			if(input[i] > 0) b1[++cnt21] = input[i];
		}
		for(int i=m;i>=1;i--)
		{
			if(input[i] < 0) b2[++cnt22] = -1 * input[i];
		}
		int ans = 0;
		ans += solve(a1, b1, cnt11, cnt21);
		ans += solve(a2, b2, cnt12, cnt22);
		printf("%d\n",ans);
	}
	return 0;
}

  • 自己的理解
    因为一开始的话,处于0位置,所以可以分开负数和正数,为什么?(因为正数不可能推到负数位置上去,负数不可能推到正数位置上去。)因为不能跨越箱子推,所以箱子是累积的,这样的话,通过枚举推到每一个特殊位置就可以二分查找出,有多少个连续的箱子位于特殊位置上;维护一个最大值,然后左右相加就是答案。
    在这里插入图片描述
  • 代码
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=2e5+10,mod=1e9+7;
int F[N],S[N];
#define PII pair<int,int>
#define x first
#define y second
#define PB push_back

int deal(vector<int> &a,vector<int> &b)
{
    
    int n=a.size();
    int m=b.size();
    int sc=0;
    map<int,int> mp;
    for(auto &i:a){
        mp[i]=1;
    }
    for(auto &j:b){
        if(mp[j]==1) sc++;
    }
    int ans=sc;
    for(int i=0;i<m;i++){
        if(mp[b[i]]==1){
            sc--;
            continue;
        }
        int fir=upper_bound(a.begin(),a.end(),b[i])-a.begin();
        int sec=lower_bound(b.begin(),b.end(),b[i]-fir+1)-b.begin();
        ans=max(ans,i+1-sec+sc);
    }
    return ans;
}

void solve()
{
    vector<int> Fl,Fr,Sl,Sr;
	int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>F[i];
    for(int i=1;i<=m;i++) cin>>S[i];
    for(int i=1;i<=n;i++) {
        if(F[i]>0) Fr.push_back(F[i]);
        else Fl.push_back(-F[i]);
    }
    for(int i=1;i<=m;i++) {
        if(S[i]>0) Sr.push_back(S[i]);
        else Sl.push_back(-S[i]);
    }
    reverse(Fl.begin(),Fl.end());
    reverse(Sl.begin(),Sl.end());
    int ans=0;
    ans+=deal(Fl,Sl);
    ans+=deal(Fr,Sr);
    cout<<ans<<endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
}

E题

  • 题意
    在这里插入图片描述
    给你一个图,每条边都有一个字母。给你m次操作,要么增加一条边,要么去掉一条边,要么查询是否存在路径长度为k的一条路,正着走,反着走一样。
  • 思路
    如果k是奇数,那么只要找到一组双向边的就行。
    如果k是偶数,那么需要一组双向边的权值一样。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
int a[maxn];
map<pii , char> e;
int main()
{
    ios::sync_with_stdio(false);
    int n , m; cin >> n >> m;
    int x , y; x = y = 0;
    for (int i = 1 ; i <= m ; i++){
        char c; cin >> c;
        int u , v;
        char w;
        if (c == '+'){
            cin >> u >> v >> w;
            e[mp(u,v)] = w;
            pii g = mp(v,u);
            x += (e.find(g) != e.end());
            y += (e.find(g) != e.end() && e[g] == w);
        }
        else if (c == '-'){
            cin >> u >> v;
            w = e[mp(u,v)];
            e.erase(mp(u,v));
            pii g = mp(v,u);
            x -= (e.find(g) != e.end());
            y -= (e.find(g) != e.end() && e[g] == w);

        }else {
        	//x代表图中存在几组正向反向联通的。
        	//y代表图中存在几组不仅正反向联通,而且权值相同的。
            int k; cin >> k;
            if (k & 1) cout << (x ? "YES" : "NO") << endl;
            else cout << (y ? "YES" : "NO") << endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问组成,选手需要根据给定的问描述和测试用例,编写程序来解决这些问。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的码泰君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值