CF1178F1 Short Colorful Strip 题解

这篇具有很好参考价值的文章主要介绍了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.

CF1178F1 Short Colorful Strip 题解,题解,c++,算法,c语言

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 .

CF1178F1 Short Colorful Strip 题解,题解,c++,算法,c语言

解题思路

有题目描述可知, 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;
}

没看懂?戳我文章来源地址https://www.toymoban.com/news/detail-790489.html

到了这里,关于CF1178F1 Short Colorful Strip 题解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • CodeForces CF1846G 题解

    CodeForces题目链接 洛谷题目链接 标准答案是状压之后,转化成Dijkstra算法跑最短路。我这里提供一个不一样的思路。 主人公得了病,有部分类型的症状。所有类型症状共有 (n) 种,用长为 (n) 的01串表示是否有那种症状。共有 (m) 种药,吃第 (i) 种药需要花费时间 (t_i) ,

    2024年02月14日
    浏览(28)
  • CF961E Tufurama 题解

      给定长度为 (n) 的序列 (a) ,统计二元组 ((i,j)) 的个数,使得该二元组满足 (1 leq i j leq n, a_i geq j, a_j geq i) 。 (n) 在 (2 times 10^{5}) 级别, (a_i) 在 (1 times 10^{9}) 级别。   我们考虑把序列中 (n) 个元素看成 ((i,a_i)) 坐标的点,至于平面直角坐标系中。我们先

    2024年02月08日
    浏览(34)
  • CF1011A Stages 题解

    题目意思: 给你一个长度为 n n n 的字符串 a a a ,在这个字符串中选一个长度为 k k k 的好串(好串标准是啥自己去题目里看吧),问这个好串的最小价值是多少。 思路: 贪心。 首先我们将字符串 a a a 里面的字符进行排序。 因为要最小的价值,所以排好序后的 a a a 的第一个

    2024年02月12日
    浏览(21)
  • CF338D GCD Table 题解

    你有一个长度为 (k) 的数列 (a) , 询问是否存在 (xin[1,n]~~~yin[1,m]) 使得 (forall i~~~ gcd(x,y+i-1)=a_i) 。 我们转换一下可以得到: [forall i ~~left{ begin{matrix}xequiv 0pmod{a_i} \\\\y+i-1equiv 0pmod{a_i}end{matrix}right.] 前面一个 (x) 很好解决,直接 最大公倍数 。 (y) 可以转化一下:

    2024年02月07日
    浏览(27)
  • CF1145G AI Takeover 题解

    人工智能取得了进展。在这一题中,我们考虑的是石头剪刀布游戏。 然而,比赛的前一天晚上有一群人把机器人弄坏了,于是使用一个程序替代。 您需要找到一个策略,使您能够获胜。祝你好运! 为了方便,石头剪刀布分别用三个字符表示: R , P , S 。 本题有 6 个测试点,

    2024年03月26日
    浏览(33)
  • CF963B Destruction of a Tree 题解

      洛谷题目链接   这里提供一个较为朴素的 DP 想法。   给定一棵树,节点个数不超过 (2 times 10^5) ,每次可以删掉度数为偶数的点。问最后能不能删完;能删完给出删除方案。   首先可以随便选一个点作为根。   其次,我们考虑在一棵子树的删除情况,我们令

    2024年02月08日
    浏览(27)
  • CF1881F Minimum Maximum Distance 题解 贪心+DFS

    You have a tree with n n n vertices, some of which are marked. A tree is a connected undirected graph without cycles. Let f i f_i f i ​ denote the maximum distance from vertex i i i to any of the marked vertices. Your task is to find the minimum value of f i f_i f i ​ among all vertices. For example, in the tree shown in the example, vertices 2 2 2 , 6 6

    2024年02月22日
    浏览(25)
  • R语言【cli】——ansi_strip():抹除字符串中所有的ANSI标记

    Package  cli  version 3.6.0 输入可以是cli_ansi_string类,这也从结果中删除。 参数【string】 :输入字符串。 参数【sgr】 :是否移除SGR(样式化)控制序列。 参数【csi】 :非sgr控制序列是否移除。 参数【link】 :是否移除ANSI超链接。 清理干净后的字符串。注意,ansi_strip()总是放弃

    2024年01月21日
    浏览(25)
  • 3易懂AI深度学习算法:长短期记忆网络(Long Short-Term Memory, LSTM)生成对抗网络 优化算法进化算法

    继续写:https://blog.csdn.net/chenhao0568/article/details/134920391?spm=1001.2014.3001.5502 1.https://blog.csdn.net/chenhao0568/article/details/134931993?spm=1001.2014.3001.5502 2.https://blog.csdn.net/chenhao0568/article/details/134932800?spm=1001.2014.3001.5502 长短期记忆网络(LSTM)是一种特殊的循环神经网络(RNN),主要用于处

    2024年02月04日
    浏览(34)
  • AGC004B Colorful Slimes

    题目链接:Colorful Slimes 思路:挺神奇的$dp$ 一个比较显然的结论:最小值的方案中第$2$种操作最多用$n-1$次 证明大概就是一个数用$n-1$次一定会变成另一个数 下面说说$dp$的思路: $dp[i][j]$表示能用最多$j$次第$2$种操作能变成$a_i$的最小值 假设$a_k$所有可以用最多$j$次第$2$种操作

    2024年02月08日
    浏览(20)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包