首页 > 其他分享 >2023.6.20 每日一题

2023.6.20 每日一题

时间:2023-06-20 17:35:56浏览次数:40  
标签:20 格子 int 每日 dfs 2023.6 include dp op

原题链接

A: Educational Codeforces Round 115 (Rated for Div. 2) - E

B: Codeforces Round 698 (Div. 2) - C

A. Staircases - 2100

题目大意

给定一个 \(n\times m\) 的网格,每个格子为自由或者锁定,初始时所有格子均为自由的。

定义满足如下条件的路径为楼梯:

  • 起点和终点均为自由的

  • 路径上的所有点均为自由的

  • 满足以下两种结构之一

    1.第二个格子在第一个格子右面,第三个格子在第二个格子下面,第四个格子在第三个格子右面,以此类推……

    2.第二个格子在第一个格子下面,第三个格子在第二个格子右面,第四个格子在第三个格子下面,以此类推……

此外,一个单独的自由格子也被视为一个楼梯。

给定 \(q\) 个询问,每个询问给定整数 \(x、y\),并将格子 \((x,y)\) 颜色翻转。

输出每次操作后网格中不同楼梯的个数。

解题思路

首先我们肯定知道求不同的楼梯个数是通过一个二维的dp来解决。可以使用额外的一维来存储是否被锁定的数据。

因为我们只能向右向下走,那么很明显dp的转移就是左格子和上格子加一。当然因为单独一个自由格也算是一个楼梯,那么我们需要去重处理一下。

最后递归处理修改后的情况即可。

注:这里并不是dfs,只是递归习惯性命名dfs。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;

const int N = 1e3 + 10;
const int MOD = 1e9 + 7;

int n, m, q;
LL dp[N][N][2];
LL res;
bool vis[N][N];

void dfs(int x, int y, int op) {
    if (x > n || x < 0 || y > m || y < 0) return;
    res -= dp[x][y][op];
    dp[x][y][op] = 0;
    if (!vis[x][y]) {
        dp[x][y][op] = dp[x - !op][y - op][!op] + 1;
    }
    res += dp[x][y][op];
    dfs(x + op, y + !op, !op);
}

void solve() {
    cin >> n >> m >> q;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            dp[i][j][0] += dp[i - 1][j][1] + 1;
            dp[i][j][1] += dp[i][j - 1][0] + 1;
            res += dp[i][j][0] + dp[i][j][1];
        }
    }
    res -= n * m;
    while (q--) {
        int x, y;
        cin >> x >> y;
        vis[x][y] = !vis[x][y];
        vis[x][y] ? res++ : res--;
        dfs(x, y, 0);
        dfs(x, y, 1);
        cout << res << endl;
    }
}

signed main() {
    ios;
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

B.Nezzar and Symmetric Array - 1700

题目大意

定义长度为 \(2 \times n\) 的数组 \(a\) ,\(a\) 中的元素不重复,并且对于任意一个下标\(i(1 \leq i \leq 2 \cdot n, i \ne j)\),都能找到一个下标 \(j\),使得 \(a_i = -a_j\) 。

现在给定一个数组 \(d\),其中 \(d_i = \sum_{j=1}^{2n}|a_i -a_j|\),问能不能通过数组 \(d\) 构造出数组 \(a\) 。

解题思路

其实就是数学贪心推式子嘛,看了看觉得洛谷的题解很不错,就不再分析了,直接把代码贴在下面。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;

const int N = 2e5 + 10;
const int MOD = 1e9 + 7;

LL a[N], s[N];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= (n << 1); ++i) {
        cin >> a[i];
    }
    sort(a + 1, a + (n << 1) + 1);
    for (int i = 1; i <= n; ++i) {
        if (a[i << 1] != a[(i << 1) - 1]) {
            cout << "NO" << endl;
            return;
        }
    }
    LL cnt = unique(a + 1, a + (n << 1) + 1) - (a + 1);
    if (cnt != n) {
        cout << "NO" << endl;
        return;
    }
    for (int i = 2; i <= n; ++i) {
        s[i] = s[i - 1] + (a[i] - a[i - 1]) / ((i - 1) << 1);
        if ((a[i] - a[i - 1]) % ((i - 1) << 1) || (a[i] - a[i - 1]) <= 0) {
            cout << "NO" << endl;
            return;
        }
    }
    for (int i = 1; i <= n; ++i) {
        a[1] -= s[i] << 1;
    }
    cout << (a[1] % (n << 1) || a[1] <= 0 ? "NO" : "YES") << endl;
}

