首页 > 其他分享 >AtCoder Beginner Contest 361)

AtCoder Beginner Contest 361)

时间:2024-07-07 09:30:21浏览次数:1  
标签:AtCoder begin qr Beginner int cin 立方体 empty 361

推荐个C++编程仓库模板

https://github.com/yxc-s/programming-template

A - Insert

void solve() {
	int n, k, x;
	cin >> n >> k >> x;

	vector<int> a(n);
	for (auto& x : a){
		cin >> x;
	}
	a.insert(a.begin() + k, x);

	for (int i = 0; i < a.size(); ++i){
		cout << a[i] << " \n"[i == n];
	}
}

B Intersection of Cuboids

题意:给定空间中2个立方体,每个立方体用两个三维的对角线坐标表示。问两个立方体是否相交。

思路:只要在任意一个维度上,某个立方体的终点<=另一个立方体的起点,则无解。

总结:一开始只考虑了一个立方体的每个维度上都在另一个立方体内部的情况,其实3个维度,不管哪个立方体满足条件都可以。

void solve() {
    vector<array<int, 6>> a(2);
    for (int i = 0; i < 2; ++i){
        for (auto& x : a[i]){
            cin >> x;
        }
    }

    for (int i = 0; i < 3; ++i){
        if (a[0][i + 3] <= a[1][i] || a[1][i + 3] <= a[0][i]){
            cout << "No\n";
            return;
        }
    }

    cout << "Yes\n";
}

C - Make Them Narrow

题意:给定一个序列,删除k个数后,让序列中最大与最小值的差最小。

思路:排序后,检查长度为(n - k)的滑动窗口中最左最右元素的差值最小值。

总结:一开始以为,以为要删除k个数,让序列组成的数最小和最大,然后求他们的差值,使用了一个next数组,数组中表示当前位置的下一个数[1~9]所在的位置是多少,依次检查当前的数最有可能变成的数需要删除多少个元素。。最后准备测试了发现读错题了。。

void solve() {
	int n, k;
	cin >> n >> k;

	vector<int> a(n);
	for (auto& x : a){
		cin >> x;
	}

	sort(a.begin(), a.end());

	int t = n - k - 1;
	int ans = INF;
	for (int i = 0; i + t < n; ++i){
		ans = min(ans, a[i + t] - a[i]);
	}

	cout << ans << endl;
}

D - Go Stone Puzzle

题意:搜索问题

思路:直接搜就行

总结:不知道为什么双向bfs没过,但是单向bfs过了??

void solve() {
    int n;
    string s, t;
    cin >> n >> s >> t;

    if (count(s.begin(), s.end(), 'W') != count(t.begin(), t.end(), 'W')){
        cout << "-1\n";
        return;
    }

    if (s == t){
        cout << "0\n";
        return;
    }

    s += "..";
    t += "..";
    unordered_set<string> sett{s};
    unordered_set<string> sett_r{t};
    auto bfs = [&](){
        queue<pair<string, int>> q;
        queue<pair<string, int>> qr;
        q.emplace(s, 0);
        qr.emplace(t, 0);
        while (!q.empty() || !qr.empty()){
            int step = q.front().second;
            while (!q.empty() && q.front().second <= step){
                auto[cur, cnt] = q.front();
                q.pop();
                int p = cur.find('.');
                for (int i = 0; i < n + 1; ++i){
                    if (abs(i - p) <= 1){
                        continue;
                    }
                    string tt = cur;
                    swap(tt[i], tt[p]);
                    swap(tt[i + 1], tt[p + 1]);
                    #if defined(double_search)
                    if (sett_r.count(tt)){
                        cout << 2 * cnt + 1 << '\n';
                        return true;
                    }
                    #else
                    if (tt == t){
                        cout << cnt + 1<< '\n';return true;
                    }
                    #endif
                    if (!sett.count(tt)){
                        sett.insert(tt);
                        q.emplace(tt, cnt + 1);
                    }
                }
            }
            #if defined(double_search)
            while (!qr.empty() && qr.front().second <= step){
                auto [cur, cnt] = qr.front();
                qr.pop();
                int p = cur.find('.');
                for (int i = 0; i < n + 1; ++i){
                    if (abs(i - p) <= 1){
                        continue;
                    }
                    string tt = cur;
                    swap(tt[i], tt[p]);
                    swap(tt[i + 1], tt[p + 1]);
                    if (sett.count(tt)){
                        cout << 2 * cnt + 2 << '\n';
                        return true;
                    }
                    if (!sett_r.count(tt)){
                        sett_r.insert(tt);
                        qr.emplace(tt, cnt + 1);
                    }
                }
            }
            #endif
        }

        return false;
    };

    if (!bfs()){
        cout << "-1\n";
    }
}

更新,双向bfs已A,时间从50多ms降到6ms,tle的原因是第一个队列可能已经空了,导致step计数错了,但是第二个队列还没空,但是step是个垃圾值,所以导致一直循环。

