首页 > 代码库 > CodeForces - 402B Trees in a Row (暴力)

CodeForces - 402B Trees in a Row (暴力)

题意:给定n个数,要求修改其中最少的数,使得这n个数满足ai + 1 - ai = k。

分析:

暴力,1000*1000。

1、这n个数,就是一个首项为a1,公差为k的等差数列。k已知,如果确定了a1,就能确定整个数列。

2、1 ≤ ai ≤ 1000,因此,可以从1~1000中枚举a1,将形成的数列与给定的数列比较,统计两数列对应下标中不同数字的个数。

3、不同数字的个数最少的那个数列就是最终要修改成的数列,然后输出对应下标的那个数的变化值即可。

#include<cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>#include<sstream>#include<iterator>#include<algorithm>#include<string>#include<vector>#include<set>#include<map>#include<stack>#include<deque>#include<queue>#include<list>#define lowbit(x) (x & (-x))const double eps = 1e-8;inline int dcmp(double a, double b){    if(fabs(a - b) < eps) return 0;    return a > b ? 1 : -1;}typedef long long LL;typedef unsigned long long ULL;const int INT_INF = 0x3f3f3f3f;const int INT_M_INF = 0x7f7f7f7f;const LL LL_INF = 0x3f3f3f3f3f3f3f3f;const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};const int MOD = 1e9 + 7;const double pi = acos(-1.0);const int MAXN = 1000 + 10;const int MAXT = 10000 + 10;using namespace std;int a[MAXN];int main(){    int n, k;    scanf("%d%d", &n, &k);    for(int i = 1; i <= n; ++i){        scanf("%d", &a[i]);    }    int id = 0;    int _min = 0x7f7f7f7f;    for(int i = 1; i <= 1000; ++i){        int cnt = 0;        for(int j = 1; j <= n; ++j){            if(a[j] != i + (j - 1) * k){                ++cnt;            }        }        if(cnt < _min){            _min = cnt;            id = i;        }    }    printf("%d\n", _min);    for(int i = 1; i <= n; ++i){        if(a[i] != id + (i - 1) * k){            if(a[i] > id + (i - 1) * k){                printf("- %d %d\n", i, a[i] - id - (i - 1) * k);            }            else{                printf("+ %d %d\n", i, id + (i - 1) * k - a[i]);            }        }    }    return 0;}

  

CodeForces - 402B Trees in a Row (暴力)