Codeforces Round 987 (Div. 2) 总结

A

常见的套路,将一个序列变为不下降序列所需要改变的值的最小数量,考虑最大能保留多少个,显然是求最长上升子序列,而这题给出的 \(a\) 序列保证不上升,所以只需要考虑相同长度的一段。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map> using namespace std;
typedef long long ll;
const int N=55;
int n;
int a[N];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int ans=0,len=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i-1]) ans=max(ans,len),len=1;
else len++;
}
ans=max(ans,len);
cout<<n-ans<<'\n';
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--) solve();
return 0;
}

B

考虑每个数 \(p_i\) 能否移动到 \(i\) 位置。

首先能交换的值只有 \(p_i-1\) 和 \(p_i+1\),显然不能连续移动两次,不然比 \(p_i\) 大或小 \(1\) 的数一定不会到该到的位置。因此最多交换一次。再看是否能交换到自己想要的位置,如果有一个不能,那就不可行。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map> using namespace std;
typedef long long ll;
const int N=2e5+5;
int n;
int a[N];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int st=1;
for(int i=1;i<=n;i++)
{
if(a[i]<i&&!(i-a[i]==1&&a[a[i]]==a[i]+1)) st=0;
else if(a[i]>i&&!(a[i]-i==1&&a[a[i]]==a[i]-1)) st=0;
}
if(st) cout<<"Yes\n";
else cout<<"No\n";
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--) solve();
return 0;
}

C

构造题,思路还是比较清晰的。

首先不难想到 \(1,1,2,2,3,3,\dots\) 这样的情况,所以 \(n\) 为偶数时一定成立。

再考虑奇数,由于任意两个相同的馅料之间的距离都要是完全平方数,考虑三个相同的,位置为 \(x,y,z\),满足 \(y-x=a^2\),\(z-y=b^2\),\(z-x=c^2\),且 \(a,b,c\) 都为正整数,因此有 \(c^2=a^2+b^2\)。

考虑最小的勾股数 \(3,4,5\)。令 \(x=1,y=10,z=26\),这样剩下的位置就是偶数个,比较好构造了。下面给出一种构造方案:

\[ 1,2,2,3,3,4,4,5,5,1,6,6,7,7,8,8,9,9,10,10,11,11,12,13,13,1,12,\dots
\]

后面就按偶数的接下去就行了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map> using namespace std;
typedef long long ll;
const int N=2e5+5;
int n;
int a[N];
void solve()
{
cin>>n;
if(n%2==0)
{
int cnt=1;
for(int i=1;i<=n;i+=2) a[i]=a[i+1]=++cnt;
}
else
{
if(n>=27)
{
a[1]=a[10]=a[26]=1;
int cnt=1;
for(int i=2;i<=8;i+=2) a[i]=a[i+1]=++cnt;
for(int i=11;i<=21;i+=2) a[i]=a[i+1]=++cnt;
a[23]=a[27]=++cnt;
a[24]=a[25]=++cnt;
for(int i=28;i<=n;i+=2) a[i]=a[i+1]=++cnt;
}
else
{
cout<<-1<<'\n';
return ;
}
}
for(int i=1;i<=n;i++) cout<<a[i]<<' ';
cout<<'\n';
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--) solve();
return 0;
}

D

赛时的清奇想法。

首先发现能跳跃的两个位置是逆序对,因此考虑用并查集维护,并记录集合内最大值与最小值。

再考虑这样一种做法,先遍历一遍数组,目前遇到的最大值为 \(x\),下标为 \(id\),加入一个数 \(a_i\)。

  • 若 \(a_i>=x\) 更新 \(x\) 和 \(id\)。
  • 若 \(a_i<x\) 那么就合并 \(i\) 和 \(id\)。

以最后一组样例为例:

用 \(mx_i\) 表示集合 \(i\) 中最大的 \(a_i\),\(mi_i\) 表示最小值。

然后考虑合并不同集合,从后往前,如果出现两个不同集合 \(i,j\) 且 \(i<j\)。由前面的过程易知 \(mx_i \le mx_j\),如果 \(mx_i>mi_j\),就说明这两个集合可以合并。

