题目链接:https://codeforces.com/contest/978/problem/D

题解:

  题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对于给定的数列,公差的可能性是已知的。

  我的解决思路是:对于给定的数列,最后一位数 - 第一位数 / (n -1) 必定是公差,而对于我们要判断的原数列,其最后一位 - 第一位数的值是可以-2,-1,0,+1,+2的,穷举可能性一切公差,找出变动最小的情况。代码如下(有点小复杂,没优化):

  

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = +;
int ini[N];
int ans[N];
int tofopr;
set<int> jg;
int isok(int n,int newc){
int c = ans[];
int t = ;
for(int i = ;i<=n - ;i++){
c+= newc;
if(abs(ini[i] - c) > ) return -;
if(ini[i] != c){
t++;
}
}
return t;
} int main(void){
int n;
jg.clear();
scanf("%d",&n);
if(n <= ){
int b;
for(int i = ;i<=n;i++){
scanf("%d",&b);
}
printf("0\n");
}
else{
int b;
for(int i = ;i<=n;i++){
scanf("%d",&b);
ini[i] = b;
}
int c = ini[n] - ini[];
int start,end;
for(int i = c -;i<=c+;i++){
tofopr = ;
if(i%(n-) == ){
int newc = i / (n - );
if(i == c - ){
start = ini[] + ;
end = ini[n] - ;
if(end >= ){
ans[] = start;
ans[n] = end;
tofopr = isok(n,newc);
if(tofopr >= ){
tofopr += ;
jg.insert(tofopr);
}
}
}
if(i == c -){
start = ini[];
end = ini[n] - ;
if(end >= ){
ans[] = start;
ans[n] = end;
tofopr = isok(n,newc);
if(tofopr >= ){
tofopr += ;
jg.insert(tofopr);
}
}
start = ini[] + ;
end = ini[n];
ans[] = start;
ans[n] = end;
tofopr = isok(n,newc);
if(tofopr >= ){
tofopr += ;
jg.insert(tofopr);
}
}
if(i == c){
start = ini[];
end = ini[n];
ans[] = start;
ans[n] = end;
if(tofopr >= ){
tofopr = isok(n,newc);
jg.insert(tofopr);
}
}
if(i == c + ){
start = ini[] -;
end = ini[n];
if(start >= ){
ans[] = start;
ans[n] = end;
tofopr = isok(n,newc);
if(tofopr >= ){
tofopr += ;
jg.insert(tofopr);
} }
start = ini[];
end = ini[n]+;
ans[] = start;
ans[n] = end;
tofopr = isok(n,newc);
if(tofopr >= ){
tofopr += ;
jg.insert(tofopr);
}
}
if(i == c + ){
start = ini[] - ;
end = ini[n] + ;
if(start >= ){
ans[] = start;
ans[n] = end;
tofopr = isok(n,newc);
if(tofopr >= ){
tofopr += ;
jg.insert(tofopr);
}
}
} } }
if(jg.empty()){
printf("-1");
}
else
{
auto it = jg.begin();
printf("%d",*it);
}
}
return ;
}

  而神犇的代码如下,思路是一致的,但是神犇找最小的方法很特殊!代码如下:

 

#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + ;
typedef long long ll; ll n, num[maxn]; void init() {
scanf("%lld", &n);
for (int i = ; i < n; i++)
scanf("%lld", &num[i]);
} ll get_ans(ll tol) {
ll cnt[],st;
cnt[] = cnt[] = cnt[] = ;
for(int i=;i<;i++) {
if(i == ) st = num[]-;
else if(i == ) st = num[];
else st = num[] + ; for (ll j = ; j < n; j++) {
if(abs(num[j]-st) > ) {
cnt[i] = INT_MAX;
break;
}
cnt[i] += abs(num[j] - st);
st += tol;
}
}
if(cnt[] == cnt[] && cnt[] == cnt[] && cnt[] == INT_MAX) //无法组成等差数列返回-1
return -;
return min(cnt[], min(cnt[], cnt[]));
} int main() {
init();
ll Min = INT_MAX,tol;
tol = num[]-num[];
ll ans = get_ans(tol);
if(ans >= )
Min = min(Min, ans);
for (int i = ; i <= ; i++) {
ans = get_ans(tol-i);
if (ans >= )
Min = min(Min, ans); ans = get_ans(tol+i);
if (ans >= )
Min = min(Min, ans);
} if (Min == INT_MAX)
printf("-1");
else
printf("%lld", Min); return ;
}

CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression的更多相关文章

  1. CF刷题-Codeforces Round #481-G. Petya's Exams

    题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...

  2. CF刷题-Codeforces Round #481-F. Mentors

    题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. 水题 Codeforces Round #302 (Div. 2) A Set of Strings

    题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...

  5. 水题 Codeforces Round #300 A Cutting Banner

    题目传送门 /* 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 判断是否余下的是 "CODEFORCES" :) */ #include <cs ...

  6. 水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas

    题目传送门 /* 很简单的水题,晚上累了,刷刷水题开心一下:) */ #include <bits/stdc++.h> using namespace std; ][] = {" ...

  7. 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

    题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...

  8. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  9. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

随机推荐

  1. 递归根据父ID 找所有子类ID

    function getinfo($pid){ $str = ''; $row = M('user')->where(array('pid'=>$pid))->select(); i ...

  2. Linux系统的环境变量$PATH

    $PATH:决定了shell将到哪些目录中寻找命令或程序,PATH的值是一系列目录,当您运行一个程序时,Linux在这些目录下进行搜寻编译链接. 修改$PATH的方法有很多,比如: export PA ...

  3. linux的虚拟文件系统VFS

    虚拟文件系统(virtual file system),别名虚拟文件系统开关,是linux中的一个软件层,向用户空间提供文件系统操作接口. VFS包含的系统调用包括open(2).stat(2).re ...

  4. SQL引用DAL

    步骤:https://www.cnblogs.com/grom/articles/8981116.html 异常: 解决方案: 执行 ALTER DATABASE [DBName] SET TRUST ...

  5. Oracle特殊查询 行列倒转 分页

    --查询工资最高的前三名 (分页的感觉)select * from(select * from emp order by sal desc) twhere rownum <=3--查询工资最高的 ...

  6. zookeeper报错 JAVA_HOME is not set

    很多开发者安装zookeeper的时候,应该会发现到这么一个问题: JAVA_HOME is not set 好的!那么这个是什么意思呢? 就是说你的  JAVA_HOME 变量没有设定 为什么会提示 ...

  7. poj1733 Parity Game(扩展域并查集)

    描述 Now and then you play the following game with your friend. Your friend writes down a sequence con ...

  8. 『ACM C++』 PTA 天梯赛练习集L1 | 018-020

    终于一周有这么一天能够安静下来好好学习打打题,还是很美滋滋的哈哈~加油加油~ ------------------------------------------------L1-018------- ...

  9. MySQL学习【第九篇存储引擎】

    一.存储引擎介绍 1.我们知道mysql程序构成由连接层,sql层,存储引擎层.存储引擎层和磁盘进行交互,由其去取数据,而我们取得数据是表的形式展现出来,谁做的呢?就是存储引擎结构化成表的形式返回给用 ...

  10. [修正] Firemonkey SpeedButton 鼠标移开按钮后 IsPressed 为 False 的问题

    未修正: 修正代码: 请将 FMX.StdCtrls.pas 复制到自己的工程目录下,再修改如下代码: procedure TCustomButton.RestoreButtonState; begi ...