小y的序列
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
又是一年 NOIP,高中机房的学长们都在做题,安静的有点可怕,突然听到隔壁机房某老师熟悉的声音:“我们看一下这道题,找找规律发现这个序列很熟悉啊,就是2,3,5,7,12这其实就是一个a[i+1]-a[i]=i的序列哦,突然隔壁的吵闹声大了起来,老师,老师好像有个数写错了(大雾)~~~~~~~~~~~~

课后,小y大牛跑到隔壁机房在黑板上写下了这个题目,让小朋友们做:给出一个长度为n的整数序列a,你能改动最少的数,使之满足a[i+1]-a[i]=i吗?1<=i<n。

输入
第一行一个整数n;
第二行包含n个整数(每两个数之间有一个空格),分别表示a[1]到a[n]。

输出
输出一个整数,表示最少改多少个数
样例输入 Copy
5
1 2 4 5 11
样例输出 Copy
1
提示
对于30%的数据 n<=1000
对于100%的数据 n<=100000
输入的其他数据的绝对值均小于等于1E9

今天状态有点好,就做了一个题 2333。
一般我做这种题直接喜欢直接推一下公式,让后就在纸上瞎搞,搞出来个式子如下 :
a [ n - 1 ] + (n - 1) = a [ n ]
a [ n - 2 ] + (n - 1) - 1 = a [ n - 1 ]
a [ n - 3 ] + (n - 1) - 2 = a [ n - 2 ]

由此发现,对于 1 ~ n - 1 中 每一个 a [ i ] ,都可以求出来在序列正确的情况下,对应的 a [ n ] 的值。很简单,直接把对应的值依次带进去,就可以把第 i 项的前几项都消去,得到每个数对应第 n 项的值 (即为代码中的sum):

	h=n-1;for(int i=n-1,j=0;i>=1;i--,j++)LL sum=h*(j+1)-f[j]+a[i];//f是从0~j的和

那么求出来这个有什么用呢?
现在,我们知道了每一个数在合适的情况下对应的 a [ n ] 的值,那么我们只需要考虑是否修改第n个数和是否修改前 n - 1 个数中的某些。答案也就呼之欲出了,对于求出来的所有第 n 个数可能的值,假设当前的 a [ n ] 就为该值 t,cnt是该值对应的a [ i ] 的数量,那么答案就是 (n - 1) - cnt + ( a[ n ] ! = t )
一开始没注意自己 a 数组开的 LL ,用 int 读,看着红红的 wrong answer ,陷入了对人生和社会的大思考。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
using namespace std;typedef long long LL;
typedef pair<int,int> PII;const int N=100010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
LL a[N],f[N];
int ans=1e9;
map<LL,int>has;int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);for(int i=1;i<=1e5;i++)f[i]=f[i-1]+i;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%lld",&a[i]);LL h=n-1;for(int i=n-1,j=0;i>=1;i--,j++){LL sum=h*(j+1)-f[j]+a[i];has[sum]++;}for(auto t:has){LL x=t.X;int y=t.Y;int cnt=h-y;if(x!=a[n]) cnt++;if(ans>cnt) ans=cnt;}printf("%d\n",ans);return 0;
}
/**/
Libra_Glow
原创文章 62获赞 15访问量 5380
关注私信
展开阅读全文