那有没有可能出现不是相邻的集合合并呢?答案是否定的,考虑 \(k,i,j\) 三个集合,\(mx_k \le mx_i \le mx_j\),如果 \(i\) 与 \(j\) 不能合并,则 \(mx_i \le mi_j\),就会有 \(mx_k \le mx_i \le mi_j\),显然 \(k\) 与 \(j\) 不能合并。因此每个集合都只能和相邻的集合合并。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map> using namespace std;
typedef long long ll;
const int N=5e5+5;
int n;
int a[N],ans[N];
int fa[N],mi[N],mx[N];
int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void merge(int x,int y)
{
x=fa[x],y=fa[y];
fa[y]=x;
mi[x]=min(mi[x],mi[y]);
mx[x]=max(mx[x],mx[y]);
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
fa[i]=i;
mi[i]=mx[i]=a[i];
}
int x=a[1],id=1;
for(int i=2;i<=n;i++)
{
if(a[i]<x) merge(id,i);
else x=a[i],id=i;
} for(int i=n-1;i>=1;i--)
if(find(i)!=find(i+1)&&mi[find(i+1)]<mx[find(i)])
merge(i,i+1);
for(int i=1;i<=n;i++) cout<<mx[find(i)]<<' ';
cout<<'\n';
}
int main ()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--) solve();
return 0;
}

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

  1. Codeforces Round #485 (Div. 2)

    Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...

  2. Codeforces Round #485 (Div. 2) D. Fair

    Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...

  3. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  4. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  5. Codeforces Round #485 (Div. 2) C. Three displays

    Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...

  6. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet

    Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...

  7. 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 ...

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

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

  9. Codeforces Round #368 (Div. 2)

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

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

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

随机推荐

  1. MSYS2、MinGW、Cygwin 关系梳理

    还记得大一刚开始写 C 代码时,经常看到 MSYS2.MinGW.Cygwin 等名词.对于第一次接触编程的我来说这些名词让我眼花缭乱.当时查阅了一些资料,但是对于这些名词的解释始终让我云里雾里.现在 ...

  2. 操作 JAR 文件

    列出 JAR 文件内容 使用 jar 命令来列出 JAR 文件的内容: jar tf myapp.jar -t 选项表示列出文件,-f 表示指定 JAR 文件. 解压 JAR 文件 使用 jar 命令 ...

  3. Python 项目及依赖管理工具技术选型

    Python 项目及依赖管理工具,类似于 Java 中的 Maven 与 Node 中的 npm + webpack,在开发和维护项目时起着重要的作用.使用适当的依赖管理工具可以显著提高开发效率,减少 ...

  4. chroot 整理

    chroot 是什么? 就是change root directory ,比如默认是 /, 可以用这个chroot 把 / 换成其他指定的目录 chroot 干什么的? 增加了系统的安全性,限制了用户 ...

  5. Dell R920 服务器iDrac口默认账号密码和IP

    Dell服务器iDrac口默认账号密码和IP   账号:root 密码:calvin IP:192.168.0.120/24

  6. JavaScript – XMLHttpRequest

    前言 XMLHttpRequest 是 JavaScript built-in 的一个 class,用于发送 HTTP 请求,俗称 AJAX. 这几年 XMLHttpRequest 已经逐渐被 Fet ...

  7. Fluent Builder 模式

    前言 以前最讨厌设计复杂方法调用, 就是那种需要一堆有逻辑规则的 config 作为参数的方法. 这种 config 通常是一个大对象, 有许多 property, property 之间有存在一些逻 ...

  8. ASP.NET Core Library – scriban (Template Engine)

    前言 有些项目会需要让 end user 写模板 (rich text) 同时又需要做一些 data binding. 这几乎是 programmer 的工作了... 在 C#, 大可以使用 Razo ...

  9. foobar2000 v2.1.4 汉化版(更新日期:2024.04.24)

    foobar2000 v2.1.4 汉化版 -----------------------[软件截图]---------------------- -----------------------[软件 ...

  10. 【ZZ】Linux 安装 edge 浏览器

    For Debain/Ubuntu/Deepin etc.## Setupcurl https://packages.microsoft.com/keys/microsoft.asc | gpg -- ...