[Offer收割]编程练习赛40

Posted PoorLitt1eThin9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Offer收割]编程练习赛40相关的知识,希望对你有一定的参考价值。

不到一个小时AK,虽然是VP的,舒服,第一次。都简单的一比,没什么可说的。

查找三阶幻方

技术分享图片
#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);
}

int a[200][200];
bool check(int x, int y) {
    int sum = a[x][y] + a[x][y + 1] + a[x][y + 2];

    for(int i = 0; i < 3; i++) {
        if(a[x + i][y] + a[x + i][y + 1] + a[x + i][y + 2] != sum) return false;

        if(a[x][y + i] + a[x + 1][y + i] + a[x + 2][y + i] != sum) return false;

        if(a[x][y] + a[x + 1][y + 1] + a[x + 2][y + 2] != sum) return false;

        if(a[x + 2][y] + a[x + 1][y + 1] + a[x][y + 2] != sum) return false;
    }

    return true;
}
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 = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }

    int ans = 0;

    for(int i = 0; i + 2 < n; i++) {
        for(int j = 0; j + 2 < m; j++) {
            if(check(i, j)) ans++;
        }
    }

    cout << ans << endl;
    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<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);
}
bool f[10];
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    lint x;
    int t, k;
    cin >> t;

    while(t--) {
        cin >> x >> k;
        int st = 1, cnt = 0;

        while(st != k) {
            cnt++;

            if(st == 0 || cnt > 1000) {
                x = -1;
                break;
            }

            st = st * (x % 10) % 10;
            x -= 2;

            if(x <= 0) {
                x = -1;
                break;
            }
        }

        cout << x << endl;
    }

    return 0;
}
View Code

01间隔方阵

技术分享图片
#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);
}

int f[1005][1005], fl[1005][1005], fu[1005][1005];
char a[1005][1005];
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 = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }

    memset(f, 0, sizeof(f));
    memset(fl, 0, sizeof(fl));
    memset(fu, 0, sizeof(fu));

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(j == 0) {
                f[i][j] = 1;
                fl[i][j] = 1;
            } else {
                if(a[i][j] == a[i][j - 1]) fl[i][j] = 1;
                else fl[i][j] = fl[i][j - 1] + 1;
            }

            if(i == 0) {
                f[i][j] = 1;
                fu[i][j] = 1;
            } else {
                if(a[i][j] == a[i - 1][j]) fu[i][j] = 1;
                else fu[i][j] = fu[i - 1][j] + 1;
            }
        }
    }

    int ans = 1;

    for(int i = 1; i < n; i++) {
        for(int j = 1; j < m; j++) {
            if(a[i][j] != a[i - 1][j - 1]) {
                f[i][j] = 1;
            } else {
                int tmp = 0x3FFFFFFF;
                tmp = min(tmp, fl[i][j]);
                tmp = min(tmp, fu[i][j]);
                tmp = min(tmp, f[i - 1][j - 1] + 1);
                f[i][j] = tmp;

                if(ans < tmp) ans = tmp;
            }
        }
    }

    cout << ans << endl;
    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<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);
}

class SegmentTree {
private:
    int *data, *lazy;
    void pushup(int rt) {
        data[rt] = max(data[rt << 1], data[rt << 1 | 1]);
    }
    void pushdown(int rt, int m) {
        if(lazy[rt] == 0) return;

        lazy[rt << 1] = lazy[rt];
        lazy[rt << 1 | 1] = lazy[rt];
        data[rt << 1] = lazy[rt];
        data[rt << 1 | 1] = lazy[rt];
        lazy[rt] = 0;
    }
public:
    SegmentTree(int n) : data((int *)malloc((n << 3) * sizeof(int))), lazy((int *)malloc((n << 3) * sizeof(int))) {}
    void Build(int * base, int l, int r, int rt) {
        lazy[rt] = 0;

        if(l == r) data[rt] = base[l];
        else {
            int mid = (l + r) >> 1;
            Build(base, l, mid, rt << 1);
            Build(base, mid + 1, r, rt << 1 | 1);
            pushup(rt);
        }
    }
    void Modify(int l, int r, int rt, int L, int R, int v) {
        if(L <= l && R >= r) {
            lazy[rt] = v;
            data[rt] = v;
            return;
        }

        pushdown(rt, r - l + 1);
        int mid = (l + r) >> 1;

        if(L <= mid)
            Modify(l, mid, rt << 1, L, R, v);

        if(R > mid)
            Modify(mid + 1, r, rt << 1 | 1, L, R, v);

        pushup(rt);
    }
    int QueryPoint(int l, int r, int rt, int val) {
        if(l == r) return data[rt];

        pushdown(rt, r - l + 1);
        int mid = (l + r) >> 1;
        int ret = 0;

        if(val <= mid) ret = QueryPoint(l, mid, rt << 1, val);
        else ret = QueryPoint(mid + 1, r, rt << 1 | 1, val);

        pushup(rt);
        return ret;
    }
    int QuerySegment(int l, int r, int rt, int L, int R) {
        pushdown(rt, r - l + 1);

        if(L == l && R == r) return data[rt];

        int mid = (l + r) >> 1;

        if(R <= mid) return QuerySegment(l, mid, rt << 1, L, R);

        if(mid < L) return QuerySegment(mid + 1, r, rt << 1 | 1, L, R);

        return max(QuerySegment(l, mid, rt << 1, L, mid), QuerySegment(mid + 1, r, rt << 1 | 1, mid + 1, R));
    }
};
SegmentTree st(100005);
int h[100005];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    memset(h, 0, sizeof(h));
    st.Build(h, 1, 100000, 1);
    int n, l, r;
    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> l >> r;
        int tmp = st.QuerySegment(1, 100000, 1, l, r);
        cout << (tmp + 1) << endl;
        st.Modify(1, 100000, 1, l, r, tmp + 1);
    }

    return 0;
}
View Code

以上是关于[Offer收割]编程练习赛40的主要内容,如果未能解决你的问题,请参考以下文章

HihoCoder1665方块游戏([Offer收割]编程练习赛40)(线段树)

Hihocoder1662 : 查找三阶幻方([Offer收割]编程练习赛40)(暴力)

HihoCoder1663双阶乘的末尾数字([Offer收割]编程练习赛40)(暴力||数学)

hihocoder - [Offer收割]编程练习赛17

[Offer收割]编程练习赛42

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)