CF1178F1 Short Colorful Strip 题解

Short Colorful Strip

传送门

题面翻译

题目描述

这是F题的第一个子任务。F1和F2的区别仅在对于m和时间的限制上

有n+1种颜色标号从0到n,我们有一条全部染成颜色0的长为m的纸带。

Alice拿着刷子通过以下的过程来给纸带染色:

我们按照从1到n的顺序进行染色,进行每次染色时,我们选取一个区间[ai,bi],0<=ai<bi<=m,并且这个区间内必定是单种颜色。

染色到最后,纸带上有各种颜色除了颜色0.给出纸带最终的状态,问有多少种不同的染色方案能到达最终状态。输出时结果模998244353。

输入格式

第一行有两个整数n和m,1<=n<=500,并且保证m=n。n代表除了颜色0有多少种颜色,m代表纸带的长度。

第二行有m个用空格分隔的整数c1,c2,…,cm,(1<=ci<=n),代表完成染色后纸带的最终状态。保证最终状态中能在纸带上找到从1到n所有的颜色。

注意,这个子任务保证了n=m,那么就是说最终状态是1到n的一个排列。

输出格式

输入一个单独的整数,即Alice可行的染色方案数模998244353.

题目描述

This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of m m m and the time limit. You need to solve both subtasks in order to hack this one.

There are n + 1 n+1 n+1 distinct colours in the universe, numbered 0 0 0 through n n n . There is a strip of paper m m m centimetres long initially painted with colour 0 0 0 .

Alice took a brush and painted the strip using the following process. For each i i i from 1 1 1 to n n n , in this order, she picks two integers 0 ≤ a i < b i ≤ m 0 \leq a_i < b_i \leq m 0ai<bim , such that the segment [ a i , b i ] [a_i, b_i] [ai,bi] is currently painted with a single colour, and repaints it with colour i i i .

Alice chose the segments in such a way that each centimetre is now painted in some colour other than 0 0 0 . Formally, the segment [ i − 1 , i ] [i-1, i] [i1,i] is painted with colour c i c_i ci ( c i ≠ 0 c_i \neq 0 ci=0 ). Every colour other than 0 0 0 is visible on the strip.

Count the number of different pairs of sequences { a i } i = 1 n \{a_i\}_{i=1}^n {ai}i=1n , { b i } i = 1 n \{b_i\}_{i=1}^n {bi}i=1n that result in this configuration.

Since this number may be large, output it modulo 998244353 998244353 998244353 .

输入格式

The first line contains a two integers n n n , m m m ( 1 ≤ n ≤ 500 1 \leq n \leq 500 1n500 , n = m n = m n=m ) — the number of colours excluding the colour $ 0 $ and the length of the paper, respectively.

The second line contains m m m space separated integers c 1 , c 2 , … , c m c_1, c_2, \ldots, c_m c1,c2,,cm ( 1 ≤ c i ≤ n 1 \leq c_i \leq n 1cin ) — the colour visible on the segment [ i − 1 , i ] [i-1, i] [i1,i] after the process ends. It is guaranteed that for all j j j between 1 1 1 and n n n there is an index k k k such that c k = j c_k = j ck=j .

Note that since in this subtask n = m n = m n=m , this means that c c c is a permutation of integers 1 1 1 through n n n .

输出格式

Output a single integer — the number of ways Alice can perform the painting, modulo $ 998244353 $ .

样例 #1

样例输入 #1

3 3
1 2 3

样例输出 #1

5

样例 #2

样例输入 #2

7 7
4 5 1 6 2 3 7

样例输出 #2

165

提示

In the first example, there are 5 5 5 ways, all depicted in the figure below. Here, 0 0 0 is white, 1 1 1 is red, 2 2 2 is green and 3 3 3 is blue.

Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour 2 2 2 .

解题思路

