CF1119A Ilya and a Colorful Walk

  • 这题二分是假的.. \(1,2,1,2,1\) 有间隔为 \(3\) 的,但没有间隔为 \(2\) 的.开始被 \(hack\) 了一次.后来改过来了.
  • 我的做法:扫一遍,记录每个颜色第一次出现的位置,并存入一个 \(set\) 中,若当前颜色未出现,就更新位置,存入 \(set\) ,否则将那个位置删除.然后查询一次 \(set\) 中最小元素.最后再把那个位置插入回去.
  • 官方题解:最远距离两个端点中一定包含 \(1\) 或 \(n\) 中至少一个.若 \(c_i\not =c_j,1<i<j<n\) ,那么如果 \(c_1,c_n\) 不同,那么端点可以直接选 \(1,n\) ,否则若 \(c_1=c_n\) ,那么 \(c_1\not = c_j,c_i\not =c_n\) 至少有一个成立,端点也会拓展到边界上.
  • 找与 \(1​\) 颜色不同的点与 \(1​\) 的最远距离,与 \(n​\) 颜色不同的点与 \(n​\) 的最远距离取 \(\max​\) 即可.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
int n;
const int MAXN=3e5+10;
int c[MAXN];
int check(int x)
{
for(int i=1;i+x<=n;++i)
if(c[i]!=c[i+x])
return 1;
return 0;
}
int pos[MAXN];
set<int> s;
int main()
{
n=read();
s.insert(n+1);
int ans=0;
for(int i=1;i<=n;++i)
{
int x=read();
int y=pos[x];
if(y)
s.erase(y);
int p=*s.begin();
ans=max(ans,i-p);
if(!pos[x])
pos[x]=i;
if(pos[x])
s.insert(pos[x]);
}
cout<<ans<<endl;
return 0;
}

CF1119B Alyona and a Narrow Fridge

  • 这题二分是真的,能放下前 \(k\) 个显然就能放下前 \(k-1\) 个嘛.于是二分答案,判断放前 \(k\) 个是否可行.这里贪心放,最大的和次大的放在一起,后面同理,排序后两个两个放下来即可.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define mp make_pair
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
int n,h;
const int MAXN=1e3+10;
int a[MAXN];
int b[MAXN],tp=0;
bool check(int k)
{
for(int i=1;i<=k;++i)
b[i]=a[i];
sort(b+1,b+1+k);
int f=0,rs=h;
for(int i=k;i>=1;--i)
{
if(!f)
{
rs-=b[i];
if(rs<0)
return false;
}
f^=1;
}
return true;
}
int main()
{
n=read(),h=read();
int s=0;
for(int i=1;i<=n;++i)
a[i]=read();
int L=1,R=n;
int ans=0;
while(L<=R)
{
int mid=(L+R)>>1;
if(check(mid))
ans=mid,L=mid+1;
else
R=mid-1;
}
cout<<ans<<endl;
return 0;
}

CF1119C Ramesses and Corner Inversion

  • 注意到这个翻转操作只改变四个角落的值,不会改变每一行每一列的 \(1\) 个数的奇偶性.
  • 那么若有一行/列 \(A,B\) 奇偶性不同,显然不合法.
  • 否则?对于每个不同的位置 \((x,y)\) ,我们都选择它作为右下角, \((1,1)\) 作为左上角进行操作.这样所有不同的地方都被修改到一样了,而 \(A,B\) 每一行/列奇偶性都相同,多修改的部分 \((1,1),(1,y),(1,x)\) 恰好抵消, \(A\) 就变成了 \(B\) ,所以一定合法.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=512;
int a[MAXN][MAXN],b[MAXN][MAXN];
int main()
{
int n=read(),m=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
a[i][j]=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
b[i][j]=read();
for(int i=1;i<=n;++i)
{
int s1=0,s2=0;
for(int j=1;j<=m;++j)
s1+=a[i][j],s2+=b[i][j];
s1&=1,s2&=1;
if(s1!=s2)
return puts("No"),0;
}
for(int j=1;j<=m;++j)
{
int s1=0,s2=0;
for(int i=1;i<=n;++i)
s1+=a[i][j],s2+=b[i][j];
s1&=1,s2&=1;
if(s1!=s2)
return puts("No"),0;
}
puts("Yes");
return 0;
}

CF1119D Frets On Fire

  • 题面太鬼畜了...建议直接看 \(Formally\) 那一段.
  • 首先容易发现, \(s\) 数组的顺序对答案是不影响的.所以我们可以先将 \(s\) 从小到大排序.
  • 每次询问 \((l,r)\) ,对于每个 \(s_i\) ,它所贡献的数就是 \([s_i+l,s_i+r]\) 这一段,那么就等价于给了 \(n\) 条长度一样的线段,问覆盖的长度.
  • 可以考虑用整个区间 \([s_1+l,s_n+r]\) 的长度减去中间没有覆盖到的部分.容易发现这样的部分只可能出现在两条相邻线段之间,可以直接用 \(\max(s_{i+1}+l-s_i-r-1,0)\) 来计算.那么这次询问的答案就是

\[ans=s_n+r-s_1-l+1-\sum_{i=1}^{n-1} \max(s_{i+1}+l-s_i-r-1,0)\\
=s_n+r-s_1-l+1-\sum_{i=1}^{n-1} \max(s_{i+1}-s_i-(r-l+1),0)
\]

  • 记 \(b_i=s_{i+1}-s_i\) ,将 \(b_i\) 排序后记录一下前缀和,询问时二分找出 \(b_i\geq r-l+1\) 的第一个位置,只算后面的部分即可.时间复杂度 \(O(nlogn+qlogn)\) .
