日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/3 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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-j1?j
如果是fff那么就可以擴展一層,對后面的語句造成影響
因此我們如果是便輸入便處理,一定處理的是前一條語句

dp[i][j]dp[i][j]dp[i][j]就有兩種情況了
(1):前一條是sss:放在第jjj層循環內的所有方案數都可以放在111j?1j-1j?1層,那么就應該將111j?1j-1j?1的方案數都加上這個值
(2):前一條是fff:擴展一層,即放在第jjj層循環內的方案數等于放在第j?1j-1j?1層循環內的所有方案數

最后就是發現二維數組MLE,再細想iii的狀態只跟i?1i-1i?1有關,就可以使用滾動數組

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-2?2,首尾特殊的?1-1?1
然后再重新鎖字母就可以了

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

感覺今天的題解怎么這么水了

總結

以上是生活随笔為你收集整理的[2.9训练]【CF909C】Python Indentation,【CF909D】Colorful Points,【CF909E】Coprocessor的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。