有题目描述可知, n = m n=m n=m,所以每个颜色只能涂一次。又可知,颜色要从编号小的到编号大的涂,所以可以用搜索算法,每次找出带子上颜色编号最小的位置作为基准点,算出 d f s ( l , m i n i d − 1 ) dfs(l,minid-1) dfs(l,minid1) d f s ( m i n i d + 1 , r ) dfs(minid+1,r) dfs(minid+1,r)。而题目要求求涂色方案数,所以可以用乘法原理,算出 d f s ( l , m i n i d − 1 ) = d f s ( l , x − 1 ) × d f s ( x , m i n i d − 1 ) dfs(l,minid-1)=dfs(l,x-1) \times dfs(x,minid-1) dfs(l,minid1)=dfs(l,x1)×dfs(x,minid1) d f s ( m i d i d + 1 , r ) = d f s ( m i n i d , x ) × d f s ( x + 1 , r ) dfs(midid+1,r)=dfs(minid,x) \times dfs(x+1,r) dfs(midid+1,r)=dfs(minid,x)×dfs(x+1,r),因而 d f s ( l , r ) = d f s ( l , m i n i d − 1 ) × d f s ( m i d i d + 1 , r ) = d f s ( m i n i d , x ) × d f s ( x + 1 , r ) dfs(l,r)=dfs(l,minid-1) \times dfs(midid+1,r)=dfs(minid,x) \times dfs(x+1,r) dfs(l,r)=dfs(l,minid1)×dfs(midid+1,r)=dfs(minid,x)×dfs(x+1,r)

为了避免做重复涂色操作,所以要记忆化,用 t o n g l , r tong_{l,r} tongl,r
记录 l l l~ r r r 区间的涂色方案数。

AC Code

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
	#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
	#include <ccomplex>
	#include <cfenv>
	#include <cinttypes>
	#include <cstdalign>
	#include <cstdbool>
	#include <cstdint>
	#include <ctgmath>
	#include <cwchar>
	#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
	#include <array>
	#include <atomic>
	#include <chrono>
	#include <condition_variable>
	#include <forward_list>
	#include <future>
	#include <initializer_list>
	#include <mutex>
	#include <random>
	#include <ratio>
	#include <regex>
	#include <scoped_allocator>
	#include <system_error>
	#include <thread>
	#include <tuple>
	#include <typeindex>
	#include <type_traits>
	#include <unordered_map>
	#include <unordered_set>
#endif
using namespace std;
#define int long long
const int Mod = 998244353;
const int Maxn = 500 + 5;
int n, m, a[Maxn];
int tong[Maxn][Maxn];//区块l~r的方案数
inline int dfs(int l, int r) {
	if (tong[l][r]) {
		return tong[l][r];
	} else if (l >= r) {
		tong[l][r] = 1;
	} else {
		int minid = l, tmp1 = 0, tmp2 = 0;
		for (int i = l; i <= r; i++) {//求当前区块编号最小颜色
			if (a[i] < a[minid]) {
				minid = i;
			}
		}
		for (int i = l; i <= minid; i++) {
			tmp1 = (tmp1 + dfs(l, i - 1) * dfs(i, minid - 1) % Mod) % Mod;
		}
		for (int i = minid; i <= r; i++) {
			tmp2 = (tmp2 + dfs(minid + 1, i) * dfs(i + 1, r) % Mod) % Mod;
		}
		tong[l][r] = tmp1 * tmp2 % Mod;
	}
	return tong[l][r];
}
inline void work() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(1, m);
	cout << tong[1][m] % Mod << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	work();
	return 0;
}

没看懂?戳我

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/320880.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

美国初创公司Rabbit推出口袋AI设备R1;吴恩达课程:使用LangChain.js构建强大的JavaScript应用

&#x1f989; AI新闻 &#x1f680; 美国初创公司Rabbit推出口袋AI设备R1&#xff0c;短时间内被抢购一空 摘要&#xff1a;美国初创公司Rabbit在CES 2024上发布了口袋AI设备R1&#xff0c;这款设备在一天内被抢购一空&#xff0c;售价为199美元。R1具有小巧玲珑的触屏、摄像…

