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连试试水的深浅..... ...
随机推荐
- LaTeX 交叉引用的四次编译
编译包含交叉引用的 LaTeX 文件需要编译四次(pdflatex + bibtex + pdflatex * 2),一直对这四次编译都干了什么事很好奇.这次就来看一下每一步具体都干了些什么. 源文件 ...
- web 3d 技术预研及数据可视化技术
web 3D 技术 白纸一张,先理一理概念: webGL 是low level 库,three.js 是high level 库,一般只要理解webGL里的概念,实际用thee.js 开发更容易 3D ...
- SpringBoot兼容SpringMVC带有.do后缀的请求
背景 MVC框架请求的都是.do后缀,但controller控制层拦截的是没有后缀的链接.如controller请求/111/222,当请求/111/222.do时,可以正常进入.当我们将存量一些旧工 ...
- linux 环境中cat命令进行关键字搜索
在linux环境中通过关键字搜索文件里面的内容 1.显示文件里匹配关键字那行以及上下50行 cat 文件名 | grep -C 50 '关键字' 2.显示关键字及前50行 cat 文件名 | grep ...
- 小tips:vue2中broadcast和dispatch的实现
/* * broadcast 事件广播 * @param {componentName} 组件名称 * @param {eventName} 事件名 * @param {params} 参数 * 遍历 ...
- webpack系列-externals配置使用(CDN方式引入JS)
如果需要引用一个库,但是又不想让webpack打包(减少打包的时间),并且又不影响我们在程序中以CMD.AMD或者window/global全局等方式进行使用(一般都以import方式引用使用),那就 ...
- Java怎么把多个对象的list的数据合并
1.示例一:创建几个包含Person对象的List,并将它们合并成一个新的List 在Java中,将多个对象的List合并通常涉及到遍历这些List并将它们的元素添加到一个新的List中.这里,我将给 ...
- 十五,Spring Boot 整合连接数据库(详细配置)
十五,Spring Boot 整合连接数据库(详细配置) @ 目录 十五,Spring Boot 整合连接数据库(详细配置) 最后: JDBC + HikariDataSource(Spring Bo ...
- 使用pxe安装ARM服务器(鲲鹏920)遇到的坑
一.关于PXE获取到IP之后无ACK,无法获取引导文件. 目前ARM服务器基本都是使用UEFI的方式进行引导,我们只需要关注EFI方式引导即可,Legacy引导已经随着时代的发展被扫进历史的垃圾桶. ...
- 彻底解决 user.config 文件损坏
症状见 发生 Configuration system failed to initialize 错误的一个特例 解决的办法,在去读 user.settings 之前捕获错误,比如 Main() 里面 ...