#include<bits/stdc++.h>
#define inf (1e18)+10
using namespace std;
typedef long long ll;
inline ll read()
{
ll out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=1e6+10;
ll n,a[MAXN];
ll b[MAXN];
ll sum[MAXN];
int main()
{
n=read();
for(int i=1;i<=n;++i)
a[i]=read();
sort(a+1,a+1+n);
for(int i=1;i<n;++i)
b[i]=a[i+1]-a[i];
sort(b+1,b+n);
for(int i=1;i<n;++i)
sum[i]=sum[i-1]+b[i];
int Q=read();
while(Q--)
{
ll l=read(),r=read();
ll s=r-l+1;
ll ans=a[n]+r-a[1]-l+1;
if(s>b[n-1])
{
cout<<ans<<' ';
continue;
}
int p=lower_bound(b+1,b+n,s)-b;
ans-=sum[n-1]-sum[p-1];
ans+=1LL*(n-p)*s;
cout<<ans<<' ';
}
return 0;
}

CF1119E Pavel and Triangles

  • 可以发现, \(2^i+2^j>2^k,i\leq j \leq k\) 有解,只可能是 \(i\leq j,k=j\) .
  • 那么直接贪心选,每次读入了 \(x\) 个 \(2^i\) ,就先尝试用两个 \(i\) 与之前剩下的任意一个 \(j\) 配对,再尝试 \((i,i,i)\) 三个配对,再将剩余的留着,后面再用.
  • 为什么要先尝试 \((i,i,i)\) 三个配对呢?因为如果不进行这一步,它在之后的步骤中每想形成一个三角形就还需要 \(2\) 个木棍构成 \((i,j,j)\) 的形式,显然劣于 \((i,i,i)\) 直接配对.
  • 官方证明.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
fh=-1,jp=getchar();
while (jp>='0'&&jp<='9')
out=out*10+jp-'0',jp=getchar();
return out*fh;
}
const int MAXN=3e5+10;
int a[MAXN];
ll ans=0,rest=0;
int main()
{
int n=read();
for(int i=1;i<=n;++i)
{
int x=read();
ll p=min(1LL*x/2,rest);
ans+=p;
rest-=p;
x-=p*2;
ans+=x/3;
x%=3;
rest+=x;
}
cout<<ans<<endl;
return 0;
}

CF1119 Global Round 2的更多相关文章

  1. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  2. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  3. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  4. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  5. [题解向] CF#Global Round 1の题解(A $\to$ G)

    这里是总链接\(Link\). \(A\) 题意:求\(\sum_{i=1}^{k} a_i\times b^{k-i}\)的奇偶性, \(k = \Theta(n \log n)\) --其实很容易 ...

  6. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  7. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  8. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

  9. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

随机推荐

  1. Codeforces 294D - Shaass and Painter Robot

    294D - Shaass and Painter Robot 思路: 可以用数学归纳法证明一个结论:整个棋盘黑白相间当且仅当边缘黑白相间. 分奇偶讨论又可得出边缘黑色格个数为n+m-2 这样就可以暴 ...

  2. rsync+inotify文件同步 - 同步慢的问题

    rsync+inotify文件同步 - 同步慢的问题 我们来看网上的教程,我加了注释.(网上所有的教程基本都一模一样,尽管写法不一样,致命点都是一样的) #!/bin/bash /usr/bin/in ...

  3. 20 多继承 MRO 算法 深度优先遍历 super

    类的多继承 一个类可以继承多个无关的类. 一个类可以被多个无关的类继承 1.经典类. 在python2.2之前. 已经是历史了. MRO 采用的是树形结构的深度递归遍历(一条道跑到黑) 2.新式类 在 ...

  4. SQL语句增加列、修改列类型、修改列、删除列

    1.增加列: alter table tableName add columnName varchar(30) 2.修改列类型: alter table tableName alter column ...

  5. JVM笔记(三) 垃圾收集器(2)收集算法

    垃圾收集器2:收集算法 主要通过阅读<深入了解Java虚拟机>(周志明 著)和网络资源汇集而成,为本人学习JVM的笔记.同时,本文理论基于JDK 1.7版本,暂不考虑 1.8和1.9 的新 ...

  6. 比较强大 优秀的开源框架 :Android图片加载与缓存:Android Glide 的用法

    使用Android Glide,需要先下载Android Glide的库,Android Glide在github上的项目主页: https://github.com/bumptech/glide . ...

  7. TADOTABLE 永久字段的顺序 和 AppendRecord

    AppendRecord 方法,添加记录的字段到数据库里时,是按照IDE里永久字段的顺序,不是数据库表里的字段顺序. 自动编号 字段,以nil为值. 日期时间 字段,直接now 写法

  8. Fedora 安装与常用命令

    fedora下载地址: https://getfedora.org/ 原来装的26,现在装27 用的是server版网络安装500多M 现在都是U盘安装了,fedora的写优盘说明 https://f ...

  9. Solr增删改查索引

    一.添加索引,提交文档 1.如图,我的xml文档有predicate.object字段,这些在Solr配置文档里没有,所以xml文档提交不了 2.在F:\solr-4.10.0\example\sol ...

  10. 使用pool的多进程,不执行的问题

    from multiprocessing import Pool def fetch_data(idlist,test): pass p=Pool(4) result=[] for i in rang ...