[2.9训练]【CF909C】Python Indentation,【CF909D】Colorful Points,【CF909E】Coprocessor

文章目录

  • T1:Python Indentation
    • 题目
    • 题解
    • code
  • T2:Colorful Points
    • 题目
    • 题解
    • code
  • T3:Coprocessor
    • 题目
    • 题解
    • code

在这里插入图片描述

T1:Python Indentation

题目

题目描述
In Python, code blocks don’t have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation.

We will consider an extremely simplified subset of Python with only two types of statements.

Simple statements are written in a single line, one per line. An example of a simple statement is assignment.

For statements are compound statements: they contain one or several other statements. For statement consists of a header written in a separate line which starts with “for” prefix, and loop body. Loop body is a block of statements indented one level further than the header of the loop. Loop body can contain both types of statements. Loop body can’t be empty.

You are given a sequence of statements without indentation. Find the number of ways in which the statements can be indented to form a valid Python program.

输入格式
The first line contains a single integer N ( 1<=N<=5000) — the number of commands in the program. N lines of the program follow, each line describing a single command. Each command is either “f” (denoting “for statement”) or “s” (“simple statement”). It is guaranteed that the last line is a simple statement.

输出格式
Output one line containing an integer - the number of ways the given sequence of statements can be indented modulo 109+710^{9}+7109+7

题意翻译
CF909C Python的缩进 Python的代码中不需要写begin、end或者大括号去标记开头或结尾。 我们将考虑一种Python非常简化的子集,它的语句只有两种类型。 每行只写一个简单语句,比如赋值。 For语句是一个较复杂的语句,他们可能包含一个或多个其他的语句。 For语句由一个单独的行组成,以“For”前缀和循环体开头。 循环体是一个语句块,比循环头缩进一级。 循环体可以包含这两种类型的语句。循环体不能为空。 给你一个没有缩进的序列,求有多少种方式添加缩进可以形成一个完整的Python代码。 输入格式: 第一行:N 接下来N行每行一个字符f(for语句)或s(简单语句)。 保证最后一行一定是s(简单语句)。 输出格式: 输出方案数,答案对10^9+7取模。

输入输出样例
输入
4
s
f
f
s
输出
1

输入
4
f
s
f
s
输出
2

题解

dp[i][j]dp[i][j]dp[i][j]表示第iii条语句在第jjj层循环

接着该条语句如果是sss能放在jjj层,则后一条不管是什么都能放在1−j1-j1j
如果是fff那么就可以扩展一层,对后面的语句造成影响
因此我们如果是便输入便处理,一定处理的是前一条语句

dp[i][j]dp[i][j]dp[i][j]就有两种情况了
(1):前一条是sss:放在第jjj层循环内的所有方案数都可以放在111j−1j-1j1层,那么就应该将111j−1j-1j1的方案数都加上这个值
(2):前一条是fff:扩展一层,即放在第jjj层循环内的方案数等于放在第j−1j-1j1层循环内的所有方案数

最后就是发现二维数组MLE,再细想iii的状态只跟i−1i-1i1有关,就可以使用滚动数组
在这里插入图片描述

code

#include <cstdio>
#include <iostream>
using namespace std;
#define MAXN 5005
#define LL long long
const int mod = 1e9 + 7;
int n, cnt, f;
char s, pre;
LL result, dp[MAXN], last[MAXN];int main() {scanf ( "%d", &n );dp[1] = 1;for ( int i = 1;i <= n;i ++ ) {cin >> s;if ( pre == 's' ) {for ( int j = cnt;j >= 1;j -- )dp[j] = ( dp[j + 1] + last[j] ) % mod;	}else {++ cnt;for ( int j = cnt;j > 1;j -- )dp[j] = last[j - 1];}for ( int j = 1;j <= cnt;j ++ ) {last[j] = dp[j];dp[j] = 0;}pre = s;}for ( int i = 1;i <= cnt;i ++ )result = ( result + last[i] ) % mod;printf ( "%lld", result );return 0;
} 

T2:Colorful Points

题目

题目描述
You are given a set of points on a straight line. Each point has a color assigned to it. For point a a , its neighbors are the points which don’t have any other points between them and a a . Each point has at most two neighbors - one from the left and one from the right.

You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can’t perform it.

How many operations will you need to perform until the next operation does not have any points to delete?

输入格式
Input contains a single string of lowercase English letters ‘a’-‘z’. The letters give the points’ colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.

The number of the points is between 1 and 10610^{6}106
输出格式
Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete.

题意翻译
你将得到一些在同一条线上的点,每一个点都有一种颜色。对于点a,他的邻居是他左边最靠近他的点和他左边最靠近他的点。(每个点最多有两个邻居,左边的和右边的) 你要对这些点做一些操作:每次同时删除颜色与邻居不同的点 。 解词:
同时:不会出现本来a点不用删,但删除a的邻居后,a需要删除,举个例子:
一开始:aabb 第一次后:ab(第二个a和第一个b删掉) 最后: (没了) 现在问你你要执行几次操作后,这些点无法再进行删除。(相当于几次后这些点都删完了或颜色都一样)

