【Codeforces Round #446 (Div. 2) C】Pride
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的。
则枚举那一段在哪个地方。
我们设这一段中所有的数字都做了一次gcd.
假设在i..j这一段。
则求gcd的顺序是(i,i+1),(i+1,i+2)...(j-1,j)
这样a[j]=gcd(a[i],a[i+1]..a[j])了
即顺序求了一遍gcd.
这样,预处理一下i到j的gcd.
如果gcd[i][j]==1,则获取到一个可行方案。
求一下次数的最小值就好了
即(j-i)*2 + i-1 + n-j
当然,如果已经有1了,就没必要去凑一个1出来了。
直接枚举从1开始向左、向右就好。
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3;
int a[N+10],n;
int f[N+10][N+10];
int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
scanf("%d",&n);
for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
int ans = -1;
//have 1
for (int i = 1;i <= n;i++)
if (a[i]==1){
int cnt = 0;
for (int j = i-1;j >= 1;j--)
if (a[j]!=1){
cnt++;
}
for (int j = i+1;j <= n;j++)
if (a[j]!=1){
cnt++;
}
if (ans==-1 || ans > cnt) ans = cnt;
}
int temp = a[1];
for (int i = 2;i <=n;i++) {
temp = __gcd(temp,a[i]);
}
if (temp!=1){
puts("-1");
return 0;
}
for (int i = 1;i <= n;i++){
f[i][i] = a[i];
for (int j = i+1;j <= n;j++){
f[i][j] = __gcd(f[i][j-1],a[j]);
}
}
for (int i = 1;i <= n;i++){
for (int j = i;j <= n;j++)
if (f[i][j]==1){
int temp = (j-i)*2 + (i-1) + (n-j);
if (ans==-1){
ans = temp;
}else ans = min(ans,temp);
}
}
printf("%d\n",ans);
return 0;
}
【Codeforces Round #446 (Div. 2) C】Pride的更多相关文章
- 【Codeforces Round #446 (Div. 2) B】Wrath
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...
- 【Codeforces Round #446 (Div. 2) A】Greed
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心选容量大的瓶子就好 [代码] #include <bits/stdc++.h> #define int long l ...
- 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
[链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
- 【Codeforces Round #423 (Div. 2) B】Black Square
[Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...
- 【Codeforces Round #423 (Div. 2) A】Restaurant Tables
[Link]:http://codeforces.com/contest/828/problem/A [Description] 有n个组按照时间顺序来餐馆; 每个组由一个人或两个人组成; 每当有一个 ...
随机推荐
- Linux常用视频播放器
1.SMplayer是一款跨平台的视频播放工具,可以支持大部分的视频和音频文件.它支持音频轨道切换.允许调节亮度.对比度.色调.饱和度.伽玛值,按照倍速.4倍速等多种速度回放.还可以进行音频和字幕延迟 ...
- 翻译《虚幻引擎4艺术大师 - 蓝图 III 》 中文版
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/49800063 作者:c ...
- Linux学习总结(7)——阿里云centeros服务器上安装 jdk,tomcat,mysql
查看服务器的系统版本 # cat /etc/issue 查看服务器是64位还是32位 #uname -a 或者用:#getconf LONG_BIT 查看当前有没有安装jdk #rpm -q ...
- [Python] Read and Parse Files in Python
This lesson will teach you how to read the contents of an external file from Python. You will also l ...
- shell学习四十九天----进程建立
进程 前言:进程指的是运行中程序的一个实例.新进程由fork()与execve()等系统调用所起始,然后运行,知道他们下达exit()系统调用为止. linux系统都支持多进程.尽管计算机看起来像是一 ...
- /dev/shm和swap差别与联系
1.基本理论 /dev/shm这个文件是寄生虫,寄存在内存中 swap是暂时在硬盘中划分一个区域,把它作为内存使用 2.怎样查看 使用df -lh能够查看/dev/shm 使用free -m能够查看s ...
- LinkedIn Cubert 实践指南
· LinkedIn Cubert安装指南 · Understanding Cubert Concepts(一)Partitioned Blocks · Understanding Cubert Co ...
- Android加载大图不OOM
首先,我们试着往sdcard里放一张400k的图片,但是分辨率是2560*1600 布局简单 <?xml version="1.0" encoding="utf-8 ...
- Linux下使用网上银行
Linux下使用网上银行 650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpi ...
- IFC数据模式架构的四个概念层
IFC模型体系结构由四个层次构成, 从下到上依次是 资源层(Resource Layer).核心层(Core Layer).交互层(Interoperability Layer).领域层(Domain ...