【线性表的基本操作实现及其应用 】

线性表的基本操作实现及其应用 1.实验目的 ⑴ 熟练掌握线性表的基本操作在两种存储结构上的实现&#xff0c;其中以熟悉各种链表的操作为重点。 ⑵ 巩固高级语言程序设计方法与技术&#xff0c;会用线性链表解决简单的实际问题。 2.实验原理与要求 ⑴ 按照数据结构实验任务书&…

【笔记】书生·浦语大模型实战营——第四课(XTuner 大模型单卡低成本微调实战)

【参考&#xff1a;tutorial/xtuner/README.md at main InternLM/tutorial】 【参考&#xff1a;(4)XTuner 大模型单卡低成本微调实战_哔哩哔哩_bilibili-【OpenMMLab】】 总结 学到了 linux系统中 tmux 的使用 了解了 XTuner 大模型微调框架的使用 pth格式参数转Hugging …

【量化交易故事】小明开启了量化创业之旅-01

故事开始于2023年的春天&#xff0c;小明是一位对金融市场充满热情的IT工程师。在经历了数次基于主观判断和个人情绪进行投资却收获平平后&#xff0c;他意识到传统交易方式中的人为因素难以避免&#xff0c;而这往往成为影响投资决策稳定性和准确性的关键障碍。在一次偶然的机…

工作压力测试

每个职场人都会遇到工作压力&#xff0c;在企业人力资源管理的角度来看&#xff0c;没有工作压力是人力资源的低效&#xff0c;适当的工作压力可以促使员工不断进取&#xff0c;然而每个人的抗压能力是不同的&#xff0c;同样的工作量和工作难度&#xff0c;不同的人在面对相同…

【Java语言基础②】Java基本语法——Java程序基本格式,注释,标识符,常量

通过前面的学习&#xff0c;大家对Java语言有了一个基础认识&#xff0c;但现在还无法使用Java语言编写程序&#xff0c;要熟练使用Java语言编写程序&#xff0c;必须充分掌握Java语言的基础知识。今天咱们就来聊一聊Java的基本语法。 1.java程序的基本格式 Java程序代码必须…

在win11中安装“mingw-w64-gcc-13.2-stable-r40”

在windows系统中&#xff0c;安装完VSCode后&#xff0c;还需要安装mingw&#xff0c;才可以使用C和C编译。 1、从MinGW-w64镜像站点&#xff1a;http://files.1f0.de/mingw&#xff0c;下载“mingw-w64-gcc-13.2-stable-r40”&#xff0c;见下图&#xff1a; 2、将“mingw-w6…

Centos7编译Python3.11源码并安装完成的详细教程

Python3.11的Linux源码&#xff1a; Index of /ftp/python/https://www.python.org/ftp/python/由于Centos7里自带的openssl是1.0版本的&#xff0c;而Centos Stream8和9用的是openssl-1.1.1版本的。 注意&#xff1a;openssl必须是openssl-1.1.1版本的&#xff0c;虽然最高版…

【大厂秘籍】 - Java多线程面试题

Java多线程面试题 友情提示&#xff0c;看完此文&#xff0c;在Java多线程这块&#xff0c;基本上可以吊打面试官了 线程和进程的区别 进程是资源分配的最小单位&#xff0c;线程是CPU调度的最小单位 线程是进程的子集&#xff0c;一个进程可以有很多线程&#xff0c;每条线…

10.9.2 std::function 存储函数对象 Page184

41行&#xff0c;pending只是inc的复制品&#xff0c;所以43&#xff0c;44行&#xff0c;不会改变inc()的值 demo_function2()的运行结果&#xff1a; 59行&#xff0c;pending是inc的引用&#xff0c;所以61,62行将会改变inc()的值

CloudFlare平台下载的WARP一直连不上(warp无法连接)解决办法

