Code forces round #744 (Div. 3)

Before I start, I still want to celebrate in advance. For the first time, I write so many problem solutions QwQ ~ ~.

A

Click here to view the corresponding topic.
Simulation of this problem
Directly count the number of letters of a, b and c, and finally judge whether the number of c is equal to the number of a+b.

Time complexity O ( n ) O(n) O(n)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10,INF = 1e9 + 1;
int a,b,c;

void solve()
{
   a = 0,b = 0,c = 0;
   string s;
   cin >> s;
   for(int i = 0;i < s.size();i ++){
        if(s[i] == 'A') a ++;
        if(s[i] == 'B') b ++;
        if(s[i] == 'C') c ++;
    }
   if(b == a + c) puts("Yes");
   else puts("No");
}
int main()
{
    int t;
    cin >> t;
    while(t--){
         solve();
    }
    return 0;
}


B

Click here to view the corresponding topic.
This problem involves algorithm: sorting
This question is to sort this string of data.

My idea is to sort the original sequence into a new sequence, then compare the new sequence with the original sequence, traverse all sequences, determine the moving interval of equal elements between the original sequence and the new sequence, record each moving interval, and change the sequence itself.

Time complexity O ( n l o g n ) O(nlogn) O(nlogn)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10,INF = 1e9 + 1;
int a[N],b[N],c[N];
struct S
{
    int l,r,d;
};

void solve()
{
   vector<S> res;
   int n;
   cin >> n;
   for(int i = 1;i <= n ;i ++){
        cin >> a[i];
        b[i] = a[i];
   }

    sort(b + 1,b + n + 1);

    for(int i = n;i >= 1;i --) {
        int k = 0;
        for(int j = i;j >= 1;j --)
            if(a[j] == b[i]){
                k = j;
                break;
            }

        if(k && k != i){
            res.push_back({k,i,1});
            for(int d = 1;d <= k - 1;d ++) c[d] = a[d];
            for(int d = k;d <= n - 1;d ++) c[d] = a[d + 1];
            c[n] = a[k];
            memcpy(a,c,sizeof(c));
        }

    }

    cout << res.size() << '\n';
    for(int i = 0;i < res.size();i ++) cout << res[i].l << " " << res[i].r << " " << res[i].d << '\n';
}
int main()
{
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


C

Click here to view the corresponding topic.
Simulation of this problem
This question is to see if you can simulate all the √ check boxes and whether you can cover all the marked cells. I simulated all the √ check boxes here ′ ∗ ′ '*' The process of continuously extending the '*' element to the upper and both sides is OK (here I use recursion to extend).

Time complexity O ( n 2 ∗ m ) O(n^2*m) O(n2∗m)

#include<bits/stdc++.h>
using namespace std;
const int N = 110,INF = 1e9;
char g[N][N];
bool st[N][N];
int n,m,k;
struct Node
{
    int a,b,c;
};
vector<Node> v;

bool judge()
{
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            if(!st[i][j] && g[i][j] == '*')
                return false;
    return true;
}
void init()
{
    for(int i = 0;i < N;i ++)
        for(int j = 0;j < N;j ++){
            g[i][j] = '.';
            st[i][j] = false;
        }
}
int dg(int x,int y1,int y2,int h)
{
    if(g[x][y1] != '*' || g[x][y2] != '*'){
        if(h >= k){
            for(int i = 0;i < v.size();i ++)
                st[v[i].a][v[i].b] = true,st[v[i].a][v[i].c] = true;
            return 1;
        }
        return 0;
    }
    v.push_back({x,y1,y2});
    dg(x - 1,y1 + 1,y2 - 1,h + 1);
}
void solve()
{
    cin >> n >> m >> k;
    init();
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            cin >> g[i][j];

    int maxn = -INF;
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            if(g[i][j] == '*'){
                int t = dg(i - 1,j + 1,j - 1,0);
                if(t) st[i][j] = true;
                v.clear();
            }

    if(judge()) puts("Yes");
    else puts("No");
}
int main()
{
    int t;
    cin >> t;
    while (t -- ){
        solve();
    }
    return 0;
}

D

Click here to view the corresponding topic.
This problem involves algorithm: greed
This problem should be greedy to select the two largest elements, reduce the two elements by 1, and finally achieve the goal that the whole sequence has only one non-0 element.

Here, priority queues are used for data storage to facilitate trial sorting. Select the queue header as the maximum value.

Time complexity O ( n ) O(n) O(n)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 1e5 + 10;

void solve()
{
    priority_queue<PII, vector<PII>, less<PII>> q;
    vector<PII> res;
    int n;
    cin >> n;

    for(int i = 1;i <= n;i ++){
        int x;
        cin >> x;
        if(x) q.push({x,i});
    }

    while(q.top().first && q.size() > 1){
        int x = q.top().first;
        int index_x = q.top().second;
        q.pop();

        int y = q.top().first;
        int index_y = q.top().second;
        q.pop();

        x --,y --;
        res.push_back({index_x,index_y});

        if(x > 0) q.push({x,index_x});
        if(y > 0) q.push({y,index_y});
    }

    cout << res.size() << '\n';
    if(res.size()){
        for(int i = 0;i < res.size();i ++) cout << res[i].first << " " << res[i].second << '\n';
    }
}
int main()
{
    int t;
    cin >> t;
    while(t -- ){
        solve();
    }
    return 0;
}

E1

Click here to view the corresponding topic.
This problem involves the algorithm: double ended queue + greedy
Simply simulate the meaning of the question and greedily add elements from front to back.

Time complexity O ( n ) O(n) O(n)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10,INF = 1e9 + 7;
int a[N];

void solve()
{
    int n;
    deque<int> q;
    cin >> n;

    for(int i = 0;i < n;i ++){
        int x;
        cin >> x;
        if(q.empty() || x < q.front()) {
            q.push_front(x);
        }else {
            q.push_back(x);
        }
    }
    for(auto it : q) cout << it << " ";
    puts("");
}
int main()
{
    int t;
    cin >> t;
    while(t -- ){
        solve();
    }
    return 0;
}

Tags: Algorithm data structure

Posted on Mon, 11 Oct 2021 15:54:46 -0400 by MikeA