[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,一经查实,立即删除!

相关文章

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

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

[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;如果这个数大于栈顶…

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;…

.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…

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

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

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

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

领域驱动设计,让程序员心中有码(五)

1 从搬砖谈领域对象有一个古老的故事&#xff0c;大概是这样的。作者问三个建筑工地上的工人他们在干什么&#xff1f;有一个没精打采的说&#xff0c;我在挖洞&#xff01;而另一一个人却说&#xff0c;我在盖一座房子。还有一个人说&#xff0c;我在建立一座巨大的城市。…

.NET Core实战项目之CMS 第十四章 开发篇-防止跨站请求伪造(XSRF/CSRF)攻击处理...

通过 ASP.NET Core&#xff0c;开发者可轻松配置和管理其应用的安全性。 ASP.NET Core 中包含管理身份验证、授权、数据保护、SSL 强制、应用机密、请求防伪保护及 CORS 管理等等安全方面的处理。 通过这些安全功能&#xff0c;可以生成安全可靠的 ASP.NET Core 应用。而我们这…

模板:左偏树

文章目录解析可以解决的问题定义&#xff1a;左偏树的基本性质基本结论操作合并访问与删除堆顶元素插入元素批量插入删除已知元素所谓左偏树&#xff0c;就是往左偏的树 下面介绍一下它的一个兄弟&#xff1a; 《右偏树》 &#xff08;逃&#xff09; 解析 所谓左偏树&#…

迎开学水题狂欢赛(舞踏会[dp+三叉树],HH去散步[矩阵快速幂],排序[模拟],铁路旅行[线段树])

快速简单记录老师口胡&#xff08;可能就我自己看得懂了吧…&#xff09; 文章目录T1&#xff1a;舞踏会titlesolutioncodeT2&#xff1a;HH去散步titlesolutioncodeT3&#xff1a;排序titlesolutioncodeT4&#xff1a;铁路旅行titlesolutioncodeT1&#xff1a;舞踏会 title …

CSP2021提高组复赛解析

前言 终于出成绩了我可以写博客辣&#xff0c;官方数据还没出就先放洛谷的题目链接了。 正题 T1-廊桥分配 https://www.luogu.com.cn/problem/P7913 题目大意 有m1m_1m1​种一类飞机&#xff0c;m2m_2m2​种二类飞机&#xff0c;每个飞机有一个占用时间的区间。要给两类飞机…

一起开心集训队第一周训练赛2021/3/14

文章目录比赛链接A CodeForces 1481D AB Graph题意&#xff1a;题解&#xff1a;代码&#xff1a;B CodeForces 1481E Sorting Books题意&#xff1a;题解&#xff1a;代码&#xff1a;C CodeForces 1478D Nezzar and Board题意&#xff1a;题解&#xff1a;代码&#xff1a;D …

使用Azure DevOps持续集成GitHub项目

点击蓝字关注我微软的Azure DevOps是一款软件开发管理工具&#xff0c;整合了需求、代码、编译、测试、发布等所有功能于一身。今天我们就来看看如何用Azure DevOps对自己GitHub上的项目做持续集成&#xff0c;并能在GitHub显示最新编译状态。其实在不久之前&#xff0c;Azure …

[BZOJ 3811]玛里苟斯(线性基)尽量理解的题解

文章目录titlesolutioncodetitle 魔法之龙玛里苟斯最近在为加基森拍卖师的削弱而感到伤心&#xff0c;于是他想了一道数学题。 S 是一个可重集合&#xff0c;S{a1,a2,…,an}。 等概率随机取 S 的一个子集 A{ai1,…,aim}。 计算出 A 中所有元素异或和&#xff0c;记为 x, 求 x^…

CF464E The Classic Problem(线段树 最短路)

CF464E The Classic Problem \(\bigstar\texttt{Hint}\)&#xff1a;发现没有什么好的突破口&#xff1f;为什么不想想怎样才能实现题目中 \(2^x\) 的加减法呢&#xff1f; 可见每次加减法&#xff0c;我们要做的是将添加的 \(1\) 和右边的连续的 \(1\) 合并为一整段&#xff0…

C. Longest Simple Cycle

C. Longest Simple Cycle 题意&#xff1a; 有n条链&#xff0c;第i条链上有c[i]个点&#xff0c;a[i]为第i条链的顶点与第i-1条链的连接点&#xff0c;b[i]为第i条链的最后一个点与第i-1条链的连接点。通过上面的方法连接链会产生很多的环&#xff0c;问这些环的最大长度。 …

【CF813F】Bipartite Checking(线段树分治+可删除并查集)

文章目录titlesolutioncodetitle You are given an undirected graph consisting of n vertices. Initially there are no edges in the graph. Also you are given q queries, each query either adds one undirected edge to the graph or removes it. After each query you…