Educational Codeforces Round 97 ABCD 题解
A. Marketing Scheme
题意:对于一个x,如果\({\lfloor{x\over 2}\rfloor}\)<= \(x\) \(mod\) \(a\),则满足题意。现在问你能否选出一个a使得[l,r]区间内的所有数都满足题意。
思路:贪心,如果\(2l > r\),即左端点最优方案能同时满足最大的r,就输出YES,反之则NO。
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll l = read(), r = read();
ll len = l*2;
if(len>r) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
B. Reverse Binary Strings
题意:让你翻转一个01子串的任意子串,问最少多少步能够凑出01交替的串。(01数量各一半)。
思路:注意到,每次翻转都不能改变翻转区间内部的交替性质(如果是11连着翻转了还是11连着),一次翻转只会改变其左右端点两边的交替状态。因此我们可以先统计出需要交换1的数量和需要交换0的位置,然后两两配对(相当于当做区间左右端点翻转),有多少对就多少个,最后加上剩下不能配对的即是答案。
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
string s;
cin>>s;
char pre = (s[0]=='1'?'0':'1');
ll zeros = 0, ones = 0;
rep(i,0,s.size()-1)
{
if(s[i]==pre)
{
if(s[i]=='1') zeros++;
else ones++;
}
pre = s[i];
}
cout<<max(zeros,ones)<<endl;
}
return 0;
}
C. Chef Monocarp
题意:有n个菜,每个菜有个最适时刻t[i],每个时刻只能出一道菜。在一个时刻j出第i道菜的贡献是\(|t[i] - j|\), 现在要你安排出菜时间使得贡献和最小。
思路:这道题比后面的D题难想一点。。。比赛没想出来,赛后发现是一个dp的问题。
用dp[i][j]表示前i道菜,在j时刻出菜的最优解。
当前这个状态可以由
1.选当前j这个位置,让前i-1道菜从前j-1个位置里面选,然后加上选这个位置的贡献。 \(- > dp[i-1][j-1] + |t[i] - j|\) 。
2.不选当前j位置,直接从j-1位置转移过来,表示利用前j-1个位置出前i道菜。\(->dp[i][j-1]\)。
然后取两个状态的最小值转移过来。
最后注意一下要先对t数组排序再进行dp。
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll dp[500][500];
ll a[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
sort(a+1,a+1+n);
rep(i,0,n) rep(j,0,2*n) dp[i][j] = inf;
dp[0][0] = 0;
rep(i,1,n*2) dp[0][i] = 0;
rep(i,1,n) rep(j,1,2*n) dp[i][j] = min(dp[i-1][j-1]+abs(a[i] - j), dp[i][j-1]);
cout<<dp[n][2*n]<<endl;
}
return 0;
}
D. Minimal Height Tree
题意: 给你一个BFS的遍历顺序,且遍历时是按照子节点从小到大排序好访问的。现在问你最小深度是多少。
思路:这个题比前面的好想很多。
贪心:
1.首先我们定义一段连续上升的子串为一个块。因为贪心,如果能放,我们就直接把整个块里面的元素塞到一个父节点下面。
2.从上面可以发现这样会使得每一层的子节点(这一层所有块里面数量的和)尽量的多,而一个子节点又可以连接一个块,所以就每次拿到一个块,看看这一层还有没有位置可以放,可以的话就塞进去,同时这一层的承载量又可以加上当前块的内部个数。若放不下,就到新的一层继续连,同时更新承载量。
view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll a[maxn];
vector<ll> block;
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
block.clear();
rep(i,1,n) a[i] = read();
ll len = 0;
a[1] = 1;
rep(i,2,n)
{
if(a[i] > a[i-1]) len++;
else block.pb(len), len = 1;
}
block.pb(len);
ll cur_max = 1;
ll cur_num = 0;
ll nxt_max = 0;
int p = 0;
ll ans = 0;
while(p<block.size())
{
cur_num++;
if(cur_num > cur_max)
{
cur_max = nxt_max;
nxt_max = block[p];
cur_num = 1;
ans++;
}
else nxt_max += block[p];
p++;
}
cout<<ans+1<<endl;
}
return 0;
}
Educational Codeforces Round 97 ABCD 题解的更多相关文章
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- Educational Codeforces Round 63部分题解
Educational Codeforces Round 63 A 题目大意就不写了. 挺简单的,若果字符本来就单调不降,那么就不需要修改 否则找到第一次下降的位置和前面的换就好了. #include ...
- Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1437 A. Marketing Scheme 题解 令 \(l = \frac{a}{2}\),那么如果 \(r < ...
- Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)
题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...
- Educational Codeforces Round 97 (Rated for Div. 2)
补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...
- Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)
题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度. 题解:不看根节点,我们从第二个位置 ...
- Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)
题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...
- Educational Codeforces Round 16---部分题解
710A. King Moves 给你图中一点求出它周围有几个可达的点: 除边界之外都是8个,边界处理一下即可: #include<iostream> #include<cstdio ...
- Educational Codeforces Round 38 部分题解
D. Buy a Ticket 分析 建一个源点,连向所有结点,边的花费为那个结点的花费,图中原有的边花费翻倍,最后跑一遍最短路即可. code #include<bits/stdc++.h&g ...
随机推荐
- nginx禁止IP访问,仅供域名访问(域名访问限制不严格漏洞)
域名访问限制不严格漏洞解决 nginx添加相关配置 通过default_server,在http中最前面加上该配置 server { listen 80 default_server; server_ ...
- MCP应用docker部署,docker-compose部署
一.概述 前面几篇文章,MCP应用直接用的python3 server.py运行的,如果服务器重启,进程就会关掉,很不方便. 所以需要使用docker部署,实现开机自启动. 二.docker部署 my ...
- iOS深色模式媒体查询css
@media (prefers-color-scheme: dark) { body { background-color: #000; color: #fff; } }
- 安卓逆向学习及APK抓包(二)--Google Pixel一代手机的ROOT刷入面具
PS:本文仅作参考勿跟操作,root需谨慎,本次测试用的N手Pixel,因参考本文将真机刷成板砖造成的损失与本人无关 1 Google Pixel介绍 1.1手机 google Pixel 在手机选择 ...
- mysql免密登录
开启mysql免密登录, vi /etc/my.cnf [mysqld]下添加 skip-grant-tables , 保存后重启mysql服务:service mysqld restart
- 正点原子ALPHA开发板使用4.3寸触摸屏LCD驱动实验显示不正常
显示问题 裸机开发时,驱动教程的PDF里给了4.3寸LCD屏幕的设置参数.如下图所示: 但是按照这个设置,编写设备树dts文件,下载到开发板里,却出现了显示异常,具体来说就是帧率不对,图和字都是歪斜的 ...
- 挑战零基础用CodeBuddy做一款音视频处理软件
朋友们,我最近不是一直在捣鼓小软件嘛!手头这个叫TransDuck的音视频处理工具刚有点小爆的趋势,反馈意见里也是收到不少关于SaaS版本的问题,比如: "翻译效果真的顶!但每次上传比较大的 ...
- 用户空间的系统调用是如何链接到内核空间的系统调用的——MIT6.S081学习记录
用户态的sysinfo(),首先系统会从user/user.h里找到声明,随后由链接到 usys.S 中的汇编代码来实现的.usys.S是通过usys.pl生成的.usys.S 文件定义了所有系统调用 ...
- Java 自定义线程池的任务
在<Java 自定义线程池的线程工厂>一文中介绍了如何优雅地自定义线程工厂,本文介绍如何自定义线程池的任务,并拿到返回值. 首先自定义一个任务类,实现Callable接口,重写ca ...
- MyBatis常见面试题:通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗?
MyBatis常见面试题:通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗? Dao接口即Mapper接 ...