• 题意:有一个长度为\(n\)的序列,可以对所有元素++或--,求最少的操作次数,如果无论如何都不能构成,则输出\(-1\).

  • 题解:一个等差数列一定由首项\(a_{1}\)和公差\(d\)来决定,而这两项可以有\(a_{1}\)和\(a_{2}\)来决定,所以我们可以直接暴力枚举\(a[1]\)和\(a[2]\),一共\(9\)种情况,每次向后枚举,判断每个位置上的数是否能得到,然后更新最小值即可.

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<long,long> PLL; int n;
    int d;
    int a[N],b[N]; int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;++i) cin>>a[i]; int ans=INF;
    for(int i=-1;i<=1;++i){
    for(int j=-1;j<=1;++j){
    b[1]=a[1]+i;
    b[2]=a[2]+j;
    d=b[2]-b[1];
    bool ok=1;
    int cnt=abs(i)+abs(j);
    for(int k=3;k<=n;++k){
    b[k]=b[k-1]+d;
    if(abs(b[k]-a[k])>1){
    ok=0;
    break;
    }
    else if(b[k]!=a[k]) cnt++;
    }
    if(ok)
    ans=min(ans,cnt);
    }
    }
    if(ans==INF) puts("-1");
    else printf("%d\n",ans); return 0;
    }

Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression (暴力)的更多相关文章

  1. Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression

    http://codeforces.com/contest/978/problem/D 题目大意: 给你一个长度为n的b(i)数组,你有如下操作: 对数组中的某个元素+1,+0,-1.并且这个元素只能 ...

  2. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  3. Codeforces Round #481 (Div. 3)

    我实在是因为无聊至极来写Div3题解 感觉我主要的作用也就是翻译一下题目 第一次线上打CF的比赛,手速很重要. 这次由于所有题目都是1A,所以罚时还可以. 下面开始讲题 A.Remove Duplic ...

  4. Codeforces Round #481 (Div. 3)题解

    成功掉到灰,真的心太累了,orz!!!!,不是很懂那些国外大佬为什么每次都是20多分钟AK的,QAQ A. Remove Duplicates time limit per test 1 second ...

  5. Codeforces Round #334 (Div. 2) D. Moodular Arithmetic 环的个数

    D. Moodular Arithmetic Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/60 ...

  6. Codeforces Round #481 (Div. 3) G. Petya's Exams

    http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...

  7. Codeforces Round #334 (Div. 1) B. Moodular Arithmetic

    B - Moodular Arithmetic 题目大意:题意:告诉你p和k,其中(0<=k<=p-1),x属于{0,1,2,3,....,p-1},f函数要满足f(k*x%p)=k*f( ...

  8. Codeforces Round #481 (Div. 3) 全题解

    A题,题目链接:http://codeforces.com/contest/978/problem/A 解题心得:题意就是让你将这个数列去重,重复的数只保留最右边的那个,最后按顺序打印数列.set+m ...

  9. Codeforces Round #481 (Div. 3) C. Letters

    题目地址:http://codeforces.com/contest/978/problem/C 题解:有n个宿舍,每个宿舍人不一样多,有m封信,每封信送给对应的第m间房间,问这封信是给第几个宿舍,第 ...

随机推荐

  1. Centos 7 杂章

    CentOS-7-x86_64-DVD-2003.iso 下载地址: http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-DV ...

  2. 【Docker】在Linux系统中安装Docker虚拟机、启动停止重启查看Docker命令

    安装Docker 文章目录 安装Docker Docker虚拟机架构 在Linux系统中安装Docker虚拟机 管理Docker虚拟机 参考资料 Docker虚拟机架构 Docker创建的所有虚拟实例 ...

  3. 【Linux】快速创建文件的命令方法

    [root@centos7 dir1]# ll total 0 -rw-r--r-- 1 root root 0 Aug 15 02:39 file1 -rw-r--r-- 1 root root 0 ...

  4. 【RAC】10grac添加节点,详细步骤

    RAC物理结构 现在的RAC环境是二个节点: dbp,dbs, 这个实验就是添加节点dbi. dbp,dbs和dbi节点的信息规划如下: 服务器主机名 dbp dbs dbi 公共IP地址(eth0) ...

  5. 【ORA】ORA-27101快速处理方法

    今天朋友的数据库出了问题,报错如下: 这个问题主要是是spfile和pfile文件不一致导致的, 生成一个pfile,完了用pfile启动数据库即可 SQL> create pfile '/ho ...

  6. SSTI

    最牛bypass:https://blog.csdn.net/solitudi/article/details/107752717 SSTI的奇怪绕过姿势:https://blog.csdn.net/ ...

  7. PAT Advanced 1003 Emergency 详解

    题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...

  8. MySQL索引性能分析

    为什么要做性能分析 你有没有这样的情况. 面对一个你没怎么写过的.复杂的业务,你构思了很久,终于开始敲下了第一段代码. 写的过程迷迷糊糊,有的时候还能把自己搞晕了. 但你还是终于把它写完了. 但是点击 ...

  9. 利用容器逃逸实现远程登录k8s集群节点

    某天, 某鱼说要吃瞄, 于是...... 李国宝:边缘计算k8s集群SuperEdge初体验 ​ zhuanlan.zhihu.com 图标 照着上一篇文章来说,我这边边缘计算集群有一堆节点. 每个节 ...

  10. Python-Flask搭建Web项目

    最近因项目需要,学习了用flask搭建web项目,以下是自己的使用感悟 Flask框架结构 static:存储一些静态资源 templates:存储对应的view app.py:涉及到页面的跳转,以及 ...