Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3)

A - Math Problem

The meaning of problems: a line segment n [l, r], may seek a degenerate segment coupled to a point, each point has at least one common to all segments.

Solution: find the rightmost and leftmost left point right point, point left> right point, then you put this line to link the two, or your line is now [left point, right point] just one point on.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
void test_case() {
    int n;
    scanf("%d", &n);
    int L = 1, R = 1e9;
    for(int i = 1; i <= n; ++i) {
        int l, r;
        scanf("%d%d", &l, &r);
        L = max(L, l);
        R = min(R, r);
    }
    int ans = 0;
    if(L > R)
        ans = L - R;
    printf("%d\n", ans);
}
 
int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

B - Box

The meaning of problems: a prefix to the maximum sequence, to meet any request that a permutation sequence.

Solution: lexicographically generated to meet a maximum alignment of this sequence. In fact because of this sequence is increasing, how chaos can be constructed. First, the maximum change in each place is certainly put culprit, then there may be some positions after the discharge is not swept again, this time to give lower_bound its maximum value at a value greater than or equal its maximum. And then - then that is less than its maximum value, put it in the past on it. Or begin to put each iterator, iterator begin until it exceeds the maximum value -1, so the lexicographically smallest.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int q[100005];
int p[100005];
set<int> tmp;
 
void test_case() {
    int n;
    scanf("%d", &n);
    tmp.clear();
    for(int i = 1; i <= n; ++i) {
        p[i] = -1;
        tmp.insert(i);
    }
    for(int i = 1; i <= n; ++i)
        scanf("%d", &q[i]);
    p[1] = q[1];
    tmp.erase(q[1]);
    for(int i = 2; i <= n; ++i) {
        if(q[i] > q[i - 1]) {
            p[i] = q[i];
            tmp.erase(q[i]);
        }
    }
    for(int i = 1; i <= n; ++i) {
        if(p[i] == -1) {
            auto t = tmp.lower_bound(q[i]);
            if(t == tmp.begin()) {
                puts("-1");
                return;
            }
            --t;
            p[i] = *t;
            tmp.erase(t);
        }
    }
    for(int i = 1; i <= n; ++i)
        printf("%d%c", p[i], " \n"[i == n]);
}
 
int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

C - Messy

Question is intended: to a "(", ")" is not more than equal to the length of the brackets 2000 series of brackets, each requires a reversal subinterval, so that the last legal string bracket, and through zero exactly k times.

Solution: greedy, greedy every time the first two characters become "(", ")", so finally there is n / 2 times zero, then the beginning of a short start to destroy it (if necessary, do not overdo ), with at most n / 2 + 1 times. Because the reasons for the network did not pay up, the problem is not the strength to naturally converge.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, k;
string s;
vector<pair<int, int> > ans;

void test_case() {
    ans.clear();
    cin >> n >> k >> s;
    int curp = 1;
    while(s.length()) {
        if(s[0] == '(' && s[1] == ')') {
            s = s.substr(2, s.length());
            curp += 2;
        } else if(s[0] == ')' && s[1] == '(') {
            ans.push_back({curp + 0, curp + 1});
            s = s.substr(2, s.length());
            curp += 2;
        } else if(s[0] == ')') {
            int j = 2;
            while(s[j] == s[0])
                ++j;
            ans.push_back({curp + 0, curp + j});
            string s1 = s.substr(0, j + 1);
            string s2 = s.substr(j + 1, s.length());
            reverse(s1.begin(), s1.end());
            s = s1 + s2;
            s = s.substr(2, s.length());
            curp += 2;
        } else {
            int j = 2;
            while(s[j] == s[0])
                ++j;
            ans.push_back({curp + 1, curp + j});
            string s0 = s.substr(0, 1);
            string s1 = s.substr(1, j);
            string s2 = s.substr(j + 1, s.length());
            reverse(s1.begin(), s1.end());
            s = s0 + s1 + s2;
            s = s.substr(2, s.length());
            curp += 2;
        }
    }
    int tk = n / 2 - k;
    if(1 + 2 * tk >= 2)
        ans.push_back({2, 1 + 2 * tk});
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); ++i)
        cout << ans[i].first << " " << ans[i].second << endl;
}

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

NOTE: to be noted that the method used substr string, really sick people, substr (a, b) is taken up from a starting position of b character substrings formed. In fact, if the use of a static array of characters will not be convenient? If you write a String class can use your own brand-new way to go.

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/11924913.html