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. vscode python3 配置生成任务

    一直用sublime,但是ubuntu下输入中文有问题,解决起来太麻烦: pycharm太重.虚拟机一开+Chrome打开10几个页面,然后再运行pycharm,静音轻薄笔记本CPU和8G内存基本都占 ...

  2. git问题解决(摘录)

    https://blog.csdn.net/dxk539687357/article/details/54629274

  3. 一个Java例子,解释清楚注解的作用

    原文出处:码农登陆 写在前面 今天聊的是注解,但其实单纯说注解,注解本身没有任何的作用.简单说和注释没啥区别,而它有作用的原因是:注解解释类,也就是相关对代码进行解释的特定类.一般这些类使用反射是可以 ...

  4. 微信小程序跳到h5,h5在跳回小程序

    1.在微信小程序后台: 设置->开发设置->业务域名: 添加业务逻辑域名 2.在html5页面添加如下代码: <! -- html --> < script type=& ...

  5. 探索Bioconductor数据包

    参考: R的bioconductor包TxDb.Hsapiens.UCSC.hg19.knownGene详解 Bioconductor的数据包library(org.Hs.eg.db)简介

  6. codeforces 497c//Distributing Parts// Codeforces Round #283(Div. 1)

    题意:有n个区间[ai,bi],然后有n个人落在[ci,di],每个人能用ki次.问一种方式站满n个区间. 两种区间都用先x后y的升序排序.对于当前的区间[ai,bi],将ci值小于当前ai的全部放入 ...

  7. Confluence 6 管理多目录概述

    这里是有关目录顺序如何影响处理流程: 目录中的顺序是被用来如何查找用户和组的顺序. 修改用户和用户组将会仅仅应用到应用程序具有修改权限的第一个目录中. 配置目录载入顺序 你可以修改在 Confluen ...

  8. python-day53--前端js

    一.基本语法(ECMA) 单行注释 // /* 多行注释 */ 变量赋值 默认以换行符作为结束符,有分好以分号作为结束符号 JS的引入方式: 1. <script> </script ...

  9. 根据id来实现小程序tab切换,

    本例根据绑定id来实现tab切换,但本例仍有缺陷,用for循环数据,无法实现切换.如有大神能够有更好方法,欢迎留言更正 WXML: <view class="tab"> ...

  10. HDU 3697贪心

    额...大意是你可以决定什么时候选课.然后呢.每五分钟只有一次机会选.每种课限制选课时间.问你能选到的课最多有多少. 感觉一点都不水.是自己太菜了吗? #include<stdio.h> ...