codeforces - 978D【思维】
1 second
256 megabytes
standard input
standard output
Polycarp likes arithmetic progressions. A sequence [a1,a2,…,an][a1,a2,…,an] is called an arithmetic progression if for each ii (1≤i<n1≤i<n ) the value ai+1−aiai+1−ai is the same. For example, the sequences [42][42] , [5,5,5][5,5,5] , [2,11,20,29][2,11,20,29] and [3,2,1,0][3,2,1,0] are arithmetic progressions, but [1,0,1][1,0,1] , [1,3,9][1,3,9] and [2,3,1][2,3,1] are not.
It follows from the definition that any sequence of length one or two is an arithmetic progression.
Polycarp found some sequence of positive integers [b1,b2,…,bn][b1,b2,…,bn] . He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by 11 , an element can be increased by 11 , an element can be left unchanged.
Determine a minimum possible number of elements in bb which can be changed (by exactly one), so that the sequence bb becomes an arithmetic progression, or report that it is impossible.
It is possible that the resulting sequence contains element equals 00 .
The first line contains a single integer nn(1≤n≤100000)(1≤n≤100000) — the number of elements in bb .
The second line contains a sequence b1,b2,…,bnb1,b2,…,bn(1≤bi≤109)(1≤bi≤109) .
If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).
4
24 21 14 10
3
2
500 500
0
3
14 5 1
-1
5
1 3 6 9 12
1
In the first example Polycarp should increase the first number on 11 , decrease the second number on 11 , increase the third number on 11 , and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to [25,20,15,10][25,20,15,10] , which is an arithmetic progression.
In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.
In the third example it is impossible to make an arithmetic progression.
In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like [0,3,6,9,12][0,3,6,9,12] , which is an arithmetic progression.
解题思路:等差数列的公差相等, 所以前两个数就可以确定公差,记录后面的数满足公差需要多少步即可,O(6 * n)的复杂度。
附ac代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <string>
5 #include <cmath>
6 #include <string>
7 #include <iostream>
8 #include <map>
9 #include <queue>
10 #include <stack>
11 #include <cstdlib>
12 const int maxn = 3 * 1e5 + 10;
13 const int inf = 0x3f3f3f3f;
14
15 using namespace std;
16 typedef long long ll;
17 const ll mod = 1e9 + 7;
18 int nu[maxn];
19 int tem[maxn];
20 queue<int>q;
21
22 int main(int argc, const char * argv[]) {
23 int n;
24 scanf("%d", &n);
25 for(int i = 0; i < n; ++i)
26 {
27 scanf("%d", &nu[i]);
28 }
29 if(n <= 2)
30 {
31 puts("0");
32 return 0;
33 }
34 int i, j, k;
35 int ans = inf;
36 for(i = -1; i <= 1; ++i)
37 {
38 for(j = -1; j <= 1; ++j)
39 {
40 tem[0] = nu[0] + i;
41 tem[1] = nu[1] + j;
42 int d = tem[1] - tem[0];
43 int cnt = abs(i) + abs(j);
44 for(k = 2; k < n; ++k)
45 {
46 int u = nu[k] - tem[k - 1];
47 tem[k] = d + tem[k - 1];
48 if(u == d) continue;
49 if(abs(u - d) <= 1)
50 {
51 cnt++;
52 // printf("%d ", tem[k]);
53 }
54 else break;
55 }
56 if(k == n)
57 {
58 ans = min(cnt, ans);
59 }
60 }
61 }
62 if(ans == inf) puts("-1");
63 else
64 printf("%d\n", ans);
65 return 0;
66 }
codeforces - 978D【思维】的更多相关文章
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Queue CodeForces - 353D (思维dp)
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
- codeforces 1244C (思维 or 扩展欧几里得)
(点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- CodeForces - 417A(思维题)
Elimination Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- CodeForces 625A 思维
题意是说一个人喝酒 有两种办法 买塑料瓶的 a块钱 喝了就没了 或者是买玻璃瓶的b块钱 喝完还能卖了瓶子c块钱 求最多能喝多少瓶 在开始判断一次 a与b-c的关系 即两种方式喝酒的成本 如果a< ...
- Vladik and Complicated Book CodeForces - 811B (思维实现)
Vladik had started reading a complicated book about algorithms containing n pages. To improve unders ...
- The Contest CodeForces - 813A (思维)
Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...
随机推荐
- ABAP 多表联合查询
inner join(等值连接) 只返回两个表中联结字段相等的行left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) 返回包括右表中的所有记录 ...
- MySQL数据库基础知识及优化
MySQL数据库基础知识及优化必会的知识点,你掌握了多少? 推荐阅读: 这些必会的计算机网络知识点你都掌握了吗 关于数据库事务和锁的必会知识点,你掌握了多少? 关于数据库索引,必须掌握的知识点 目录 ...
- web项目启动链接mysql巨慢
说明:项目部署到测试服务器上,mysql部署在另一台服务器上,项目第一次启动之后登陆后台很慢,大概30s左右,经查发现第一次访问数据库的时候会通过DNS解析客户端机器域名,mysql还有DNS反向解析 ...
- Flask之路由系统
路由系统 路由的两种写法 1.第一种方法: def index(): return render_template('index.html') app.add_url_rule('/index', ' ...
- 【pytest】(十)fixture参数化-巧用params和ids优雅的创建测试数据
我们都知道参数化. 比如我要测试一个查询接口/test/get_goods_list,这个接口可以查询到商品的信息. 在请求中,我可以根据请参数goods_status的不同传值,可以查询到对应状态的 ...
- Supporting Multiple Versions of WebSocket Protocol 支持多版本WebSocket协议
https://tools.ietf.org/html/rfc6455#section-4.4 4.4. Supporting Multiple Versions of WebSocket Proto ...
- Poem 01(转)
Dear Sunshine The way you glow through my blinds in the morning. It makes me feel like you missed me ...
- TCP IP SOCKET 笔记
网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...
- Cisco的互联网络操作系统IOS和安全设备管理器SDM__散知识点1
1.启动路由器:当你初次启动一台Cisco路由器时,它将运行开机自检(POST)过程.如果通过了,它将从闪存中查找Cisco IOS,如果有IOS文件存在,则执行装载操作(闪存是一个可电子擦写.可编程 ...
- jqXHR.fail()回调方法及其参数详细说明
jqXHR.fail()是一个可供选择的 error 回调选项的构造函数,.fail()方法取代了的过时的.error()方法.从 jQuery 1.5 开始,$.ajax()返回的jqXHR对象 实 ...