AtCoder abc256全题解(区间合并模板、矩阵快速幂优化dp、线段树……)

本文详细解析了多种算法模板,包括水题、区间合并、图论建模(函数图)、树状数组、矩阵快速幂优化动态规划、线段树等,并给出了相应的代码实现。通过这些模板,可以解决各种复杂问题,如九宫格填数、区间操作和图论求解等。


传送门

本文CSDN

本文juejin

作者:hans774882968以及hans774882968

A

水,略。

B

模拟即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define re_(i,a,b) for(int i = (a);i < (b);++i)
#define dwn(i,a,b) for(int i = (a);i >= (b);--i)

const int N = 5 + 5;

int n, a[N];

void dbg() {
   
   
    puts ("");
}
template<typename T, typename... R>void dbg (const T &f, const R &... r) {
   
   
    cout << f << " ";
    dbg (r...);
}
template<typename Type>inline void read (Type &xx) {
   
   
    Type f = 1;
    char ch;
    xx = 0;
    for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar() ) if (ch == '-') f = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar() ) xx = xx * 10 + ch - '0';
    xx *= f;
}
void read() {
   
   }
template<typename T, typename ...R>void read (T &x, R &...r) {
   
   
    read (x);
    read (r...);
}

int main() {
   
   
    read (n);
    int ans = 0;
    rep (_, 1, n) {
   
   
        int x;
        read (x);
        a[0]++;
        dwn (i, 3, 0) {
   
   
            if (i + x < 4) a[i + x] += a[i];
            else ans += a[i];
            a[i] = 0;
        }
    }
    printf ("%d\n", ans);
    return 0;
}

C-枚举

题意:有一个九宫格,每格填一个正整数,输入3 <= h[0~2] <= 30, 3 <= w[0~2] <= 30分别表示期望的每行和每列的数字的和。求方案数。

只需要枚举4个格子就能确定所有的数了,计算量30^4完全可行。我选择的是(0,0), (0,1), (1,0), (1,1)这4个格子。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define re_(i,a,b) for(int i = (a);i < (b);++i)
#define dwn(i,a,b) for(int i = (a);i >= (b);--i)

const int N = 2e5 + 5;

int n, w[5], h[5];

void dbg() {
   
   
    puts ("");
}
template<typename T, typename... R>void dbg (const T &f, const R &... r) {
   
   
    cout << f << " ";
    dbg (r...);
}
template<typename Type>inline void read (Type &xx) {
   
   
    Type f = 1;
    char ch;
    xx = 0;
    for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar() ) if (ch == '-') f = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar() ) xx = xx * 10 + ch - '0';
    xx *= f;
}
void read() {
   
   }
template<typename T, typename ...R>void read (T &x, R &...r) {
   
   
    read (x);
    read (r...);
}

int main() {
   
   
    n = 3;
    re_ (i, 0, n) read (w[i]);
    re_ (i, 0, n) read (h[i]);
    int ans = 0;
    re_ (i0, 1, min (w[0], h[0]) - 1) {
   
   
        re_ (i1, 1, min (w[1], h[0]) - 1) {
   
   
            re_ (i3, 1, min (w[0], h[1]) - 1) {
   
   
                re_ (i4, 1, min (w[1], h[1]) - 1) {
   
   
                    int i2 = h[0] - i0 - i1, i5 = h[1] - i3 - i4, i6 = w[0] - i0 - i3, i7 = w[1] - i1 - i4;
                    if (i2 > 0 && i5 > 0 && i6 > 0 && i7 > 0) {
   
   
                        int i81 = h[2] - i6 - i7, i82 = w[2] - i2 - i5;
                        if (i81 == i82 && i81 > 0) ++ans;
                    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值