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 ...
随机推荐
- 腾讯出品!这款Markdown神器让你码字效率翻倍,双模式编辑太香了!
嗨,大家好,我是小华同学,关注我们获得"最新.最全.最优质"开源项目和高效工作学习方法 由腾讯开源的CherryMarkdown编辑器,集思维导图式大纲写作与专业分屏模式于一身,支 ...
- 工具 | Hashcat
0x00 简介 Hashcat是一款强大的密码破解工具. 下载地址 Hashcat下载: Hashcat下载 0x01 功能说明 直接破解 组合攻击 掩码暴力破解 混合攻击 联合攻击 注:仅供安全研究 ...
- Linux系列:聊一聊 SystemV 下的进程间共享内存
一:背景 1. 讲故事 昨天在分析一个 linux 的 dump 时,看到了这么一话警告,参考如下: 0:000> !eeheap -gc *** WARNING: Unable to veri ...
- Python 3.14 新特性盘点,更新了些什么?
Python 3.14.0 稳定版将于 2025 年 10 月正式发布,目前已进入 beta 测试阶段.这意味着在往后的几个月里,3.14 的新功能已冻结,不再合入新功能(除了修复问题和完善文档). ...
- 基于CARLA/ROS的多传感器融合感知系统实战教程(附完整代码)
引言:为什么需要多传感器融合? 在自动驾驶系统中,单一传感器存在固有缺陷: 摄像头:易受光照影响,缺乏深度信息: 激光雷达(LiDAR):成本高,纹理信息缺失: 毫米波雷达:分辨率低,角度精度差. 本 ...
- [已解决] Compilation error ptxas fatal : Value ‘sm_30‘ is not defined for option ‘gpu-name‘
在用cmake编译cuda程序时,总是报Compilation error ptxas fatal : Value 'sm_30' is not defined for option 'gpu-nam ...
- C++11 auto和decltype关键字
今天来看下C++中的auto和decltype两个关键字 auto关键字定义变量,编译器会自动判断变量的类型 举个栗子: auto i =100; // i 是 int auto p = new A( ...
- Django中的内置Tags
Dates {% now "m/d/Y" %} copyright {% now 'Y' as current_year %} 该tag也可以接受Django的date 变量,比如 ...
- C#/.NET/.NET Core技术前沿周刊 | 第 38 期(2025年5.12-5.18)
前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...
- JavaScript入门笔记day1
文章目录 啥是JavaScript JavaScript与HTML的结合方式 js文件在HTML中的位置 注释方式 变量 命名规范: 定义的关键字 To be a struggling Rick fo ...