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 ...
随机推荐
- 高斯消元求主元——模意义下的消元cf1155E
#include <bits/stdc++.h> , MO = ; ; inline int qpow(int a, int b) { ; while(b) { ) { ans = 1ll ...
- 安卓混合开发——原生Java和H5交互,保证你一看就懂!
** 在Android开发中,越来越多的商业项目使用了Android原生控件与WebView进行混合开发,当然不仅仅就是显示一个WebView那么简单,有时候还需要本地Java代码与HTML中的Jav ...
- 到底什么是dp思想(内含大量经典例题,附带详细解析)
期末了,通过写博客的方式复习一下dp,把自己理解的dp思想通过样例全部说出来 说说我所理解的dp思想 dp一般用于解决多阶段决策问题,即每个阶段都要做一个决策,全部的决策是一个决策序列,要你求一个 最 ...
- 用JavaScript中jQuery编写放大镜效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 容易忽略的expect脚本问题,暗藏的僵尸进程,wait命令不要漏掉
问题描述 前几天有个小需求,用到expect脚本去循环的发送一些数据,主要问题代码如下: #! /usr/bin/expect while {true} { set timeout 60 spawn ...
- Notes 20180509 : Java基本数据类型
计算机就是个机器,这个机器由CPU.内存.硬盘和输入输出设备组成.计算机上运行着操作系统,操作系统提供对外的接口供各厂商和开发语言,开发运行在其上的驱动和应用程序. 操作系统将时间分成细小的时间碎片, ...
- generate failed: Cannot resolve classpath entry: mysql-connector-java-5.1.38.jar
详细错误及处理方法如下: [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3 ...
- ios软键盘将页面抵到上面后,关闭软键盘页面不回弹
这个问题有时候会导致弹出框确定按钮失效等一系列问题, 解决办法:失去焦点时将页面滚动到底层,或者最顶部,个人看实际情况滚动到适合位置 $('input,textarea').on('blur', fu ...
- 页面缓存优化系列一(expires,cache-control 解读)
参考文章:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Cache-Control http://caibaojian.com/s ...
- ABAP术语-Update Data
Update Data 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/20/1114169.html The data which is t ...