[Offer收割]编程练习赛41

比赛日程安排

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;

    for(int i = 0; i < 200000; i++) cout << 1000000000 << ' ';

    fclose(stdout);
}

const int day[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
VI a[100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    //std::ios::sync_with_stdio(0), cin.tie(0);
    int n, m, t;
    scanf("%d", &t);

    while(t--) {
        bool ok = true;
        scanf("%d%d", &n, &m);

        for(int i = 1; i <= n; i++) a[i].clear();

        for(int i = 0; i < m; i++) {
            int mm, d, u, v, tmp = 0;
            scanf("%d-%d%d%d", &mm, &d, &u, &v);

            for(int j = 1; j < mm; j++)tmp += day[j - 1];

            tmp += d;
            a[u].push_back(tmp);
            a[v].push_back(tmp);
        }

        for(int i = 1; i <= n; i++) {
            sort(a[i].begin(), a[i].end());

            for(int j = 1; j < a[i].size(); j++) {
                if(a[i][j] - a[i][j - 1] <= 1) ok = false;
            }
        }

        if(ok) printf("YES
");
        else printf("NO
");
    }

    return 0;
}
View Code

反转子串

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
#include<set>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;

char str[6000000];
int l[6000000], r[6000000], s[6000000];

void work(int L, int R, int rev) {
    int i;
    if (L > R) return;
    if (rev & 1) {
        i = R;
        while (i >= L) {
            if (str[i] == ')') {
                work(l[i] + 1, i - 1, rev + 1);
                i = l[i] - 1;
            } else {
                putchar(str[i]);
                i--;
            }
        }
    } else {
        i = L;
        while (i <= R) {
            if (str[i] == '(') {
                work(i + 1, r[i] - 1, rev + 1);
                i = r[i] + 1;
            } else {
                putchar(str[i]);
                i++;
            }
        }
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //std::ios::sync_with_stdio(0), cin.tie(0);
    scanf("%s", str);
    int n = strlen(str);
    int dep = 0, top;
    memset(l, 0, sizeof(l));
    memset(r, 0, sizeof(r));
    for (int i = 0; i < n; i++) {
        if (str[i] == '(') {
            s[dep++] = i;
        } else if (str[i] == ')') {
            top = s[dep - 1];
            l[i] = top;
            r[top] = i;
            dep--;
        }
    }
    work(0, n - 1, 0);
    return 0;
}
//agfdecbhijk
View Code

区间问题

把所有区间按结束位置排序,从小到大处理。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;
    for (int i = 0; i < 200000; i++) cout << 1000000000 << ' ';
    fclose(stdout);
}

PII a[110000];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, x, y;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        a[i] = make_pair(y, x);
    }
    sort(a, a + n);
    int l = a[0].first - 1, r = a[0].first, ans = 2;
    for (int i = 1; i < n; i++) {
        if (a[i].second <= l) continue;
        if (a[i].second <= r) {
            ans++;
            l = r;
            r = a[i].first;
        } else {
            ans += 2;
            l = a[i].first - 1, r = a[i].first;
        }
    }
    cout << ans << endl;
    return 0;
}
View Code

01间隔矩阵

比我想的简单一点,我觉得会超时的代码AC了,可能是因为数据是随机生成所以长宽都不会太大的缘故。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    int n = 10, m = 10, x = 100, c;
    cout << n << ' ' << m << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < x; k++) c = rand() % 2;
            cout << c;
        }
        cout << endl;
    }
    fclose(stdout);
}

char a[3000][3000];
int h[3000], c[3000], l[3000], r[3000], tmph[3000], ans;
void calc(int ll, int rr) {
    for (int i = ll; i <= rr; i++) tmph[i] = h[i];
    tmph[ll - 1] = 0, tmph[rr + 1] = 0;
    l[ll] = ll - 1, r[rr] = rr + 1;
    for (int i = ll + 1; i <= rr; i++) {
        l[i] = i - 1;
        while (tmph[l[i]] >= tmph[i]) l[i] = l[l[i]];
    }
    for (int i = rr - 1; i >= ll; i--) {
        r[i] = i + 1;
        while (tmph[r[i]] >= tmph[i]) r[i] = r[r[i]];
    }
    for (int i = ll; i <= rr; i++) ans = max(ans, tmph[i] * (r[i] - l[i] - 1));
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    ans = 0;
    for (int i = 1; i <= n; i++) {
        h[0] = 0;
        if (i == 1) {
            for (int j = 1; j <= m; j++) h[j] = 1;
        } else {
            for (int j = 1; j <= m; j++)
                if (a[i][j] != a[i - 1][j]) h[j]++;
                else h[j] = 1;
        }
        c[1] = 0;
        for (int j = 2; j <= m; j++)
            if (a[i][j] != a[i][j - 1]) c[j] = c[j - 1];
            else c[j] = c[j - 1] ^ 1;
        int ptr = 1, ll, rr;
        while (ptr <= m) {
            ll = ptr;
            while (ptr + 1 <= m && c[ptr + 1] == c[ptr]) ptr++;
            rr = ptr;
            calc(ll, rr);
            ptr = rr + 1;
        }
    }
    cout << ans << endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/dramstadt/p/8117791.html