【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的物品,问是否存在取物方案使得价 ...
随机推荐
- 获取图片的EXIF信息
对于专业的摄影师来说,Exif信息是很重要的信息,也包含了非常多的东西 1.EXIF EXIF(Exchangeable Image File)是“可交换图像文件”的缩写,当中包含了专门为数码相机的照 ...
- Mudo C++网络库第四章学习笔记
C++多线程系统编程精要 学习多线程编程面临的最大思维方式的转变有两点: 当前线程可能被切换出去, 或者说被抢占(preempt)了; 多线程程序中事件的发生顺序不再有全局统一的先后关系; 当线程被切 ...
- Openssl与私有CA搭建
转自:http://www.tuicool.com/articles/aURnim 随着网络技术的发展.internet的全球化,信息共享程度被进一步提高,各种基于互联网的应用如电子政务.电子商务日益 ...
- html5 - Storage 本地存储
Storage的解释 http://www.w3school.com.cn/html5/html_5_webstorage.asp 简单的理解就是: Storage 有两种: 1.localStora ...
- $Django ajax简介 ajax简单数据交互,上传文件(form-data格式数据),Json数据格式交互
一.ajax 1 什么是ajax:异步的JavaScript和xml,跟后台交互,都用json 2 ajax干啥用的?前后端做数据交互: 3 之前学的跟后台做交互的方式: -第一种:在浏览器 ...
- CANopen--实现双电机速度同步
图1 将上图图中左边的电机和右边的电机进行速度同步,右边的电机同步左边的电机速度.这里需要知道Copley的驱动中的速度环的输入输出情况.如下图所示,速度环限制器接收速度命令信号,经限制后,产生一限制 ...
- python第13天
装饰器 装饰器本质上就是一个python函数,他可以让其他函数在不需要做任何改动的前提下,增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用场景:比如插入日志,性能测试,事务处理,缓存等等 ...
- awk-for循环简单用法
文本: [root@VM_0_84_centos ~]# cat sshd.txt 1 2 3 4 5 6 7 8 9 循环打印上述文本 for 循环的固定格式 i=1设置i的初始变量 i< ...
- linux杀死僵尸进程
用下面的命令找出僵死进程 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 命令注解: -A 参数列出所有进程 -o 自定义输出字段 我们设定显示字段为 sta ...
- Hive学习01-基础常见问题
理论: 什么是hive: 1. Hive旨在实现轻松的数据汇总,即时查询和分析大量数据. 2. 它提供了SQL,使用户可以轻松地进行临时查询,汇总和数据分析. 3. Hive可以使用用户定义函数( ...