Codeforces Round 987 (Div. 2)
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\),这样剩下的位置就是偶数个,比较好构造了。下面给出一种构造方案:
\]
后面就按偶数的接下去就行了。
#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)的更多相关文章
- Codeforces Round #485 (Div. 2)
Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...
- Codeforces Round #485 (Div. 2) D. Fair
Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...
- Codeforces Round #485 (Div. 2) C. Three displays
Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...
- Codeforces Round #485 (Div. 2) A. Infinity Gauntlet
Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...
- 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连试试水的深浅..... ...
随机推荐
- win指令学习收集
要执行多句,需要用到逻辑判断 1 & 2 1成不成功都会执行2 1 && 2 1成功才会执行2 1 || 2 1不成功才会执行2
- 2024-08-28:用go语言,给定一个从1开始、长度为n的整数数组nums,定义一个函数greaterCount(arr, val)可以返回数组arr中大于val的元素数量。 按照以下规则进行n次
2024-08-28:用go语言,给定一个从1开始.长度为n的整数数组nums,定义一个函数greaterCount(arr, val)可以返回数组arr中大于val的元素数量. 按照以下规则进行n次 ...
- 【Python】之pip安装报错问题:WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))
我想安装python第三方库,但是始终报安装失败 经查询得知,需要国内源,有如下地址: 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https: ...
- 17 Python异常处理(捕获异常、抛出异常、自定义异常)
本篇是 Python 系列教程第 17 篇,更多内容敬请访问我的 Python 合集 当我们编写代码时,可能会遇到各种各样的错误情况,比如除数为零.找不到文件.网络问题等等.为了优雅地处理这些问题,P ...
- Google Maps Embed API & JavaScript API
前言 很多年前写过一篇 Google Map 谷歌地图, 这篇算是翻新版本. Google Map Registration Google Maps Platform 是整个 Google Map 的 ...
- Azure – WAF (Web Application Firewall)
前言 最近有客户想购买 Azure 的 Web Application Firewall (WAF), 来防 SQL Injection, XSS 攻击. 一开始我是觉得没什么必要, 毕竟什么年代了, ...
- Figma 学习笔记 – Team Library Style and Component
Design System 我们做设计通常会 Follow 一个 Design System, 比如 Material Guide. 里头会定义 Font, Color, Effect (Elevat ...
- BFS 颜色填涂———洛谷p1162
填涂颜色 题目描述 由数字 \(0\) 组成的方阵中,有一任意形状的由数字 \(1\) 构成的闭合圈.现要求把闭合圈内的所有空间都填写成 \(2\).例如:\(6\times 6\) 的方阵(\(n= ...
- 原生CSS、HTML 和 JavaScript 实现酷炫表单
一直使用 Vue/React ,习惯了用组件,偶尔想用原生三剑客写点 Demo 发现样式丑的不忍直视.最近看 掘金小册<玩转CSS的艺术之美>看到 CSS 相关的内容,发现原生 CSS 也 ...
- 【ARMv8】异常级别的定义EL0、EL1、EL2、EL3
Exception levels ARMv8-A系列定义了一系列的异常等级,从EL0到EL3,下面具体说明其含义: ELn中,随着n的增加,软件的执行权限也相应的增加: EL0被称为无特权执行: EL ...