Educational Codeforces Round 159 (Rated for Div. 2)(B 二分贪心 Cgcd D二分+前缀和 E字典树)

本文主要是介绍Educational Codeforces Round 159 (Rated for Div. 2)(B 二分贪心 Cgcd D二分+前缀和 E字典树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

A - Binary Imbalance

有只要在01之间插入就能制造无限个0,没有0就统计0 1个数即可

#include<bits/stdc++.h>
using namespace std;
const int N =1100+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
using node=tuple<int,int,int>;
int n,m,k;
int s[N][N];
char p[N][N];
void solve()
{cin>>n;string s;cin>>s;int cnt0=0;bool ok=false;for(int i=0;i<s.size();i++){if(s[i]=='0') cnt0++;else cnt0--;if(i!=0&&s[i-1]!=s[i]) ok=true;}if(ok||cnt0>0){cout<<"Yes\n";}else cout<<"No\n";
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;cin>>t;while(t--) solve();
}

B - Getting Points

具有单调性,n也挺大,我直接二分可以休息多少天了

然后直接算n天一共有多少个任务,然后有个特殊情况是

如果我最后一天刚好是发布任务的那天且今天是周一

那么我当前的任务只能让最后一个做

前面能做的任务要减1,能解决的总任务是2*x-1

#include<bits/stdc++.h>
using namespace std;
const int N =2e5+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
using node=tuple<int,int,int>;
int n,m,k;
void solve()
{int p,l,t;cin>>n>>p>>l>>t;auto check=[&](int x){x=n-x;int now=(n+6)/7;int res=l*x+min(now,x*2)*t;if(n%7==1){res=l*x+min(now-1,x*2-1)*t+t;}return res>=p;};int ll=0,r=n;while(ll<r){int mid=ll+r+1>>1;if(check(mid)) ll=mid;else r=mid-1;}cout<<ll<<"\n";
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;cin>>t;while(t--) solve();
}

C:

我直接一眼直觉得出结论了ahhh(虽然不知道这想法哪来的,反正就很直觉)

赛后证明一下

每个数要变成一个统一的数字 x

那么第一个数要是 a+kw=x ->> x-a=kw

第二个数是 b+kw=x -> x-b=kw

....

因为只能数只能增大不能减少,所以我们直接让x为最大值即可

然后求最大值和每个数的差值的最大公因数即可

然后增加一个数,肯定也要上最大值加上这个gcd了

然后我是直接枚举两种情况的

比最大值大,和比最大值小的

循环n+1次肯定都能找到,然后计算代价即可

#include<bits/stdc++.h>
using namespace std;
const int N =2e5+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;int n,m,k;
int a[N];
int gcd(int a,int b){return b?gcd(b,a%b):a;
}
void solve()
{cin>>n;for(int i=1;i<=n;i++) cin>>a[i];if(n==1){cout<<1<<"\n";return ;}sort(a+1,a+1+n);vector<int> b;for(int i=1;i<n;i++){b.push_back(a[n]-a[i]);}int g=b[0];for(auto x:b) g=gcd(g,x);int res=0;for(auto x:b){res+=x/g;}map<int,int> mp;for(int i=1;i<=n;i++){mp[a[i]]++;}int mn=2e18;int cnt=n;for(int i=0,now=a[n];i<=n;i++){now+=g;if(!mp.count(now)){mn=min(mn,n*(now-a[n])/g);break;}}for(int i=0,now=a[n];i<=n;i++){now-=g;if(!mp.count(now)){mn=min(mn,abs(now-a[n])/g);break;}}cout<<res+mn<<"\n";
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;cin>>t;while(t--) solve();
}

D - Robot Queries

手画一下可以发现,中间那段翻转不影响最终结果

然后还是做不了发现

然后我们要进行转化一下

转化成前缀和,把区间问题变成选择两个点后到达的点是xi,yi

对于不是反转的区间 [1,L-1]和[R-1,N]这两个区间,直接从里面找即可

对于反转的我们其实也可以直接找

比如要找的点是 xi,y1

那么还原回来

pre[L-1]+pre[R]-pre[x-1]=xi

pre[x-1]=pre[L-1]+pre[R]-xi

注意这里的pre[R]-pre[x-1]是代表着翻转的区间的变化量

#include<bits/stdc++.h>
using namespace std;
const int N =2e5+10,mod=998244353;
#define int long long
typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
using node=tuple<int,int,int>;
int n,m,k;
int a[N];
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
map<char,int>dir={{'U',2},{'D',3},{'L',1},{'R',0}};
void solve()
{cin>>n>>m;string s;cin>>s;vector<PII> pre(n+10);pre[0]={0,0};s="?"+s;for(int i=1;i<=n;i++){int k=dir[s[i]];auto [x,y]=pre[i-1];x+=dx[k],y+=dy[k];pre[i]={x,y};}vector<set<PII>> v(6*n+10);for(int i=0;i<=n;i++){auto [x,y]=pre[i];v[x+3*n].insert({y,i});}for(int i=0;i<=4*n;i++){v[i].insert({inf,inf});}while(m--){int tx,ty,L,R;cin>>tx>>ty>>L>>R;int px,py;px=tx,py=ty;auto [y1,id1]=*v[px+3*n].lower_bound({py,0});if(y1==py&&id1<=L-1){cout<<"YES\n";continue;}//[L,R]auto [adx,ady]=pre[L-1];auto [bdx,bdy]=pre[R];px=bdx+adx-tx,py=bdy+ady-ty;//cout<<px<<' '<<py<<'\n';auto [y2,id2]=*v[px+3*n].lower_bound({py,L-1});if(y2==py&&id2>=L-1&&id2<=R){cout<<"YES\n";continue;}//[R+1,n]px=tx,py=ty;auto [y3,id3]=*v[px+3*n].lower_bound({py,R+1});if(y3==py&&id3>=R+1&&id3<=n){cout<<"YES\n";continue;}cout<<"NO\n";}
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;// cin>>t;while(t--) solve();
}

E - Collapsing Strings

这个题之前我博客遇到过相似的题

然后也直接一眼题...

直接跑个字典树,然后翻转字符串减去要减去的贡献即可

我的query里面你可以

第一个点是p,第二个点的cnt[p]是3

然后要减去的代价是两个点之间消失的个数*当前点的长度-1

3-2就代表着我从第二个点到第三个点少了一个可以匹配的字符串

说明这一个字符串只能匹配到长度2的前缀他就没了

然后还有个问题是如果匹配完了我的lst不为0

说明有lst个字符串能完整匹配完

#include<bits/stdc++.h>
using namespace std;
const int N =1e6+10,mod=998244353;typedef long long LL;
typedef pair<int, int> PII;
const long long inf=1e17;
using node=tuple<int,int,int>;
int n,m,k;
int a[N];
int tr[N][27];
string s[N];
int cnt[N],idx;
long long res;
void insert(string s){int p=0;for(int i=0;i<s.size();i++){int u=s[i]-'a';if(!tr[p][u]) tr[p][u]=++idx;p=tr[p][u];cnt[p]++;}
}
void query(string s){int n=s.size();int len=0;int p=0,lst=-1;for(int i=0;i<s.size();i++){int u=s[i]-'a';len++;if(!tr[p][u]){if(lst){res-=1ll*lst*2*(len-1);}return ;}p=tr[p][u];if(lst==-1){lst=cnt[p];continue;}res-=1ll*(lst-cnt[p])*2*(len-1);lst=cnt[p];}if(lst){res-=1ll*lst*2*len;}}
void solve()
{cin>>n;int sum=0;for(int i=1;i<=n;i++){cin>>s[i];insert(s[i]);sum+=s[i].size();}for(int i=1;i<=n;i++){res+=sum+1ll*(int)(s[i].size())*n;reverse(s[i].begin(),s[i].end());query(s[i]);}cout<<res<<"\n";
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t=1;// cin>>t;while(t--) solve();
}

这篇关于Educational Codeforces Round 159 (Rated for Div. 2)(B 二分贪心 Cgcd D二分+前缀和 E字典树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/455794

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

poj 3258 二分最小值最大

题意: 有一些石头排成一条线,第一个和最后一个不能去掉。 其余的共可以去掉m块,要使去掉后石头间距的最小值最大。 解析: 二分石头,最小值最大。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <c

poj 2594 二分图最大独立集

题意: 求一张图的最大独立集,这题不同的地方在于,间接相邻的点也可以有一条边,所以用floyd来把间接相邻的边也连起来。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <sta

poj 3692 二分图最大独立集

题意: 幼儿园里,有G个女生和B个男生。 他们中间有女生和女生认识,男生男生认识,也有男生和女生认识的。 现在要选出一些人,使得这里面的人都认识,问最多能选多少人。 解析: 反过来建边,将不认识的男生和女生相连,然后求一个二分图的最大独立集就行了。 下图很直观: 点击打开链接 原图: 现图: 、 代码: #pragma comment(

poj 2112 网络流+二分

题意: k台挤奶机,c头牛,每台挤奶机可以挤m头牛。 现在给出每只牛到挤奶机的距离矩阵,求最小化牛的最大路程。 解析: 最大值最小化,最小值最大化,用二分来做。 先求出两点之间的最短距离。 然后二分匹配牛到挤奶机的最大路程,匹配中的判断是在这个最大路程下,是否牛的数量达到c只。 如何求牛的数量呢,用网络流来做。 从源点到牛引一条容量为1的边,然后挤奶机到汇点引一条容量为m的边