Beautiful Sequence

\[Time Limit: 1000 ms\quad Memory Limit: 256 MB
\]

首先我们可以考虑到 \(0\) 只能 和 \(1\) 放在一起、\(3\) 只能和 \(2\) 放在一起,那么我们想办法先把 \(0\) 和 \(3\) 凑出来,最后就剩下 \(1\) 和 \(2\) 了,我们只要把他们放在一起就可以了。

所以我们可以贪心考虑三个 \(string\),分别长成 \(0101...0101\)、\(2323...2323\)、\(1212...1212\) 这样的,那么现在的问题就是把这三个 \(string\) 合并起来,那么完全可以把他们全排列并二进制枚举每个 \(string\) 是否翻转,然后 \(check\) 一遍。

view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T;
int a, b, c, d; string s[4], ss; bool ok(string a, string b, string c) {
int lena = a.size(), lenb = b.size(), lenc = c.size();
int len = lena+lenb+lenc;
for(int i=0; i<7; i++) {
if(i&(1<<(0))) {
reverse(a.begin(), a.end());
}
if(i&(1<<(1))) {
reverse(b.begin(), b.end());
}
if(i&(1<<(2))) {
reverse(c.begin(), c.end());
}
ss = a+b+c;
if(i&(1<<(0))) {
reverse(a.begin(), a.end());
}
if(i&(1<<(1))) {
reverse(b.begin(), b.end());
}
if(i&(1<<(2))) {
reverse(c.begin(), c.end());
} int flag = 1;
for(int i=1; i<len; i++) {
if(abs(ss[i]-ss[i-1])!=1) {
flag = 0;
break;
}
}
if(flag) {
printf("YES\n");
for(int i=0; i<len; i++) {
printf("%c%c", ss[i], i==len-1 ? '\n':' ');
}
return true;
}
}
return false;
} int main() {
scanf("%d%d%d%d", &a, &b, &c, &d);
s[1] = "";
while(a&&b) {
s[1] += "01";
a--, b--;
}
if(a) {
s[1] += "0";
a--;
}
if(b) {
s[1] = "1"+s[1];
b--;
} s[3] = "";
while(c&&d) {
s[3] += "23";
c--, d--;
}
if(c) {
s[3] += "2";
c--;
}
if(d) {
s[3] = "3"+s[3];
d--;
} s[2] = "";
while(b&&c) {
s[2] += "12";
b--, c--;
}
if(b) {
s[2] += "1";
b--;
}
if(c) {
s[2] = "2"+s[2];
c--;
}
if(a||b||c||d) return 0*puts("NO");
do {
if(ok(s[1], s[2], s[3]))
return 0;
} while(next_permutation(s+1, s+1+3));
puts("NO");
return 0;
}

Beautiful Mirrors

\[Time Limit: 2000 ms\quad Memory Limit: 256 MB
\]

首先令 \(dp[i]\) 表示从第 \(i\) 天到结束所需要的期望天数,为了方便,可以假设 \(n+1\) 天为结束位置,那么 \(dp[n+1] = 0\)。

对于 \(1<=i<=n\),有 \(dp[i] = \frac{p_i*dp[i+1] + (100-p_i)*dp[1]}{100}+1\)

然后一直带进去,最后可以发现 \(dp[1]\) 可以表示为

\[ dp[1] = A*dp[1] + B + 1
\]

其中 \(A、B\) 都是具体的数字,那么就得到的 \(dp[1]\) 的值。

