2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 1 B, I (structure)

Topic link: B brackets , I limit the arrangement of non-prime pairs

B-bracket

Topic

  • Construct a non-empty bracket string so that the number of "()" subsequences is n
  • The length of the string does not exceed 1e5

Ideas

  • The same length is 10, 5*5> 4 * 6, the square is big, so start with the root number n
  • First output the root number n left parentheses, and then there are d1 = n / root number n right parentheses, and d2 = n% is left to match the root number n to the parenthesis, then insert it to the left of the last d2 right parenthesis of the right parenthesis A left parenthesis is over.

ac code  

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int k; cin >> k;
    if(k == 0){
        cout << ")(" << endl;
        return 0;
    }
    string a;
    int d = sqrt(k); k -= d * d;
    for(int i = 1; i <= d; i ++) a += "(";
    for(int i = 1; i <= d; i ++) a += ")";
    int d1 = k / d, d2 = k % d;
    for(int i = 1; i <= d1; i ++) a += ")";
    for(int i = 0; i < a.size(); i ++){
        if(a.size() - i == d2) cout << "(";
        cout << a[i];
    }
    return 0;
}

I-Limit the arrangement of non-prime pairs

Topic

  • Construct a full permutation of n, so that there are exactly k adjacent pairs in this sequence, and the two numbers are not mutually prime, and can output anything, if not -1.
  • Range: 2≤n≤1e5, 0≤k≤n/2

Ideas

  • There are n/2 even numbers, and the even numbers are not relatively prime, but there are at most n/2-1 pairs that are not relatively prime in even numbers, so there are two cases
  1. When k==n/2, then find an even odd number that is not coprime to a certain even number. Here we find a 3 and 6 match. Then the beginning is 3642, and then all the remaining even numbers 8 10 12... are output, followed by all the odd numbers 1 3 5...
  2. When k <n / 2, if n is odd, output n first, then output k + 1 even numbers from large to small, the k+1 even number is ai, and then output k+1 odd numbers from ai+1 , The last remaining number is output from the beginning 1 2 3...
  • For example, 12 2->12 10 8 | 7 9 11 | 1 2 3 4 5 6 (three parts), 11 3->11 | 10 8 6 4 | 3 5 7 9 | 1 2 (four parts), 10 5- >3 | 6 4 2 8 10 | 1 5 7 9 (three parts)
  • Since 1 is relatively prime to any number, it is said that (2 1), (3 1), (4 2), (5 2) do not exist

ac code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> ans;
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    if((n==4||n==5)&&m==2){
        puts("-1");
        return 0;
    }
    if((n == 2 || n == 3 ) && m == 1){
        puts("-1");
        return 0;
    }
    if(m < n / 2){
        if(n & 1) ans.push_back(n);
        int st = n / 2 * 2; //最大的偶数
        for(int i = 0; i <= m; i ++){ //m+1个偶数,m对不互质
            ans.push_back(st);
            st -= 2;
        }
        st ++; //m+1个奇数,与上面偶数对应,相差1
        for(int i = 0; i <= m; i ++){
            ans.push_back(st);
            st += 2;
        }
        int dd = n - (m + 1) * 2; //剩下的数
        if(n & 1) dd --; //如果n是奇数,开头输出n,那么剩下的数-1
        for(int i = 1; i <= dd; i ++){
            ans.push_back(i);
        }
    }else{
        ans.push_back(3);
        ans.push_back(6);
        ans.push_back(4);
        ans.push_back(2);
        for(int i = 8; i <= n; i += 2) ans.push_back(i);
        for(int i = 1; i <= n; i += 2){
            if(i == 3) continue; //之前输出过
            ans.push_back(i);
        }
    }
    for(int i = 0; i < ans.size(); i ++){
        if(i) cout << " ";
        cout << ans[i];
    }
    cout << endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43911947/article/details/113531506