咕咕咕了好多天终于有时间写篇博客了_(:з」∠)_

打网赛打到自闭的一周,终于靠这场CF找回了一点信心...

1041A - Heist

\(ans=max\left \{ a_i \right \}-min\left \{ a_i \right \}+1-n\)

#include<bits/stdc++.h>
using namespace std;
#define N 1001
int n,a[N],mx,mi;
int main()
{
mi=;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),
mx=max(mx,a[i]),
mi=min(mi,a[i]);
printf("%d\n",mx-mi+-n);
return ;
}

1041B - Buying a TV Set

设\(d=gcd(x,y), X=\frac{x}{d}, Y=\frac{y}{d}\),则\(ans=min(\frac{a}{X},\frac{b}{Y})\)

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL a,b,x,y;
LL gcd(LL x,LL y){return y?gcd(y,x%y):x;}
int main()
{
scanf("%I64d%I64d%I64d%I64d",&a,&b,&x,&y);
LL d=gcd(x,y);x/=d,y/=d;printf("%I64d\n",min(a/x,b/y));
}

1041C - Coffee Break

难点在于理解题意,题意大致就是把\(n\)分成\(x\)块,使得每块里的数两两相差大于\(d\),求最小的\(x\)

直接排序之后莽就好了,贪心是能保证正确性的

#include<bits/stdc++.h>
using namespace std;
#define N 200001
#define mp make_pair
struct rua{int v,id;}a[N];
set<pair<int,int> >s;
int n,m,d,cnt,f[N];
bool cmp(rua x,rua y){return x.v<y.v;}
int main()
{
scanf("%d%d%d",&n,&m,&d);
for(int i=;i<=n;i++)
scanf("%d",&a[i].v),a[i].id=i;
sort(a+,a+n+,cmp);
s.insert(mp(a[].v,)),f[a[].id]=++cnt;
for(int i=;i<=n;i++)
{
if((*s.begin()).first+d<a[i].v)
f[a[i].id]=(*s.begin()).second,s.erase(s.begin()),s.insert(mp(a[i].v,f[a[i].id]));
else s.insert(mp(a[i].v,++cnt)),f[a[i].id]=cnt;
}
printf("%d\n",cnt);
for(int i=;i<=n;i++)
printf("%d%c",f[i],i<n?' ':'\n');
}

1041D - Glider

显然在气流的开头跳是最优的,因此枚举在哪个气流的开头跳,二分求出能撑到第几个气流结束,通过预处理可以得到在一个区间内可以被抬多久,从而得出答案

#include<bits/stdc++.h>
using namespace std;
#define N 200001
int n,h,ans,s[N];
struct rua{int l,r;}a[N];
bool check(int st,int i,int j)
{
return h-(a[i].r-st)+(s[i]-s[j-])>;
}
int get(int st,int i)
{
int l=i,r=n;
while(l<r)
{
int mid=l+r+>>;
if(check(st,mid,i))l=mid;
else r=mid-;
}
return h+(s[l]-s[i-]);
}
int main()
{
scanf("%d%d",&n,&h);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].l,&a[i].r),s[i]=s[i-]+a[i].r-a[i].l;
for(int i=;i<=n;i++)
ans=max(ans,get(a[i].l,i));
printf("%d\n",ans);
return ;
}

1041E - Tree Reconstruction

显然\(n\)这个数一定会出现\(n-1\)次,现在只需考虑其他数字的出现情况

考虑\(n-1\)这个数,它出现的次数是等于\(n-1\)所在点到\(n\)所在距离的,且在从\(n\)到\(n-1\)这条路径上出现的数字是不会在输入中出现的。若将剩余出现的数从大到小排序,则有 当前数字出现次数=当前数字所在点到已知路径的距离,这里的已知路径是指已加入点之间的路径。可以发现构造一条链是足以满足题意的,对于空出的点将数字从大到小依次填入,并判断合法性即可。

代码中\(c_i\)为数字\(i\)出现的次数,\(p_i\)为数字\(i\)在答案中的位置,\(x_i\)则代表第\(i\)个位置是否已经被使用

#include<bits/stdc++.h>
using namespace std;
#define N 1005
int n,a[N],b[N],l,j=,c[N],p[N],ans[N],f[N],g[N];
bool x[N];
int main()
{
scanf("%d",&n),c[n]=p[n]=,l=n,x[]=true;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
if(a[i]<b[i])swap(a[i],b[i]);
if(a[i]<n)return printf("NO\n"),;
c[b[i]]++;
}
for(int i=n-;i>=;i--)if(c[i])
p[i]=p[l]+c[i],l=i,x[p[i]]=true;
for(int i=n;i>=;i--)if(!c[i])
{
while(j<=n && x[j])j++;p[i]=j,x[j]=true;
}
for(int i=;i<=n;i++)ans[p[i]]=i;
for(int i=;i<=n;i++)f[i]=max(f[i-],ans[i]);
for(int i=n;i>=;i--)g[i]=max(g[i+],ans[i]);
for(int i=;i<n;i++)
{
int A=f[i],B=g[i+],xx=;
for(int j=;j<=n;j++)
if(A==a[j] && B==b[j])
{a[j]=b[j]=,xx=;break;}
if(!xx)return printf("NO\n"),;
}
printf("YES\n");
for(int i=;i<=n;i++)printf("%d %d\n",ans[i-],ans[i]);
}

