【Codeforces】Codeforces Round #551 (Div. 2)

时间:2022-08-29 18:09:21

Codeforces Round #551 (Div. 2)

算是放弃颓废决定好好打比赛好好刷题的开始吧

A. Serval and Bus

处理每个巴士最早到站且大于t的时间

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,t;
int c[105];
void Solve() {
read(N);read(t);
int s,d;
for(int i = 1 ; i <= N ; ++i) {
read(s);read(d);
if(s >= t) c[i] = s;
else c[i] = s + ((t - s - 1) / d + 1) * d;
}
int ans = 1;
for(int i = 2 ; i <= N ; ++i) {
if(c[ans] - t > c[i] - t) ans = i;
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

B. Serval and Toy Bricks

保证有解直接在俯视图每个1的位置填上行列上限的最小值即可

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M,H;
int f[105],l[105];
void Solve() {
read(N);read(M);read(H);
for(int i = 1 ; i <= M ; ++i) {
read(f[i]);
}
for(int i = 1 ; i <= N ; ++i) {
read(l[i]);
}
int a = 0;
for(int i = 1 ; i <= N ; ++i) {
for(int j = 1 ; j <= M ; ++j) {
read(a);
if(!a) out(0);
else out(min(l[i],f[j]));
space;
}
enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

C. Serval and Parenthesis Sequence

如果每个位置都不合法,只需要左右两端是匹配的括号,然后对于剩下的部分做括号匹配即可

就是从前往后扫,如果遇到左括号扔进一个栈,遇到问号扔到一个栈,遇到右括号优先找一个左括号匹配,然后问好匹配左括号,然后问号两两匹配,不用在乎先后顺序,因为对于一个完整的匹配(),如果我在这个括号里插入一个(,在最右再多一个),还是一个完整匹配

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
char s[300005];
int N;
int sta[2][300005],top[2];
void Solve() {
read(N);
scanf("%s",s + 1);
if(s[1] == ')' || s[N] == '(') {puts(":(");return;}
if(N & 1) {puts(":(");return;}
s[1] = '(';s[N] = ')';
for(int i = 2 ; i < N ; ++i) {
if(s[i] == '(') sta[0][++top[0]] = i;
else if(s[i] == '?') sta[1][++top[1]] = i;
else {
if(top[0]) --top[0];
else if(top[1]){
int u = sta[1][top[1]--];
s[u] = '(';
}
else {puts(":(");return;}
}
}
while(top[0]) {
int u = sta[1][top[1]--],v = sta[0][top[0]--];
if(u < v) {puts(":(");return;}
s[u] = ')';
}
if(top[1] & 1) {puts(":(");return;}
for(int i = 1 ; i <= top[1] ; ++i) {
int u = sta[1][i];
if(i & 1) s[u] = '(';
else s[u] = ')';
}
for(int i = 1 ; i <= N ; ++i) {
putchar(s[i]);
}
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

D. Serval and Rooted Tree

我们可以求出每个点最大能取到第几大的

设这个值是\(dp[u]\)

如果一个节点是min,答案就是这个节点所有儿子的dp[u] + 1的总和-1

如果一个节点是max,答案就是这个节点所有儿子中最小的dp[u]

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 300005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,op[MAXN],f[MAXN],dp[MAXN],K;
vector<int> son[MAXN];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(op[i]);
for(int i = 2 ; i <= N ; ++i) read(f[i]);
for(int i = N ; i >= 1 ; --i) {
if(son[i].size() == 0) {dp[i] = 0;++K;}
else {
int sum = 0,mm = N;
for(auto s : son[i]) {
mm = min(dp[s],mm);
sum += dp[s] + 1;
}
if(!op[i]) dp[i] = sum - 1;
else dp[i] = mm;
}
son[f[i]].pb(i);
}
out(K - dp[1]);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

E. Serval and Snake

我们直接问每一行或一列(认为是一行的下边界或一列的右边界)的线切开了多少大蛇,如果某一行切开的蛇和上一行切开的蛇相差是奇数,那么这行必定有个头或者有个尾

如果行列都有这样的,那么判断这两行两列交出的四个点哪个点连向四周的边是奇数

如果只有行或列有,那么可以通过二分,就是最小列的使得这一行的切割点是奇数

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 300005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int r[1005],c[1005];
vector<int> ac,ar;
vector<pii > ans;
int Query(int x1,int y1,int x2,int y2) {
printf("? %d %d %d %d\n",x1,y1,x2,y2);
fflush(stdout);
int x;read(x);return x;
}
void Solve() {
read(N);
for(int i = 1 ; i < N ; ++i) {
r[i] = Query(1,1,i,N);
c[i] = Query(1,1,N,i);
}
for(int i = 1 ; i <= N ; ++i) {
if((r[i] ^ r[i - 1]) & 1) ar.pb(i);
if((c[i] ^ c[i - 1]) & 1) ac.pb(i);
}
if(ar.size() >= 2 && ac.size() >= 2) {
for(int i = 0 ; i < 2 ; ++i) {
for(int j = 0 ; j < 2 ; ++j) {
if(Query(ar[i],ac[j],ar[i],ac[j]) == 1) ans.pb(mp(ar[i],ac[j]));
}
}
}
else if(ar.size() >= 2) {
int l = 1,r = N;
while(l < r) {
int mid = (l + r) >> 1;
if(Query(ar[0],1,ar[0],mid) & 1) r = mid;
else l = mid + 1;
}
ans.pb(mp(ar[0],l));ans.pb(mp(ar[1],l));
}
else if(ac.size() >= 2) {
int l = 1,r = N;
while(l < r) {
int mid = (l + r) >> 1;
if(Query(1,ac[0],mid,ac[0]) & 1) r = mid;
else l = mid + 1;
}
ans.pb(mp(l,ac[0]));ans.pb(mp(l,ac[1]));
}
putchar('!');space;out(ans[0].fi);space;out(ans[0].se);space;
out(ans[1].fi);space;out(ans[1].se);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

F. Serval and Bonus Problem

老了,这么普通的一道计数都切不动

就是转化成插入一个点,使得这个点至少在k个之间

可以用dp完成,相当于给2*n + 1个点分配身份

就是\(f[i][j][x]\)x为0或1表示有没有插入特殊点,到了第\(i\)个点

然后如果\(j >= k , x = 0\),那么可以转移\(f[i][j][x] \rightarrow f[i + 1][j][x]\)

或者加上一个新区间\(f[i][j][x] \rightarrow f[i + 1][j + 1][x]\)

或者结束一个区间\(f[i][j][x] \rightarrow f[i + 1][j - 1][x] \times j\)

方案总数是\(\frac{(2n + 1)!}{n!2^{n}}\)因为两个点是等价的就直接除上\(2^n\),然后n个区间的标号是没有顺序的,所以再除上一个\(n!\)

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 300005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
int k,n,l;
int dp[4005][2005][2],fac[4005];
void Solve() {
read(n);read(k);read(l);
dp[0][0][0] = 1;
for(int i = 0 ; i < 2 * n + 1; ++i) {
for(int j = 0 ; j <= n ; ++j) {
for(int x = 0 ; x <= 1 ; ++x) {
if(!dp[i][j][x]) continue;
if(j >= k && !x) update(dp[i + 1][j][1],dp[i][j][x]);
if(j >= 1) update(dp[i + 1][j - 1][x],mul(dp[i][j][x],j));
update(dp[i + 1][j + 1][x],dp[i][j][x]);
}
}
}
fac[0] = 1;
for(int i = 1 ; i <= 2 * n + 1 ; ++i) fac[i] = mul(fac[i - 1],i);
int ans = l;
ans = mul(ans,dp[2 * n + 1][0][1]);
ans = mul(ans,fpow(fac[2 * n + 1],MOD - 2));
ans = mul(ans,fac[n]);
ans = mul(ans,fpow(2,n));
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}

【Codeforces】Codeforces Round #551 (Div. 2)的更多相关文章

  1. 【Codeforces】CF Round &num;592 &lpar;Div&period; 2&rpar; - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...

  2. 【二分】CF Round &num;587 &lpar;Div&period; 3&rpar;E2 Numerical Sequence &lpar;hard version&rpar;

    题目大意 有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...,即重复的1~1,1~2....1~n,给你一个\(k\),求第\(k(k<=10^{1 ...

  3. 【题解】Codeforces 961G Partitions

    [题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...

  4. CF Round &num;551 &lpar;Div&period; 2&rpar; D

    CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...

  5. 思维题--code forces round&num; 551 div&period;2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  6. Codeforces 716A Crazy Computer 【模拟】 &lpar;Codeforces Round &num;372 &lpar;Div&period; 2&rpar;&rpar;

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces 716B Complete the Word【模拟】 &lpar;Codeforces Round &num;372 &lpar;Div&period; 2&rpar;&rpar;

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. 【codeforces】Codeforces Round &num;277 &lpar;Div&period; 2&rpar; 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  9. 【CF1256】Codeforces Round &num;598 &lpar;Div&period; 3&rpar; 【思维&plus;贪心&plus;DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

随机推荐

  1. &lbrack;Nginx&rsqb; 在Linux下的启动、停止和重加载

    Nginx的启动 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 其中-c参数指定配置文件路径.   Nginx的停止 ...

  2. C&num;中的Marshal

    Const.MaxLengthOfBufferd的长度固定为0x2000   也就是8192 private bool SendMessage(int messageType, string ip, ...

  3. Hotel - poj 3667&lpar;求连续子区间&rpar;

    题意:有两种操作 1,从左往右找一个区间是 D 的连续序列,然后覆盖,返回区间最前面的数,如果没有输出0 2, 释放从L开始连续D的区间 分析:就是从左往右查找一个D的连续区间,可以使用三个值操作ls ...

  4. iOS 辛格尔顿

    单例模式: 为什么使用单例,单例模式的用途是什么?以下我们举一个样例来诠释一下 举个大家都熟知的样例--Windows任务管理器,如图,我们能够做一个这种尝试,在Windows的"任务栏&q ...

  5. Android studio登录界面

    打开Android studio,你需要建立两个类LoginMainAcitivity.java和SuccessMainActivity.java,和与之相对应的xml布局文件login_main.x ...

  6. SpriteBuilder中子节点的相对位置&lpar;&percnt;百分比定位&rpar;

    子节点(或在这里确切的为精灵sprites)50%的偏移效果使得其在父节点中居中显示,该父节点的纹理在左下角(锚点为0,0). 这样做好过用父节点的位置的实际值来定位.根据父节点实际位置来定位在早期的 ...

  7. html&plus;css&plus;jq随记

    随便写个博客吧,记录一下自己的历程,今天忽然用自己好久不用的jq还做项目,并且从零开始搭建,让自己慌乱不已啊!遇到了如下问题 1.ios端点击闪屏的问题,解决办法如下 在body上添加 -webkit ...

  8. Redis持久化及其配置

    引言 终于可以有时间继续看书,整理自己的见解, 写下2019年第一篇自己的随笔.从去年9月份跳槽到新公司后,几乎天天的加班让整个人都盲目了,原本计划好的事情总是会被打乱.都说坚持一件事情很难,特别是写 ...

  9. composer install Your requirements could not be resolved to an installable set of packages

    composer install --ignore-platform-reqs 或者 composer update --ignore-platform-reqs

  10. 【HNOI2017】单旋

    题面 题解 trajan的spaly是O(1)的(逃 通过看题解手玩发现只要将最值的点放到树根,其他的父子关系不需要变. 于是想到动态连边和断边的数据结构:\(\mathrm{LCT}\),于是用\( ...