CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression
题目链接: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的更多相关文章
- CF刷题-Codeforces Round #481-G. Petya's Exams
题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...
- CF刷题-Codeforces Round #481-F. Mentors
题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
- 水题 Codeforces Round #300 A Cutting Banner
题目传送门 /* 水题:一开始看错题意,以为是任意切割,DFS来做:结果只是在中间切出一段来 判断是否余下的是 "CODEFORCES" :) */ #include <cs ...
- 水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas
题目传送门 /* 很简单的水题,晚上累了,刷刷水题开心一下:) */ #include <bits/stdc++.h> using namespace std; ][] = {" ...
- 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...
- 水题 Codeforces Round #303 (Div. 2) A. Toy Cars
题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...
- 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...
随机推荐
- .net根据经纬度获取地址(百度api)
private string GetAddress(string lng, string lat) { try { string url = @"http://api.map.baidu.c ...
- 近十年one-to-one最短路算法研究整理
前言:针对单源最短路算法,目前最经典的思路即标号算法,以Dijkstra算法和Bellman-Ford算法为根本演进了各种优化技术和算法.针对复杂网络,传统的优化思路是在数据结构和双向搜索上做文章,或 ...
- PAT——1048. 数字加密
本题要求实现一种数字加密方法.首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10.Q代表11.K代 ...
- nRF5 SDK for Mesh(三) Installing the mesh toolchain 安装编译工具链
Installing the mesh toolchain To build the example applications, a toolchain based on either CMake o ...
- Python入门语法
Python入门语法 动态变量 a=3 整数 a='abc' a="abc" 字符串 a=3.0 小数 a=true a=false 布尔型 a=3 ...
- eclipse中svn插件装好后出现"位置错误"的处理
错误现象: 验证位置时发生错误:"org.apache.subversion.javahl.ClientException:svn:E210004: Number is larger tha ...
- OO课程总结
OO课程总结 OO终于划上了句号,这学期熬夜熬得最多的一门课,掉了最多头发的一门课. 一.测试与正确性 测试是最最最常见的用来找到程序错误以及验证程序正确的手段,在之前的作业中,写完代码还是会存在很多 ...
- node引入bootstrap npm报错
今天node引入bootstrap npm报错 但是页面正常显示 最后发现bootstrap.min.js.map没有放在文件里 虽然不用页面中引入 另外也发现了怎么看这种错误了
- 实施erp的建议
纺织行业实施ERP建议 (一)企业各层面应提高对ERP的认识 ERP项目的实施范围横跨企业的每一个部门,在实施过程中需要调动各个部门的资源,这首先需要企业领导者高度重视,从实施的各个环节给予支持:其次 ...
- IDEA导入eclipse项目并部署到tomcat
1.首先引入本地项目 我这里是maven项目就直接选择的以maven项目引入,如果选eclipse的话,pom文件不会被初始化,部署tomcat会出问题 这项选完后,就一路next,jdk可以在引入的 ...