头像

BOBLT

NCUT




离线:23小时前


最近来访(12)
用户头像
蓬蒿人
用户头像
吃花椒的喵醤
用户头像
leimingze
用户头像
Botton
用户头像
TUER
用户头像
红色喷火龙
用户头像
fighting2021
用户头像
chaos_60
用户头像
卖萌的虎鲸

活动打卡代码 AcWing 482. 合唱队形

BOBLT
1天前
/**
 *    author:  BOBLT
 *    created: 27.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    vector<int> p(n + 1), up(n + 1), down(n + 1);
    for (auto &i : p) {
        cin >> i;
    }
    for (int i = 0; i < n; ++i) {
        up[i] = 1;
        for (int j = 0; j < n; ++j) {
            if (p[j] < p[i]) {
                up[i] = max(up[i], up[j] + 1);
            }
        }
    }
    for (int i = n - 1; ~i; --i) {
        down[i] = 1;
        for (int j = n - 1; j > i; --j) {
            if (p[j] < p[i]) {
                down[i] = max(down[i], down[j] + 1);
            }
        }
    }
    int res = 0;
    for (int k = 0; k < n; ++k) {
        res = max(res, up[k] + down[k] - 1);
    }
    cout << n - res << '\n';
    return 0;
}


活动打卡代码 AcWing 1014. 登山

BOBLT
1天前

老老实实的按顺序写,不要给自己找麻烦qwq

/**
 *    author:  BOBLT
 *    created: 27.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    vector<int> alt(n + 1), up(n + 1), down(n + 1);
    for (auto &i : alt) {
        cin >> i;
    }
    int res = 0;
    for (int i = 0; i < n; ++i) {
        up[i] = 1;
        for (int j = 0; j < i; ++j) {
            if (alt[i] > alt[j]) {
                up[i] = max(up[i], up[j] + 1);
            }
        }
    }
    // for (int i = 0; i < n; ++i) {
    //     down[i] = 1;
    //     for (int j = 0; j < i; ++j) {
    //         if (alt[i] < alt[j]) {
    //             down[i] = max(down[i], down[j] + 1);
    //         }
    //     }
    // }
    // reverse(down.begin(), down.end());
    for (int i = n - 1; ~i; --i) {
        down[i] = 1;
        for (int j = n - 1; j > i; --j) {
            if (alt[i] > alt[j]) {
                down[i] = max(down[i], down[j] + 1);
            }
        }
    }
    for (int k = 0; k < n; ++k) {
        res = max(res, up[k] + down[k] - 1);
        // cerr << up[k] << " " << down[k] << '\n';
    }
    cout << res << '\n';
    return 0;
}



BOBLT
1天前
/**
 *    author:  BOBLT
 *    created: 27.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n; cin >> n;
    vector<int> seq;
    vector<int> dp(n + 1);
    for (int i = 0; i < n; ++i) {
        int x; cin >> x;
        seq.emplace_back(x);
    }
    int res = 0;
    for (int i = 0; i < n; ++i){
        dp[i] = 1;
        for (int j = 0; j < i; ++j) {
            if (seq[i] < seq[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
        res = max(res, dp[i]);
    }
    for (int i = 0; i < n; ++i){
        dp[i] = 1;
        for (int j = 0; j < i; ++j) {
            if (seq[i] > seq[j]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
        res = max(res, dp[i]);
    }
    cout << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt; cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}


活动打卡代码 AcWing 275. 传纸条

BOBLT
2天前
/**
 *    author:  BOBLT
 *    created: 26.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m; cin >> n >> m;
    vector<vector<vector<int>>> dp(n + m + 3, vector<vector<int>>(n + 3, vector<int>(n + 3)));
    vector<vector<int>> w(n + 3, vector<int>(m + 3));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> w[i][j];
        }
    }
    for (int k = 2; k <= n + m; ++k) {
        for (int x1 = 1; x1 <= n; ++x1) {
            for (int x2 = 1; x2 <= n; ++x2) {
                int y1 = k - x1, y2 = k - x2;
                if (y1 >= 1 and y1 <= m and y2 >= 1 and y2 <= m) {
                    int t = w[x1][y1];
                    if (x1 != x2) {
                        t += w[x2][y2];
                    }
                    int &st = dp[k][x1][x2];
                    for (int u = 0; u < 2; ++u) {
                        for (int v = 0; v < 2; ++v) {
                            st = max(st, dp[k - 1][x1 - u][x2 - v] + t);
                        }
                    }
                }
            }
        }
    }
    cout << dp[n + m][n][n] << '\n';
    return 0;
}



BOBLT
2天前
/**
 *    author:  BOBLT
 *    created: 26.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, m; cin >> n >> m;
    vector<int> vi;
    function<void(int, int)> Dfs = [&](int u, int v) {
        if (v <= 0) {
            for (auto &i : vi) {
                cout << i << " ";
            }
            cout << '\n';
            return;
        }
        for (int i = u; i <= n; ++i) {
            vi.emplace_back(i);
            Dfs(i + 1, v - 1);
            vi.pop_back();
        }
    };
    Dfs(1, m);
    return 0;
}


活动打卡代码 AcWing 1027. 方格取数

BOBLT
2天前
/**
 *    author:  BOBLT
 *    created: 26.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n; cin >> n;
    vector<vector<vector<int>>> dp(n + n + 3, vector<vector<int>>(n + 3, vector<int>(n + 3)));
    vector<vector<int>> g(n + 3, vector<int> (n + 3));
    int x, y, w;
    while (cin >> x >> y >> w, x or y or w) {
        g[x][y] = w;
    }
    auto Max = [&](int a, int b, int c, int d, int e) -> int {
        return max(a, max(b, max(c, max(d, e))));
    };
    for (int k = 2; k <= n + n; ++k) {
        for (int i1 = 1; i1 <= n; ++i1) {
            for (int i2 = 1; i2 <= n; ++i2) {
                int j1 = k - i1, j2 = k - i2;
                if (j1 >= 1 and j1 <= n and j2 >= 1 and j2 <= n) {
                    int t = g[i1][j1];
                    if (i1 != i2) {
                        t += g[i2][j2];
                    }
                    int &st = dp[k][i1][i2];
                    st = Max(st, dp[k - 1][i1][i2] + t, dp[k - 1][i1][i2 - 1] + t, dp[k - 1][i1 - 1][i2] + t, dp[k - 1][i1 - 1][i2 - 1] + t);
                }
            }
        }
    }
    cout << dp[n << 1][n][n] << '\n';
    return 0;
}



BOBLT
3天前

Flip Game

nowcoder链接:https://ac.nowcoder.com/acm/problem/106350
POJ链接:http://poj.org/problem?id=1753

题号:POJ1753
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it’s black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

imgConsider the following position as an example:

bwbw  
wwww  
bbwb  
bwwb  

Here “b” denotes pieces lying their black side up and “w” denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw  
bwww  
wwwb  
wwwb  

The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

输入描述

The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.

输出描述

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it’s impossible to achieve the goal, then write the word “Impossible” (without quotes).

示例1

输入

bwwb
bbwb
bwwb
bwww

输出

4

示例2

输入

wwbb
wbwb
bwww
bbwb

输出

2

Code

/**
 *    author:  _sudo
 *    created: 25.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, -1, 0, 1, 0};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<string> s(5);
    for (int i = 0; i < 4; ++i) {
        cin >> s[i];
    }
    int res = 0x3f3f3f3f;
    for (int i = 0; i < 16; ++i) {
        auto g = s;
        auto Switch = [&](int x, int y) {
            for (int i = 0; i < 5; ++i) {
                int xx = x + dx[i], yy = y + dy[i];
                if (xx >= 0 and xx < 4 and yy >= 0 and yy < 4) {
                    g[xx][yy] = (g[xx][yy] == 'w') ? 'b' : 'w';
                }
            }
        };
        int cnt_1 = 0;
        for (int j = 0; j < 4; ++j) {
            if ((i >> j) & 1) {
                ++cnt_1;
                Switch(0, j);
            }
        }
        for (int j = 1; j < 3; ++j) {
            for (int k = 0; k < 4; ++k) {
                if (g[j - 1][k] == 'w') {
                    ++cnt_1;
                    Switch(j, k);
                }
            }
        }
        bool ok = true;
        for (int j = 0; j < 4; ++j) {
            if (g[3][j] == 'w') {
                ok = false;
                break;
            }
        }
        if (ok) {
            res = min(res, cnt_1);
        }

        g = s;
        int cnt_2 = 0;
        for (int j = 0; j < 4; ++j) {
            if ((i >> j) & 1) {
                ++cnt_2;
                Switch(0, j);
            }
        }
        for (int j = 1; j < 3; ++j) {
            for (int k = 0; k < 4; ++k) {
                if (g[j - 1][k] == 'b') {
                    ++cnt_2;
                    Switch(j, k);
                }
            }
        }
        ok = true;
        for (int j = 0; j < 4; ++j) {
            if (g[3][j] == 'b') {
                ok = false;
                break;
            }
        }
        if (ok) {
            res = min(res, cnt_2);
        }
    }
    cout << res << '\n';
    return 0;
}

可能由于POJ编译器版本老旧,使这份代码多处报CE,不过大意如此。



活动打卡代码 AcWing 95. 费解的开关

BOBLT
3天前
/**
 *    author:  BOBLT
 *    created: 24.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, -1, 0, 1, 0};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt; cin >> tt;
    while (tt--) {
        vector<string> g(6);
        for (int i = 0; i < 5; ++i) {
            cin >> g[i];
        }
        int res = 0x3f3f3f3f;
        for (int i = 0; i < 32; ++i) {
            int cnt = 0;
            auto tv = g;
            function<void(int, int)> Switch = [&](int x, int y) {
                for (int i = 0; i < 5; ++i) {
                    int xx = x + dx[i], yy = y + dy[i];
                    if (xx >= 0 and xx < 5 and yy >= 0 and yy < 5) {
                        tv[xx][yy] ^= 1;
                    }
                }
            };
            for (int j = 0; j < 5; ++j) {
                if ((i >> j) & 1) {
                    Switch(0, j);
                    ++cnt;
                }
            }
            for (int j = 0; j < 4; ++j) {
                for (int k = 0; k < 5; ++k) {
                    if (tv[j][k] == '0') {
                        Switch(j + 1, k);
                        ++cnt;
                    }
                }
            }
            bool ok = true;
            for (int j = 0; j < 5; ++j) {
                if (tv[4][j] == '0') {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                res = min(res, cnt);
            }
        }
        if (res > 6) {
            cout << -1 << '\n';
        } else {
            cout << res << '\n';
        }
    }
    return 0;
}

Flip Game

nowcoder链接:https://ac.nowcoder.com/acm/problem/106350
POJ链接:http://poj.org/problem?id=1753

题号:POJ1753
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it’s black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

imgConsider the following position as an example:

bwbw  
wwww  
bbwb  
bwwb  

Here “b” denotes pieces lying their black side up and “w” denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw  
bwww  
wwwb  
wwwb  

The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

输入描述

The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.

输出描述

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it’s impossible to achieve the goal, then write the word “Impossible” (without quotes).

示例1

输入

bwwb
bbwb
bwwb
bwww

输出

4

示例2

输入

wwbb
wbwb
bwww
bbwb

输出

2

Code

/**
 *    author:  BOBLT
 *    created: 25.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, -1, 0, 1, 0};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<string> s(5);
    for (int i = 0; i < 4; ++i) {
        cin >> s[i];
    }
    int res = 0x3f3f3f3f;
    for (int i = 0; i < 16; ++i) {
        auto g = s;
        auto Switch = [&](int x, int y) {
            for (int i = 0; i < 5; ++i) {
                int xx = x + dx[i], yy = y + dy[i];
                if (xx >= 0 and xx < 4 and yy >= 0 and yy < 4) {
                    g[xx][yy] = (g[xx][yy] == 'w') ? 'b' : 'w';
                }
            }
        };
        int cnt_1 = 0;
        for (int j = 0; j < 4; ++j) {
            if ((i >> j) & 1) {
                ++cnt_1;
                Switch(0, j);
            }
        }
        for (int j = 1; j < 3; ++j) {
            for (int k = 0; k < 4; ++k) {
                if (g[j - 1][k] == 'w') {
                    ++cnt_1;
                    Switch(j, k);
                }
            }
        }
        bool ok = true;
        for (int j = 0; j < 4; ++j) {
            if (g[3][j] == 'w') {
                ok = false;
                break;
            }
        }
        if (ok) {
            res = min(res, cnt_1);
        }

        g = s;
        int cnt_2 = 0;
        for (int j = 0; j < 4; ++j) {
            if ((i >> j) & 1) {
                ++cnt_2;
                Switch(0, j);
            }
        }
        for (int j = 1; j < 3; ++j) {
            for (int k = 0; k < 4; ++k) {
                if (g[j - 1][k] == 'b') {
                    ++cnt_2;
                    Switch(j, k);
                }
            }
        }
        ok = true;
        for (int j = 0; j < 4; ++j) {
            if (g[3][j] == 'b') {
                ok = false;
                break;
            }
        }
        if (ok) {
            res = min(res, cnt_2);
        }
    }
    cout << res << '\n';
    return 0;
}

可能由于POJ编译器版本老旧,使这份代码多处报CE,不过大意如此。



活动打卡代码 AcWing 717. 简单斐波那契

BOBLT
4天前
/**
 *    author:  BOBLT
 *    created: 24.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n; cin >> n;
    int a = 0, b = 1;
    for (int i = 1; i <= n; ++i) {
        cout << a << " ";
        int t = b; b += a; a = t;
    }
    return 0;
}



BOBLT
4天前
/**
 *    author:  BOBLT
 *    created: 24.11.2021
**/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n; cin >> n;
    vector<int> v;
    for (int i = 1; i <= n; ++i) {
        v.emplace_back(i);
    }
    do {
        for (auto &i : v) {
            cout << i << " ";
        }
        cout << '\n';
    } while (next_permutation(v.begin(), v.end()));
    return 0;
}