SMU Spring 2023 Trial Contest Round 11

发布时间 2023-06-01 18:20:29作者: Ke_scholar

A. The Text Splitting

题意:给出字符串长度,给出p和q两种切割方式,任选其中一种,把字符串分割输出结果。

 

题解:先进行判断,p和q是否能整个的分割n,利用p和q的函数关系判断(见代码),再计算有几个p几个q,再进行输出即可

void solve()
{
    cin >> n >>  p >> q;
    cin >> s;
    if(p > q)
        swap(p,q);
    m = (n + p - 1) / p;
    for(int i = m; i >= 0; i--){
        if( (n - i * p) % q == 0){
            int l = i;
            while(l != 0){
                d.push_back(s.substr(0,p));
                s.erase(0,p);
                l--;
            }
            int r = (n - i * p) / q;
            while(r != 0){
                d.push_back(s.substr(0,q));
                s.erase(0,q);
                r--;
            }
            cout << d.size() << endl;
            for(auto i : d){
                cout << i << endl;
            }
            exit(0);
        }
    }
    cout << -1 << endl;
}

 

B. HDD is Outdated Technology

题解:就是从1到n之间对其序号的差取一个绝对值,再累加即可

oid solve()
{
    cin >> n;
    vector<PII> a(n);
    map<int,int> zz;
    for(int i = 0;i < n;i ++){
        int x;
        cin >> x;
        zz[x] = i + 1;
    }
    int p = 1;
    while(p != n){
        ans += abs(zz[p] - zz[p + 1]);
        p++;
    }
    cout << ans << endl;
}

 

C. Replace To Make Regular Bracket Sequence

题意:四种括号匹配问题。左括号不能匹配右括号时,可以更换左括号进行匹配,这时需要计数换了几个括号。问括号能否匹配,能匹配的话需要换多少个括号?

 

题解:对不符合的括号串先处理掉,然后利用堆栈解决,遇到右括号则与堆栈中的括号进行匹配。

oid solve()
{
    cin >> s;
    int one = 0,zero = 0;
    mp['<'] = mp['('] = mp['{'] = mp['['] = 1;
    mp['>'] = mp[')'] = mp['}'] = mp[']'] = 0;
    for(auto i : s){
        if(mp[i] == 1)
            one++;
        else
            zero ++;
        if(one < zero){
            cout << "Impossible" << endl;
            return ;
        }
    }
   // cout << one << ' ' << zero << endl;
    if(one != zero){
        cout << "Impossible" << endl;
        return ;
    }
    else{
        stack<char> tong;
        for(auto i : s){
            if(i == '<' || i == '[' || i == '{' || i == '('){
                tong.push(i);
            }
            else{
                if(i == ')'){
                    if(tong.top() != '(')
                        ans++;
                }
                else if(i == ']' && tong.top() != '[')
                    ans++;
                else if(i == '}' && tong.top() != '{')
                    ans++;
                else if(i == '>' && tong.top() != '<')
                    ans ++;
                tong.pop();
            }
        }
             cout << ans << endl;
    }
}

 

D. The Union of k-Segments

题解:

  1. 对点分类,分为左端点和右端点
  2. 所有点丢到一个vector里按照x坐标升序排序,坐标相同时优先考虑左端点
  3. 按照区间计数原理,从前往后扫描所有点,扫描到左端点计数器+1,右端点计数器-1,那么计数器>=k的区间都是合法区间
  4. 通过标记去维护合法区间的起始位置和结束位置,维护答案即可
#include  <map>
#include  <set>
#include  <cmath>
#include  <queue>
#include  <stack>
#include  <cstdio>
#include  <vector>
#include  <climits>
#include  <cstring>
#include  <cstdlib>
#include  <iostream>
#include  <algorithm>
#define inf 0x3f3f3f3f
#define endl '\n'
#define int long long

using namespace std;

const int N = 1e9 + 10, mod = 1e9 +7;

//typedef long long ll;
typedef pair<int,int> PII;
//queue<PII> q1;
map<char, int > mp;
//priority_queue <int,vector<int>,greater<int> > q2;
int n,m,t,k;
/*

 */
string s;
vector<PII> a,ans;
void solve()
{
    cin >> n >> k;
    for(int i = 0;i < n;i ++){
        int l,r;
        cin >> l >> r;
        a.push_back({l,0});
        a.push_back({r,1});
    }
    sort(a.begin(),a.end());
    int l = 0;
    for(auto [x,y] : a){
        if(y == 0){
            t++;
            if(t == k)
                l = x;
        }
        else{
            if(t==k)
                ans.push_back({l,x});
            t--;
        }
    }
    cout << ans.size() << endl;
    for(auto [i,j] : ans){
        cout << i << ' ' << j << endl;
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int Ke_scholar = 1;
    //cin >> Ke_scholar ;
    while(Ke_scholar--)
        solve();
    return 0;
}