遇到问题&#xff1a; 解决办法&#xff1a; 下载一个warp选ip的文件夹&#xff0c;选一下ip就行了。 下载链接如下&#xff1a; https://pan.kejicode.cn/d/Onedrive/WIN%E7%AB%AFwarp%E8%87%AA%E9%80%89IP(%E6%89%8B%E5%8A%A8%2B%E8%87%AA%E5%8A%A8).rar?signRqBdHIMyyhg…

查询和结果处理的Java代码

match_all查询&#xff1a; //查询所有文档 match_all查询Testvoid testMatchAll() throws IOException {// 1.准备RequestSearchRequest request new SearchRequest("hotel");// 2.准备DSLrequest.source().query(QueryBuilders.matchAllQuery());// 3.发送请求Sea…

Apollo之原理和使用讲解

文章目录 1 Apollo1.1 简介1.1.1 背景1.1.2 简介1.1.3 特点 1.2 基础模型1.3 Apollo 四个维度1.3.1 application1.3.2 environment1.3.3 cluster1.3.4 namespace 1.4 本地缓存1.5 客户端设计1.5.1 客服端拉取原理1.5.2 配置更新推送实现 1.6 总体设计1.7 可用性考虑 2 操作使用…

海外媒体宣发:新闻媒体发稿引爆社交媒体的7个诀窍-华媒舍

社交媒体的崛起已经改变了新闻媒体的传播方式。从Facebook到Twitter&#xff0c;从Instagram到LinkedIn&#xff0c;社交媒体平台为新闻媒体提供了一个巨大且潜力无限的受众群体。要在这个竞争激烈的环境中引爆社交媒体&#xff0c;需要一些技巧和诀窍。在本篇文章中&#xff0…

【CSS】保持元素宽高比

保持元素的宽高比&#xff0c;在视频或图片展示类页面是一个重要功能。 本文介绍其常规的实现方法。 实现效果 当浏览器视口发生变化时&#xff0c;元素的尺寸随之变化&#xff0c;且宽高比不变。 代码实现 我们用最简单的元素结构来演示&#xff0c;实现宽高比为4&#xf…

WorkPlus卓越的即时通讯工具,助力企业提升工作效率

在当今快节奏的商业环境中&#xff0c;高效沟通和协作是企业成功的关键。而即时通讯作为实现高效沟通的利器&#xff0c;成为了现代企业不可或缺的一部分。作为一款领先的即时通讯工具&#xff0c;WorkPlus以其卓越的性能和独特的功能&#xff0c;助力企业打造高效沟通和协作的…

初始化数组

一、静态初始化格式&#xff1a; 数据类型[ ] 数组名 new 数据类型[ ]{元素1&#xff0c;元素2&#xff0c;元素3......} 等号后面的new 数据类型可以省略 注意&#xff1a;什么类型的数组只能存放什么类型的数据 直接打印a或b会显示其地址 数组的元素个数&#xff1a;arr…

Android Studio 如何设置中文

Android Studio 是一个为 Adndroid 平台开发程序的集成开发环境&#xff08;IDE&#xff09;。 如何安装中文插件 在 Jetbrains 家族的插件市场上&#xff0c;是能够搜到语言包插件的&#xff0c;正常情况下安装之后只需要重启即可享受中文界面&#xff0c;可AndroidStudio 中…

SOLID 原则

单一功能原则 单一功能原则&#xff08;Single responsibility principle&#xff09;规定每个类都应该有一个单一的功能&#xff0c;并且该功能应该由这个类完全封装起来。所有它的&#xff08;这个类的&#xff09;服务都应该严密的和该功能平行&#xff08;功能平行&#x…

ospf-gre隧道小练习

全网可达,R5路由表没有其他路由器的路由条目 注:每个路由器都添加了自己的环回,如R1就是1.1.1.1 R1可以分别ping通与R2,R3,R4之间的隧道 R1路由表上有所有路由器环回的路由条目 R5路由表上没有其他路由器的路由条目 实现代码: 首先将各个接口IP配好 边上3个路由器:[R6][R7][R…
最新文章