view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 998244353;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; int p[maxn]; ll fpow(ll a, ll b) {
ll ans = 1;
while(b) {
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
} int main() {
ll M = fpow(100ll, mod-2);
scanf("%d", &n);
for(int i=1; i<=n; i++) {
scanf("%d", &p[i]);
}
ll b = 0, c = 0;
ll tmpb = 1;
for(int i=1; i<=n; i++) {
b += tmpb*(100ll-p[i])%mod*M%mod;
c += tmpb;
tmpb *= p[i]*M%mod;
b %= mod, c %=mod, tmpb %= mod;
}
b = mod-b+1;
b = (b%mod+mod)%mod;
ll ans = c*fpow(b, mod-2)%mod;
printf("%lld\n", ans);
return 0;
}

Beautiful Bracket Sequence (easy version)

\[Time Limit: 2000 ms\quad Memory Limit: 256 MB
\]

令 \(dp[i][j]\) 表示从 \(i\) 到 \(j\) 区间内,所有情况的括号最深深度之和。

转移的时候令 \(dp[i][i] = 0\),\(dp[i][i+1]\) 为 \(s[i]\) 和 \(s[i+1]\) 能否组成 \(()\)。

对于更大的区间,只需要考虑最左和最右端点就可以。

  1. 如果\(s[i]\) 可以放成 \((\)

    • 如果 \(s[j]\) 可以放成 \((\),\(dp[i][j] += dp[i][j-1]\)
    • 如果 \(s[j]\) 可以放成 \()\),\(dp[i][j] += dp[i+1][j-1] + 2^k\),\(k\) 代表 \([i+1,j-1]\) 这段内 \(?\) 的个数。
  2. 如果 \(s[i]\) 可以放成 \()\)

    • 如果 \(s[j]\) 可以放成 \((\),\(dp[i][j] += dp[i+1][j-1]\)
    • 如果 \(s[j]\) 可以放成 \()\),\(dp[i][j] += dp[i+1][j]\)

然后有一部分会被重复计算,也就是 \(dp[i+1][j-1]\) 这一段,所以只要顺便记录一下转移过程中计算了几次这一段,然后扣掉多计算的就可以了。

view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e3 + 10;
const int maxm = 1e5 + 10;
const ll mod = 998244353;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m, TT;
int cas, tol; int a[maxn] = {0};
char s[maxn];
ll dp[maxn][maxn]; ll fpow(ll a, ll b) {
ll ans = 1;
while(b) {
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
} int main() {
scanf("%s", s+1);
n = strlen(s+1);
for(int i=1; i<=n; i++) {
a[i] = a[i-1]+(s[i]=='?');
dp[i][i] = 0;
dp[i][i+1] = (i+1<=n && s[i]!=')' && s[i+1]!='(');
}
for(int d=3; d<=n; d++) {
for(int i=1, j=d, c; j<=n; i++, j++) {
dp[i][j] = 0, c = -1;
if(s[i] != ')') {
if(s[j] != ')') dp[i][j] += dp[i][j-1], c++;
if(s[j] != '(') dp[i][j] += dp[i+1][j-1] + fpow(2, a[j-1]-a[i]);
}
if(s[i] != '(') {
if(s[j] != ')') dp[i][j] += dp[i+1][j-1], c++;
if(s[j] != '(') dp[i][j] += dp[i+1][j], c++;
}
dp[i][j] -= max(c, 0)*dp[i+1][j-1];
dp[i][j] %= mod;
// printf("dp[%d][%d] = %lld\n", i, j, dp[i][j]);
}
}
printf("%lld\n", dp[1][n]);
return 0;
}

Codeforces Round #604 (Div. 2) D、E、F题解的更多相关文章

  1. Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学

    题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...

  2. Codeforces Round #725 (Div. 3) A-D,F题解

    A. Stone Game 思路:总共3种情况,都从最左端被拿走,都从最右端被拿走,左侧的从最左端被拿走且右侧的从最右端被拿走,取最小值即可 代码: //CF-725.A #include<bi ...

  3. Codeforces Round #604 (Div. 2) 练习A,B题解

    A题 链接 思路分析: 因为只需要做到相邻的不相同,利用三个不同的字母是肯定可以实现的, 所以直接先将所有的问号进行替换,比如比前一个大1,如果与后面的冲突,则再加一 代码(写的很烂): #inclu ...

  4. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  5. Codeforces Round #376 (Div. 2) C D F

    在十五楼做的cf..一会一断...比赛的时候做出了ABCF 就没有时间了 之后没看题解写出了D..E是个神奇的博弈(递推或者dp?)看了题解也没有理解..先写了CDF.. C 有n个袜子 每个袜子都有 ...

  6. Codeforces Round #604(Div. 2,

    // https://codeforces.com/contest/1265/problem/D /* 感觉像是遍历的思维构造题 有思路就很好做的 可以把该题想象成过山车或者山峰...... */ # ...

  7. Codeforces Round #604 (Div. 2) (题解)

    A. Beautiful String (暴力) 题目链接 题目大意: 给定一个字符串,只有 \(?a\ b\ c\ ?\) ,问是否存在一种将所有的 \(?\) 替换成 \(a\ b\ c\) ,使 ...

  8. Codeforces Round #604 (Div. 2) E. Beautiful Mirrors

    链接: https://codeforces.com/contest/1265/problem/E 题意: Creatnx has n mirrors, numbered from 1 to n. E ...

  9. Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

    链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...

随机推荐

  1. 一.OS运行机制

    运行机制: 1.中断(外部) =====一种通知行为(例如插入键盘) 2.系统调用(主动反应) ===一种请求行为 3.异常(内部) =====一种错误处理行为 系统调用和程序接口的关系,接口把系统调 ...

  2. convert datatable to List<T>

    public class DataConvert { public static List<T> ConvertDataTable<T>(DataTable dt) { Lis ...

  3. [翻译]微软 Build 2019 正式宣布 .NET 5

    原文: Introducing .NET 5 今天,我们宣布 .NET Core 3.0 之后的下一个版本将是 .NET 5 .这将是 .NET 系列的下一个重要版本. 将来只会有一个 .NET ,您 ...

  4. Git以及GitHub的一些基本使用

    1:注册GitHub账号: https://github.com/ 2:Git bash工具下载地址 https://gitforwindows.org/ 3:怎么在GitHub 新增 SSH Key ...

  5. Python - 元组 - 第九天

    Python 元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可.例如: >&g ...

  6. 来点高逼格的,使用前端Sendmessage实现SSO

    关于什么叫SSO(单点登录),这个概念具体的信息我就不详述了,网上一搜一大把,总的来说,它是一种可以控制各个独立的系统经过它的授权后, 可以用同一个帐号体系登录不同的系统,达到一处登录,多处使用的效果 ...

  7. Yapi接口管理平台 本地部署 windows环境 -

    YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布.维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只 ...

  8. px与em的区别

    PX特点:px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.EM特点 1. em的值并不是固定的:2. em会继承父级元素的字体大小.

  9. Python【day 14-2】递归遍历文件夹

    #需求 遍历文件夹中所有的子文件夹及子文件--用递归实现 '''''' ''' 伪代码 1.遍历根目录--listdir for 得到第一级子文件夹(不包含子文件夹的子文件)和文件 2.判断是文件还是 ...

  10. Codeforces 939A题,B题(水题)

    题目链接:http://codeforces.com/problemset/problem/939/A A题 A. Love Triangle time limit per test 1 second ...