Codeforces Round #509 (Div. 2)
咕咕咕了好多天终于有时间写篇博客了_(:з」∠)_
打网赛打到自闭的一周,终于靠这场CF找回了一点信心...
\(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 ;
}
设\(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));
}
难点在于理解题意,题意大致就是把\(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');
}
显然在气流的开头跳是最优的,因此枚举在哪个气流的开头跳,二分求出能撑到第几个气流结束,通过预处理可以得到在一个区间内可以被抬多久,从而得出答案
#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 ;
}
显然\(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]);
}
\(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)的更多相关文章
- Codeforces Round #509 (Div. 2) F. Ray in the tube(思维)
题目链接:http://codeforces.com/contest/1041/problem/F 题意:给出一根无限长的管子,在二维坐标上表示为y1 <= y <= y2,其中 y1 上 ...
- Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)
题目链接:http://codeforces.com/contest/1041/problem/E 题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b . 题 ...
- codeforces 1041d// Glider// Codeforces Round #509(Div. 2)
题意:给出,n和飞行员高度h,n是区间数.在区间里飞行员高度不变,其它地方每秒高度-1,x坐标+1.问在高度变为0以前,x坐标最多加多少? 用数组gap记录本区间右端到下一个区间左端的距离.用sum记 ...
- Codeforces Round#509 Div.2翻车记
A:签到 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- Codeforces Round #509 (Div. 2) A. Heist 贪心
There was an electronic store heist last night. All keyboards which were in the store yesterday were ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- ubuntu文件搜索统计
一.在ubuntu下如何搜索文件 1.特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来.我一般的查找都用这条命 ...
- JAVA第一周学习
新学期伊始,六门专业课,课课重要,无法抉择重心,但日子还是要过的,而且要精细的过,不能得过且过 JAVA第一周任务 一:学习第一章视频 二:使用JDB调试JAVA 三:输入调试教材上代码,并把代码上传 ...
- [再寄小读者之数学篇](2014-04-01 from 2103471050@qq.com 曲线积分)
求 $\int_\vGa y^2\rd s$, 其中 $\vGa$ 由 $\dps{\sedd{\ba{rl} x^2+y^2+z^2&=a^2\\ x+z&=a \ea}}$ 决定. ...
- sessionStorage:写入记事本功能[内容写入sessionStorage中,读取,删除]
知识点: 1.设置sessionStorage----setItem:sessionStorage.setItem(key,data); 存储数据使用key是唯一,不可重复,每触发都生成:如用一个固定 ...
- 新手入门django本地化服务
自动化安装 #更新依赖库 pip install --upgrade setuptools #安装指定的版本 pip install Django==2.0.4 文 ...
- JavaWeb - Apache与Tomcat有什么关系和区别
总结: 1- apache是web服务器,侧重于http server: tomcat是应用(java)服务器,侧重于servlet引擎 2-合作过程详解,请看:JavaWeb - apache和to ...
- print_r print var_dump echo区别
print_r print_r(mixed $expression [,bool $true]) 显示关于一个变量的易于理解的信息,如果给出的是string/integer/float 将打印变量值本 ...
- 再说C模块的编写(1)
[前言] 在<Lua“控制”C>中对Lua调用C函数做了初步的学习,而这篇才是重中之重,这篇文章会重点的总结C模块编写过程中遇到的一些问题,比如数组操作.字符串操作和C函数的状态保存等问题 ...
- DataStructure-链表实现指数非递减一元多项式的求和
// 2-链表实现多项式的求和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> #inclu ...
- 【原创】大叔经验分享(24)hive metastore的几种部署方式
hive及其他组件(比如spark.impala等)都会依赖hive metastore,依赖的配置文件位于hive-site.xml hive metastore重要配置 hive.metastor ...