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 ...
随机推荐
- UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)
传送门: http://www.qscoj.cn/#/problem/show/1987 童心未泯的帆宝和乐爷 Edit Time Limit: 10000 MS Memory Limit: ...
- (转)查看mysql数据库连接数、并发数相关信息
查看mysql数据库连接数.并发数相关信息 1.mysql> show status like 'Threads%';+-------------------+-------+| Variabl ...
- FD.io社区中国行暨未来网络技术沙龙·南京站 会议小结
What is FD.io VPP? FD.io VPP(Fast Data Input/Output Vector Packet Processing)is a new network multi- ...
- 字符型设备驱动程序-first-printf以及点亮LED灯(一)
学习使用 Linux 的 字符型设备驱动 来 进行 . 学习地址:http://edu.51cto.com/lesson/id-25710.html 第一步: 首先写 三个函数 ,2017年5月17 ...
- C++快速开发样本工程的建立--建立工程
因为QT建立工程清晰整洁,便于作为样板工程原型.采用QT 5.8.0 64位版本建立工程. 1.建立工程 打开VS2015 新建->新建项目->QT GUI Application -&g ...
- C++程序设计入门 引用和动态内存管理学习
引用: 引用就是另一个变量的别名,通过引用所做的读写操作实际上是作用于原变量上. 由于引用是绑定在一个对象上的,所以定义引用的时候必须初始化. 函数参数:引用传递 1.引用可做函数参数,但调用时只需 ...
- MySQL更新丢失
MySQL多主结构(比如: MGR Multi Master模式),如果多主都可以写的话,很有可能出现更新丢失的情况. 定义: T1时间,T2时间,T3时间 定义: 事务A, 事务B Node1节 ...
- activemq的高级特性:集群实战
高级特性实战需求 当消费端是多个集群,集群A又包含多个服务. 当每个集群都要接受相同的一批消息,而集群内的每个服务都去分摊消息. 解决办法一:级联 增加一个中转者.但是不是特别的优化,而且性能也不是特 ...
- mysql/mariadb学习记录——limit
在mysql/mariadb 中可以用limit来限制查询的条数.例子如下: 1.limit后加一个参数 limit n: //选中查询所有结果中的前两条记录并返回 mysql> ; +---- ...
- bat脚本实现复制特定后缀文件到其他目录
@echo off for /r %%a in (*.txt) do copy %%a D:\1 pause 1.for /r主要用于搜索指定路径及其所有子目录中符合要求的文件(/r后如果没有指定目录 ...