输入输出样例
输入
aabb
输出
2

输入
aabcaa
输出
1
说明/提示
In the first test case, the first operation will delete two middle points and leave points “ab”, which will be deleted with the second operation. There will be no points left to apply the third operation to.

In the second test case, the first operation will delete the four points in the middle, leaving points “aa”. None of them have neighbors of other colors, so the second operation can’t be applied.

题解

其实就是一道模拟题
既然只有不相同的相邻字母才会一起消失,
我们就容易联想到强连通分量里面的缩点的感觉
在这里,我们把连续相同的一段字母锁在一起
然后原数组就被我们变成了两两之间不相同的
每一个区域都要跟左右抵消−2-22,首尾特殊的−1-11
然后再重新锁字母就可以了
在这里插入图片描述

code

#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 1000005
struct node {char c;int num;node () {}node ( char C, int Num ) {c = C;num = Num;}
}f[MAXN];
char s[MAXN];
int tot, result;void press () {int len = tot;tot = 0;for ( int i = 1;i <= len;i ++ )if ( f[i].num > 0 ) {//后面的-1/2可能减成负的if ( f[i].c == f[tot].c )f[tot].num += f[i].num;elsef[++ tot] = f[i];}
} int main() {scanf ( "%s", s );int len = strlen ( s );for ( int i = 1;i <= len;i ++ ) {f[i].c = s[i - 1];f[i].num = 1;}tot = len;press ();while ( tot > 1 ) {result ++;for ( int i = 2;i < tot;i ++ )f[i].num -= 2;f[1].num --;f[tot].num --;press ();}printf ( "%d", result );return 0;
}

T3:Coprocessor

题目

题目描述
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed.

Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.

Find the minimal number of coprocessor calls which are necessary to execute the given program.

输入格式
The first line contains two space-separated integers N ( 1<=N<=1051<=N<=10^51<=N<=105 ) — the total number of tasks given, and M ( 0<=M<=1050<=M<=10^50<=M<=105 ) — the total number of dependencies between tasks.

The next line contains N space-separated integers . If Ei=0 , task i can only be executed on the main processor, otherwise it can only be executed on the coprocessor.

The next M lines describe the dependencies between tasks. Each line contains two space-separated integers T1 and T2 and means that task T1depends on task T2 (T1≠T2). Tasks are indexed from 0 to N−1 . All M pairs (T1,T2)are distinct. It is guaranteed that there are no circular dependencies between tasks.

输出格式
Output one line containing an integer — the minimal number of coprocessor calls necessary to execute the program.

题意翻译
给你一堆任务,有些要用主处理器处理,有些要用副处理器处理,副处理器可以一次处理很多个任务,一个任务能被执行的条件为前继任务已经被执行过了或者前继任务和自己同时被放进副处理器处理,现在给你这些前继任务的关系和每个任务处理要用的处理器,求副处理器最少运行了几次,保证关系是一张有向无环图

输入输出样例
输入
4 3
0 1 0 1
0 1
1 2
2 3
输出
2

输入
4 3
1 1 1 0
0 1
0 2
3 0
输出
1
说明/提示
In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor.

In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor.

题解

在这里插入图片描述
都想得到的贪心就是先把所有能再主机上做的一次性全都做了,这样在辅机上的才能尽可能地一锅端
这里因为可以前继任务和自己同时被放进副处理器处理就把我之前的纯bfs拍死了

这种要前面所有任务都完成才能进行下一步的明显就是拓扑排序的模型
将主机和辅机分开做,先把主机端完,再康康辅机里面是否有任务再端辅机

最后要保证每个任务都被处理了就行

code

#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define MAXN 100005
queue < int > Qmain, Qco;
vector < int > G[MAXN];
int n, m, T1, T2, result;
bool vis[MAXN];
int E[MAXN], d[MAXN];int main() {scanf ( "%d %d", &n, &m );for ( int i = 0;i < n;i ++ )scanf ( "%d", &E[i] );for ( int i = 1;i <= m;i ++ ) {scanf ( "%d %d", &T1, &T2 );G[T2].push_back ( T1 );d[T1] ++;}for ( int i = 0;i < n;i ++ )if ( ! d[i] )if ( E[i] )Qco.push ( i );elseQmain.push ( i );while ( ( ! Qmain.empty() ) || ( ! Qco.empty() ) ) {while ( ! Qmain.empty() ) {int t = Qmain.front();Qmain.pop();vis[t] = 1;for ( int i = 0;i < G[t].size();i ++ ) {int v = G[t][i];if ( vis[v] ) continue;d[v] --;if ( d[v] )continue;if ( E[v] )Qco.push ( v );elseQmain.push ( v );}}if ( ! Qco.empty() ) {result ++;while ( ! Qco.empty() ) {int t = Qco.front();Qco.pop();vis[t] = 1;for ( int i = 0;i < G[t].size();i ++ ) {int v = G[t][i];if ( vis[v] ) continue;d[v] --;if ( d[v] )continue;if ( E[v] )Qco.push ( v );elseQmain.push ( v );}}}}printf ( "%d", result );return 0;
}

感觉今天的题解怎么这么水了

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

模板:2-SAT问题

文章目录前言实现代码所谓2-SAT&#xff0c;就是解决两个SAT的问题 &#xff08;逃&#xff09; 前言 SAT 是适定性&#xff08;Satisfiability&#xff09;问题的简称。一般形式为 k - 适定性问题&#xff0c;简称 k-SAT。而当 k>2 时该问题为 NP 完全的。所以我们只研究 …

CF1481F-AB Tree【构造,背包】

正题 https://www.luogu.com.cn/problem/CF1481F 题目大意 给出nnn个点的一棵树&#xff0c;在每个节点上填a/ba/ba/b&#xff0c;要求恰好有mmm个aaa。要求每个节点到根路径上的字符串种类最少&#xff0c;输出方案。 1≤m≤n≤1051\leq m\leq n\leq 10^51≤m≤n≤105 解题思…

Docker最全教程之使用Tencent Hub来完成CI(十)

本周更新两篇&#xff0c;保证不太监&#xff01;在本系列教程中&#xff0c;笔者希望将必要的知识点围绕理论、流程&#xff08;工作流程&#xff09;、方法、实践来进行讲解&#xff0c;而不是单纯的为讲解知识点而进行讲解。也就是说&#xff0c;笔者希望能够让大家将理论、…

P4169 [Violet]天使玩偶/SJY摆棋子(CDQ分治+欧几里得距离)

P4169 [Violet]天使玩偶/SJY摆棋子(CDQ分治欧几里得距离) 记得上一次欧几里得距离的转化是CF1093G Multidimensional Queries&#xff0c;我们使用了点对在四种方向分别考虑并用 \(\max\) 合并的方法解决&#xff0c;现在使用一种类似的方法。 \(\bigstar\texttt{Trick}\)&…

[2.7]【CF933A】A Twisty Movement【CF926B】Add Points【CF917A】The Monster【CF919E】Congruence Equation

文章目录T1&#xff1a;A Twisty Movement题目题解codeT2&#xff1a;Add Points题目题解codeT3&#xff1a;The Monster题目题解codeT4&#xff1a;Congruence Equation题目题解codeT1&#xff1a;A Twisty Movement 题目 题目 题解 因为aia_iai​1/21/21/2&#xff0c;于…

LIS最长上升子序列

LIS算是比较经典的问题&#xff0c;常用的是O(n^2)的方法 for(int i1;i<n;i){dp[i]1;for(int j1;j<i;j){if(a[j]<a[i])dp[i]max(dp[i],dp[j]1);}mxmax(mx,dp[i]);}我们这里优化成O(nlogn) 我们模拟一个栈stack&#xff0c;每读入一个数&#xff0c;如果这个数大于栈顶…

AT5160-[AGC037C]Numbers on a Circle【贪心,堆】

正题 题目链接:https://www.luogu.com.cn/problem/AT5160 题目大意 给出两个长度为nnn的环序列aaa和bbb&#xff0c;每次你可以让aaa中的一个数变为它和相邻两个的和。 求最少的步数将aaa变为bbb。 1≤n≤105,1≤ai,bi≤1091\leq n\leq 10^5,1\leq a_i,b_i\leq 10^91≤n≤10…

YBTOJ洛谷P3209:平面图判定(2-SAT)

文章目录解析代码传送门解析 关键性质是一个定理&#xff1a;若m>3*n-6&#xff0c;必然不存在合法的平面图 这谁知道啊 不过这题应该往也许图过于稠密时必然无解这方面想 所以我们只需要考虑m、n同阶的情况就行了 这个时候我们直接暴力判断跑2-SAT就行了 代码 #include&…

线性代数 - 矩阵对角化

矩阵对角化 今天听 \(\texttt{m}\color{red}\texttt{yee}\) 嘴的&#xff0c;赶紧来补个学习笔记。 我们有点时候需要计算一个较小矩阵的 \(n\) 次幂&#xff0c;但直接求幂非常不方便&#xff0c;这是会考虑矩阵对角化&#xff0c;将 \(M\) 改写为 \(\mathcal{PDP^{-1}}\)&…

EF Core 数据库 Provider 一览

当 EF Core 1.x 系列和 2.0 版本之间经过重大的重写时&#xff0c;所有 EF Core 数据库 Provider 都受到重创。从那时起&#xff0c;各种私人和商业开发团队一直在努力填补这个空白。正文当 EF Core 1.x 系列和 2.0 版本之间经过重大的重写时&#xff0c;所有 EF Core 数据库 P…

[3.3训练赛]One-Dimensional(矩阵快速幂),Freda的迷宫(无向图强连通分量+并查集),一道防AK好题

文章目录T1:One-DimensionaltitlesolutioncodeT2:【NOIP模拟赛】Freda的迷宫titlesolutioncodeT3:【NOIP模拟赛】一道防AK好题titlesolutioncode确实没想到自己写文章能隔这么久&#xff0c;鸽王预警 T1:One-Dimensional title 考虑一个含有 N 个细胞的一维细胞自动机。细胞…

牛客网专题 概率dp

文章目录概念&#xff1a;例题引入&#xff1a;解答&#xff1a;Happy Running NC15532题意&#xff1a;题解&#xff1a;代码&#xff1a;poj2096 NC106693 Collecting Bugs题意&#xff1a;题解&#xff1a;代码&#xff1a;NC210477 带富翁题意&#xff1a;题解&#xff1a;…

模板:线性基

文章目录解析实现删除所谓线性基&#xff0c;就是线性的基 &#xff08;逃&#xff09; 解析 何为线性基&#xff1f; 定义几何BBB为集合SSS的线性基,当且仅当: .S任意子集异或和可以得到的结果&#xff0c;用B的子集都也可以得到&#xff0c;且B时所有这样的集合中元素最少的…

CF1242C-Sum Balance【状压dp】

正题 题目链接:https://www.luogu.com.cn/problem/CF1242C 题目大意 给出kkk个集合&#xff0c;现在从每个集合中取出一个数再把这些数放进每个集合里各一个&#xff0c;求能否使得所有集合的和相等&#xff0c;求方案。 保证所有集合中的出现过的数字都互不相同。 1≤k≤15…

连续段计数问题小记

给定一个长度为 \(n\) 的一个排列&#xff0c;如果区间 \([l,r]\) 之间的数是连续的&#xff0c;那么我们称这个区间时一个连续段。 比如 \([1,3,2,5,4]\) 中的连续段有&#xff1a;\([1,1],[1,3],[1,5],[2,2],[2,3],[2,5],[3,3],[4,4],[4,5],[5,5]\)。 这些连续段有一个共同的…

.NET Core 3.0 特性初探:C# 8、WPF、Windows Forms、EF Core

.NET Core 的下一个主要版本最近进入了预览阶段&#xff0c;.NET Core 3.0 将支持使用 Windows Presentation Foundation &#xff08;WPF&#xff09;、Windows Forms&#xff08;WinForms&#xff09;、Entity Framework &#xff08;EF&#xff09;、Blazor、 C# 8 和.NET S…

任意长度循环卷积单位根反演 学习笔记

今天听 \(\texttt{m}\color{red}{\texttt{yee}}\) 嘴的&#xff0c;赶紧来补个学习笔记。 PS&#xff1a;FFT 本质是长度为 \(2^k\) 的循环卷积。 单位根反演 反演本质&#xff1a; \[\frac1n\sum_{i0}^{n-1}\omega_{n}^{ai}[n|a] \]证明&#xff1a; 如果 \(n|i\)&#xff0c;…

YBTOJ洛谷P4074:糖果公园(树上莫队)

文章目录解析update:代码所谓树上莫队&#xff0c;就是在树上的莫队 &#xff08;逃&#xff09; 传送门 解析 似乎就是树上的这道题 考虑如何转化为序列问题呢? 考虑dfs序 但是又一个问题。。。 似乎这条链的dfs序不连续啊 树剖一下就好啦 考虑更阳间的方法 求出这棵树的欧…

ARC115D-Odd Degree【dp,欧拉回路】

正题 题目链接:https://atcoder.jp/contests/arc115/tasks/arc115_d 题目大意 给出nnn个点mmm条边的一张无向图&#xff0c;对于每个k∈[1,n]k\in[1,n]k∈[1,n] 求恰好有kkk个奇数入度点的生成子图数量。 1≤n,m≤50001\leq n,m\leq 50001≤n,m≤5000 解题思路 考虑有kkk个奇…

【用梨泰院class中的财阀世家带你洞悉替罪羊树】Scapegoat Tree原理,模板,例题

我想写在前面&#xff0c;本文财阀世家全是虚构&#xff0c;没有诋毁之意&#xff0c;如有雷同&#xff0c;纯属巧合 红色预警&#xff01;&#xff01;&#xff01;红色预警 文章目录Scapegoat Tree概念模板变量声明Bad函数判断是否需要重构理解模板rebuild重构理解模板inser…