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 ...
随机推荐
- Selenium封装
import os from selenium import webdriver from selenium.webdriver.common.by import By from selenium.w ...
- Linux中使用iptables开放特定端口
禁止其他主机对该特定主机进行访问和远程连接控制,所以只开放特定端口 只控制INPUT链就可达到控制其他主机对该主机的访问. 1.首先关闭INPUT链 iptables -P INPUT DROP 使用 ...
- web测试--测试点
转载自:51Testing 首先,查找需求说明.网站设计等相关文档,分析测试需求,制定测试计划,确定测试范围和测试策略,一般包括以下几个部分:功能性测试:界面测试:性能测试:数据库测试:安全性测试:兼 ...
- CC2540 OSAL 学习其中原理,以及 给任务 添加 一个事件(定时发送串口消息)
参考学习大神博客: http://blog.csdn.net/feilusia/article/details/51083953 : http://blog.csdn.net/xiaoleiacmer ...
- IIS中ASP.NET虚拟目录不继承主站点web.config设置的办法(转载)
ASP.NET提供了强大的Web.config来配置网站,一般来说一个网站只有一个根目录下的Web.config文件,有时候我们希望子目录有着不同的权限或者参数设置,则可以在相应子目录增加一个Web. ...
- 结构之美——优先队列基本结构(四)——二叉堆、d堆、左式堆、斜堆
实现优先队列结构主要是通过堆完成,主要有:二叉堆.d堆.左式堆.斜堆.二项堆.斐波那契堆.pairing 堆等. 1. 二叉堆 1.1. 定义 完全二叉树,根最小. 存储时使用层序. 1.2. 操作 ...
- leetcode -50. Pow(x, n) Accepted
前言:其实之前自己也有了解关于算法数据结构的一点内容,但是都是用相应的开发工具来写相应的代码,今天面试的时候直接leetcode来写代码,还是用的体内根深蒂固的C和Java来解的题,毕竟目前没见支持O ...
- MongoDB常用指令
db 查看当前操作的数据库 show dbs 显示所有数据库 show collections 显示当前数据库下的所有集合 use database_name 连接到一个名叫[database_nam ...
- SFTP 服务搭建
1. 介绍 sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP ...
- vue keep-alive 不生效 以及前进 后退 对数据刷新和保留缓存操作
https://blog.csdn.net/sinat_37255207/article/details/89373825 因为项目Vue router 连续嵌套了好几层 首先检查keep-alive ...