signed main() {
    ios;
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }
}

标签:20,格子,int,每日,dfs,2023.6,include,dp,op
From: https://www.cnblogs.com/SanCai-Newbie/p/17494232.html

相关文章

  • matlab2023a中解方程
    1、matlab中解方程的函数是solve2、查看帮助:helpsolvehelpsolve---sym/solve的帮助---sym/solve-EquationsandsystemssolverThisMATLABfunctionsolvestheequationeqnforthevariablevar.语法S=solve(eqn,var)S=solve......
  • 路径计数 6.20西安集训(最短哈密顿回路条数)
     因为是哈密顿回路,所以每个点度数为2假设我们已经考虑了i个点,其中b个B,w个W。若存在x条由{1,2,...n}连向{i+1,...2n},那么{1...n}内部的连边数为(2*i-x)/2而只有不同颜色的点会连边,故(2*i-x)/2<=2*min(w,b)x>=2(w+b)-4min(w,b)=2|w-b|则x>=2max(1,|w-b|).为了求得最短路,我们肯定......
  • AutoCAD2018 完整版安装图文教程、注册激活破解方法
    CAD2018是广大绘图从业者必备的软件,它强大的功能可以绘制出各个行业的完整施工图,准确的尺寸更是有利于施工团队完全按照图纸进行施工操作;但对于很多初学者来说,软件安装的诸多步骤使其望而却步,为此,我特意录制了安装教程,并分享去亲测好用的AutoCAD2018软件下载:【下载方法】选中下载......
  • 2023年新课标全国Ⅱ卷数学真题Word解析版
    前言真题图片相关下载2023年新课标全国Ⅱ卷数学真题版+解析版,提取码请微信联系:wh1979448597.......
  • Origin 2022 下载与图文安装教程(附安装包)
    解压和安装前先关闭杀毒软件(WIN8/10系统还需要关闭自带杀毒软件Windowsdefender),防止误杀激活补丁,导致破解失败本软件适用于Win7以上系统下载安装包地址https://pan.baidu.com/s/1jmK7-X-GrIzHfP3_o2-mPg?pwd=50181.把Origin资源从网盘下载到电脑上面,右键压缩包选择解压到当......
  • 2023年衣物洗护市场行业分析(京东天猫数据分析)
    近年来,受消费者习惯的推动,衣物洗护用品市场不断发展,洗护用品行业的市场规模也不断增长。根据鲸参谋电商数据分析平台的相关数据显示,今年1月份至4月份,天猫平台上衣物洗护相关产品的销量为7300万+,产品销额高达31亿+。*数据源于鲸参谋-行业趋势分析伴随用户需求的多元化,洗护产品也越来......
  • [GDOUCTF 2023]Tea
    [GDOUCTF2023]Tea无壳,64位程序程序执行回显如下:我们要输入8个8字节的数据,经过校验后,即可获得flag。我们看一下输入的数据有点像Tea算法中的128位需要加密的数据格式.IDA分析:通过字符串窗口,我们可以找到字符串"youareright!\n".交叉引用(X键)到相关函数去.字符串所在......
  • [LeetCode] 2090. K Radius Subarray Averages
    Youaregivena 0-indexed array nums of n integers,andaninteger k.The k-radiusaverage forasubarrayof nums centered atsomeindex i withthe radius k istheaverageof all elementsin nums betweentheindices i-k and i+k (i......
  • Adobe_Illustrator_2023_27.6.0新增功能_安装_下载
    AdobeIllustrator2023最新爱国版(简称Ai、一键式安装、永久使用)是一款由Adobe公司推出的矢量绘图软件,被广泛用于平面设计、插画、网页设计、多媒体等领域。该软件拥有高度精确的矢量绘图能力,可以输出各种清晰度的图像,因此备受设计师的喜爱和青睐。2023年5月版(版本27.6)桌面版......
  • 每日一题力扣 1262 https://leetcode.cn/problems/greatest-sum-divisible-by-three/
    、 题解这道题目核心就算是要知道如果x%3=2的话,应该要去拿%3=1的数字,这样子才能满足%3=0贪心sum不够%3的时候,就减去余数为1的或者余数为2的需要注意两个余数为1会变成余数为2的,所以可能减去2个余数为1核心代码如下publicintmaxSumDivThreeOther(int[]nums){​  ......