#define double_search
void solve() {
    int n;
    string s, t;
    cin >> n >> s >> t;

    if (count(s.begin(), s.end(), 'W') != count(t.begin(), t.end(), 'W')){
        cout << "-1\n";
        return;
    }

    if (s == t){
        cout << "0\n";
        return;
    }

    s += "..";
    t += "..";
    unordered_set<string> sett{s};
    unordered_set<string> sett_r{t};
    auto bfs = [&](){
        queue<pair<string, int>> q;
        queue<pair<string, int>> qr;
        q.emplace(s, 0);
        qr.emplace(t, 0);
        while (!q.empty() || !qr.empty()){
            int step = q.front().second;
            while (!q.empty() && q.front().second <= step){
                auto[cur, cnt] = q.front();
                q.pop();
                int p = cur.find('.');
                for (int i = 0; i < n + 1; ++i){
                    if (abs(i - p) <= 1){
                        continue;
                    }
                    string tt = cur;
                    swap(tt[i], tt[p]);
                    swap(tt[i + 1], tt[p + 1]);
                    #if defined(double_search)
                    if (sett_r.count(tt)){
                        cout << 2 * cnt + 1 << '\n';
                        return true;
                    }
                    #else
                    if (tt == t){
                        cout << cnt + 1<< '\n';return true;
                    }
                    #endif
                    if (!sett.count(tt)){
                        sett.insert(tt);
                        q.emplace(tt, cnt + 1);
                    }
                }
            }
            #if defined(double_search)
            step = qr.front().second;
            while (!qr.empty() && qr.front().second <= step){
                auto [cur, cnt] = qr.front();
                qr.pop();
                int p = cur.find('.');
                for (int i = 0; i < n + 1; ++i){
                    if (abs(i - p) <= 1){
                        continue;
                    }
                    string tt = cur;
                    swap(tt[i], tt[p]);
                    swap(tt[i + 1], tt[p + 1]);
                    if (sett.count(tt)){
                        cout << 2 * cnt + 2 << '\n';
                        return true;
                    }
                    if (!sett_r.count(tt)){
                        sett_r.insert(tt);
                        qr.emplace(tt, cnt + 1);
                    }
                }
            }
            #endif
        }

        return false;
    };

    if (!bfs()){
        cout << "-1\n";
    }
}

标签:AtCoder,begin,qr,Beginner,int,cin,立方体,empty,361
From: https://www.cnblogs.com/yxcblogs/p/18288185

相关文章

  • Denso Create Programming Contest 2024(AtCoder Beginner Contest 361)
    DensoCreateProgrammingContest2024(AtCoderBeginnerContest361)\(A\)Insert\(AC\)循环结构。点击查看代码inta[200];intmain(){intn,k,x,i;cin>>n>>k>>x;for(i=1;i<=n;i++){cin>>a[i];cout......
  • ABC361
    A.Insert模拟代码实现n,k,x=map(int,input().split())a=list(map(int,input().split()))a.insert(k,x)print(*a)B.IntersectionofCuboids模拟代码实现#include<bits/stdc++.h>#definerep(i,n)for(inti=0;i<(n);++i)usingnamespacest......
  • AtCoder Beginner Contest 361 补题记录(A~F)
    开题顺序:A-C-F-D-B-E。A直接模拟即可。boolbegmem;#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;classFastIO{public:intread(){into=1,x;charch;while(!isdigit(ch=getchar())){if......
  • Solution - Atcoder ARC125E Snack
    观察到这种都是数量上限的限制,且求\(\max\)。所以可以考虑网络流建模,而流量就对应着给的糖果数。令\(S\)为源点,\(T\)为汇点,编号为\(1\simn\)的点对应的糖果的种类,编号为\(n+1\simn+m\)的点对应的小孩。连边\((S,i,a_i)\),表示第\(i\)种糖果数不超过\(a_i\)......
  • #贪心#洛谷 3615 如厕计划
    题目传送门分析如果男生数目比女生数目多显然无解,在原队列的基础上考虑调换实际是将男生往前移实际上不满意度就是最后一位女生后移了多少位,记女生为一,男生为负一,运用数学归纳法证明只要后缀最小值不低于负一,那么一定存在一种方案,实际上就是求出后缀最小值,并将其调整至不低......
  • AtCoder Beginner Contest 359
    AtCoderBeginnerContest359A-CountTakahashi有\(n\)个字符串,每个串要么是Takahashi要么是Aoki,问有多少个字符串是Takahashi额....这还要有题解吗(?)#include<iostream>#include<cstring>usingnamespacestd;intmain(){stringa;intn,ans=0;cin>......
  • Solution - Atcoder AGC010E Rearranging
    因为只有相邻的互质的\(a_i\)可以交换,那么就说明不互质的\(a_i\)无法相交换,对应的位置关系也就不会改变。于是可以考虑图论建模,对于\(\gcd(a_i,a_j)>1\),连边\((i,j)\)。那么对于先手,就相当于要把这些边定向,变为一个DAG。而后手因为要保证最大,所以肯定是在DAG上跑......
  • AtCoder Beginner Contest 359
    AtCoderBeginnerContest359这场我赛时打的非常不好,只做出了\(2\)题。A-CountTakahashi签到//Problem:A-CountTakahashi//Contest:AtCoder-UNIQUEVISIONProgrammingContest2024Summer(AtCoderBeginnerContest359)//URL:https://atcoder.jp/conte......
  • Solution - Atcoder AGC034F RNG and XOR
    考虑到这个边权是\(\oplus\),那么说明对于\((u,v)\),\(u\tov\)和\(v\tou\)的边权实质是相同的。那么对于\(0\tox\),就可以反过来,记录下对应的路径,从\(x\)开始走,那么第一次走到\(0\)的时候也就是从\(0\)第一次走到\(x\)的时候。于是就转化为了\(x\)为起点,第一次......
  • AtCoder Beginner Contest 043
    D-Unbalanced只需要找到形如\(XX\)、\(XYX\)的字串即可。即两个相同字符之间最多间隔一个字符。证明:若不然,\(AXYA\),每加一个字符\(A\),都要增加多余字符\(XY\),不可能符合要求。#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;intmain(){ ios......