【Codeforces】Codeforces Round #551 (Div. 2)
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)的更多相关文章
- 【Codeforces】CF Round #592 (Div. 2) - 题解
Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...
- 【二分】CF Round #587 (Div. 3)E2 Numerical Sequence (hard version)
题目大意 有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...,即重复的1~1,1~2....1~n,给你一个\(k\),求第\(k(k<=10^{1 ...
- 【题解】Codeforces 961G Partitions
[题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...
- CF Round #551 (Div. 2) D
CF Round #551 (Div. 2) D 链接 https://codeforces.com/contest/1153/problem/D 思路 不考虑赋值和贪心,考虑排名. 设\(dp_i\ ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
随机推荐
- 【转】C++对成员访问运算符->的重载
运算符->的重载比较特别,它只能是非静态的成员函数形式,而且没有参数. 1.如果返回值是一个原始指针,那么就将运算符的右操作数当作这个原始指针所指向类型的成员进行访问: 2.如果返回值是另一个类 ...
- 【转】C++拷贝构造函数详解
一.什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: ; int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量. 下面看一个类对象拷贝 ...
- 使用sudo而无需输入密码的设置
在linux上,root用户是老大,什么事都能做.但是,很多时候由于安全等各种原因,我们不希望把root用户开放给大家,但是又希望其他的用户可以有root的权限,所以就有了sudo用户.而执行sudo ...
- page.isvalid
背景 看到这个标题,想了半天,为啥用的.应该是当初前台要动态增加行这个事情,当初用.net真是用傻了,竟然对html.aspx原理不大清楚,对于html也想着后台生成.真是弱智啊.谈到这里,想到c#这 ...
- interface{} 泛型编程
转自: 张晓龙 中兴开发者社区 https://mp.weixin.qq.com/s/EEUtTykcrXhcM2hJT01SoQ 序言 众所周知,Golang中不支持类似C++/Java中的标记式泛 ...
- 修改svn默认端口
Subversion有两种不同的配置方式,一种基于它自带的轻量级服务器svnserve,一种基于非常流行的Web服务器Apache. 根据不同的配置方式,Subversion使用不同的端口对外提供服务 ...
- VS下载Github的项目引用报错
错误如下 1.添加新的Nuget包源 添加新的源 https://www.nuget.org/api/v2/ 2.还原Nuget包 3.升级Visual Studio到最新 工具-->获取工具和 ...
- UVA 1395 MST
给你一个图, 求一个生成树, 边权Max – Min 要最小,输出最小值, 不能构成生成树的 输出 -1: 思路: Keuksal 算法, 先排序边, 然后枚举 第一条边, 往后加入边, 直到有 n- ...
- [JavaScript]iframe的contentWindow
HTMLIFrameElement.contentWindow返回的是HTMLIFrameElement类型元素的window对象 通过此对象可以修改iframe实体内的window行为 <if ...
- Confluence 6 其他需要备份和恢复的地方
XML 备份被描述用于在 Confluence 备份使用的其他方法,例如升级和移动服务器.使用上面描述的备份和恢复方法也适用这些地方. 我们的 upgrade guide 不要求使用一个 XML 备份 ...