关闭

srm 553

181人阅读 评论(0) 收藏 举报
分类:

欢迎点此阅读QvQ

250


Solution

1分别替换为0,1,2来判断答案是否变化即可。简单粗暴

Code

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
LL gao(vector<int> a) {
    stack<LL> S;
    int n = a.size();
    for (int i = 0; i < 100; ++i)   S.push(0);
    for (int i = 0; i < n; ++i) {
        if (!a[i]) {
            LL x = S.top();
            S.pop();
            LL y = S.top();
            S.pop();
            S.push(x + y);
        }
        else S.push(a[i]);
    }
    return S.top();
}
struct Suminator {
    int findMissing(vector <int> a, int S) {
        int n = a.size();
        int p;
        for (int i = 0; i < n; ++i) 
            if (a[i] == -1) p = i;
        a[p] = 0;
        LL t = gao(a);
        if (t == S) return 0;
        a[p] = 1;
        LL u = gao(a);
        a[p] = 2;
        LL v = gao(a);
        if (u == v) return -1;
        if (u <= S) return S - u + 1;
        return -1;
    }
};

500


Description

求将地图染成2个凸连通块的方案数

Solution

这是道比较有趣的题,首先发现,如果有一段连续的格子被染成同一颜色,在以后的行上,被同色染的格子数一定非递增或非递减,且会确定以后行的状态。于是可以dp,大概就是dp到第i行连续j个格子被染成同一颜色,当前状态是非递增还是非递减还是尚未确定。

Code

#include <bits/stdc++.h>//dp
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
const int N = 55, M = 1e9 + 7;
int n, m;
int dp[N][N][3][2][2];
bool bw[N][N], wb[N][N];
int f(int x, int y, int dir, int ok1, int ok2) {
    if (x == n) {
        if (y == m && dir == 2) return 0;
        return ok1 + ok2;
    }
    int &t = dp[x][y][dir][ok1][ok2];
    if (~t) return t;
    t = 0;
    for (int num = 0; num <= m; ++num) {
        if (dir == 0 && num < y || dir == 1 && num > y) continue;
        int ndir = dir;
        if (dir == 2 && num < y)    ndir = 1;
        if (dir == 2 && num > y)    ndir = 0;
        if (x == 0) ndir = 2;
        if (y == m && num == 0) continue;
        int t1 = ok1, t2 = ok2;
        if (!bw[x][num])    t1 = 0;
        if (!wb[x][num])    t2 = 0;
        (t += f(x + 1, num, ndir, t1, t2)) %= M;
    }
    return t;
}
struct TwoConvexShapes {
    int countWays(vector <string> grid) {
        n = grid.size(), m = grid[0].size();
        memset(dp, -1, sizeof(dp));
        for (int i = 0; i < n; ++i)
            for (int j = 0; j <= m; ++j) {
                bw[i][j] = 1;
                for (int k = 0; k < j; ++k)
                    if (grid[i][k] == 'W')  bw[i][j] = 0;
                for (int k = j; k < m; ++k)
                    if (grid[i][k] == 'B')  bw[i][j] = 0;
                wb[i][j] = 1;
                for (int k = 0; k < j; ++k)
                    if (grid[i][k] == 'B')  wb[i][j] = 0;
                for (int k = j; k < m; ++k)
                    if (grid[i][k] == 'W')  wb[i][j] = 0;
            }
        int ans = 0;
        (ans += f(0, 0, 2, 1, 1)) %= M;
        return ans;

    }
};
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:45126次
    • 积分:1346
    • 等级:
    • 排名:千里之外
    • 原创:86篇
    • 转载:0篇
    • 译文:0篇
    • 评论:17条
    最新评论