1041F - Ray in the tube

\(y1\)和\(y2\)是没用的,不用管它

由于\(n\)和\(m\)均大于等于1,因此答案至少为2,不少人因此FST

考虑射出去的线打到对面板上所需要走的距离\(d\),假设出发点为0,则打在自己这边上的点的坐标为\(\left \{ 0,2d,4d,6d,8d,... \right \}\),打在对面板上的则是\(\left \{ d,3d,5d,7d,9d,... \right \}\)。若将距离改为\(k\cdot d\),则两个点集分别为\(\left \{ 0,2kd,4kd,6kd,8kd,... \right \}\),\(\left \{ kd,3kd,5kd,7kd,9kd,... \right \}\),可以发现若\(k\)为奇数,两个集合中的点都只会减少不会增加,\(k\)为偶数时则有存在增减的情况,因此设\(k=2^{l}\)一定是最优的

接下去就直接暴力开个map做就好了,比赛的时候害怕FST还加了个优化,就是当\(k\)比较小时直接从\(0\)到\(k-1\)枚举余数,\(k\)较大时才遍历\(n+m\)个坐标。后来发现这个优化只优化了100ms...

#include<bits/stdc++.h>
using namespace std;
#define N 100001
int n,m,y,a[N],b[N];
map<int,int>f,g;
int main()
{
scanf("%d%d",&n,&y);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d%d",&m,&y);
for(int i=;i<=m;i++)
scanf("%d",&b[i]);
int ans=;
for(int k=;k;k*=)
{
f.clear(),g.clear();
for(int i=;i<=n;i++)
f[a[i]%k]++;
for(int i=;i<=m;i++)
g[b[i]%k]++;
if(k<=)
for(int i=;i<k;i++)
ans=max(ans,f[i]+g[(i+k/)%k]);
else
{
for(auto j:f)ans=max(ans,f[j.first]+g[(j.first+k/)%k]);
for(auto j:g)ans=max(ans,g[j.first]+f[(j.first+k/)%k]);
}
if(k==)
break;
}
printf("%d\n",ans);
return ;
}

Codeforces Round #509 (Div. 2)的更多相关文章

  1. Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)

    题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...

  2. Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)

    题目链接:http://codeforces.com/contest/1041/problem/E 题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b . 题 ...

  3. codeforces 1041d// Glider// Codeforces Round #509(Div. 2)

    题意:给出,n和飞行员高度h,n是区间数.在区间里飞行员高度不变,其它地方每秒高度-1,x坐标+1.问在高度变为0以前,x坐标最多加多少? 用数组gap记录本区间右端到下一个区间左端的距离.用sum记 ...

  4. Codeforces Round#509 Div.2翻车记

    A:签到 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  5. Codeforces Round #509 (Div. 2) A. Heist 贪心

    There was an electronic store heist last night. All keyboards which were in the store yesterday were ...

  6. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. maven多个web模块进行合并

    原文地址: https://blog.csdn.net/u011666411/article/details/77160907

  2. js实现可输入的下拉框

    <HTML> <HEAD> <META http-equiv='Content-Type' content='text/html; charset=gb2312'> ...

  3. windows服务器基本管理及服务搭建

    windows服务器基本管理及服务搭建 ****windows服务器系统版本:2000 2003 2008 2012 1.用户与组管理 用户:账户=账号/用户名+密码 每个账户有自己唯一的SID 账户 ...

  4. CIA402状态转换图

    CIA402状态转换如下图所示: 要想改变参数并使其生效,需要先将状态转换到ready,然后修改要配置的参数,再使其运行(operation enabled). 要发送的报文顺序基本如下: 1)   ...

  5. HDR拍照

    HDR 拍照:        (High Dynamic Range Imaging)高动态范围成像,是用来实现比普通数字图像技术更大曝光动态范围(即更大的明暗差别)的一组技术.高动态范围成像的目的就 ...

  6. CSS使用小记

    1. 省略显示 max-width: 200px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; 固定宽度,nowra ...

  7. 小程序 wx.request

    wx.request({ url: 'https://hanwslh5.qcloud.la/weapp/HelloWorld', 对于 GET 方法的数据,会将数据转换成 query string(e ...

  8. spring boot slf4j日记记录配置详解

    https://blog.csdn.net/liuweixiao520/article/details/78900779

  9. module.exports与exports

    API文档是枯燥的,下面本人收集了一些论坛经常有人疑问和开源代码中经常遇到的案例供大家研究一下. module.exports与exports的区别 每一个node.js执行文件,都自动创建一个mod ...

  10. Docker发布程序那些事

    最近使用docker比较频繁,所以也难免碰到一些坑,在这里记录一下,给自己写个笔记 1.docker 1.13.0版本不支持docker stack deploy -c 多个 -c的yml文件合并 如 ...