ZOJ 3365

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3365

就是给你一个序列,让你改变最少的数使其变成连续递增的数列

用b[i] = a[i]-i;会有意想不到的效果

b[i]存的就是从b[]开始一次增的的序列

代码中

if(sum < cnt)
            {
                pos = pre;
                sum = cnt;
            }


稍稍纠结了一下,本来是放在if(pre != b[i])中的,结果不停的WA,现在也没想明白

#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50000+10;
int a[N];
int b[N];
int main(void)
{
    //freopen("1.txt", "r", stdin);
//    freopen("2.txt", "w", stdout);
    int n;
    while(cin>>n)
    {
        for(int i = 0; i < n; i++)
            cin>>a[i], b[i] = a[i] - i;

        sort(b, b+n);
        int pre = b[0], sum = 0, cnt = 0, pos;
        for(int i = 0; i < n; i++)
        {
            if(pre != b[i])
            {
                pre = b[i];
                cnt = 1;
            }
            else
                cnt++;
            if(sum < cnt)
            {
                pos = pre;
                sum = cnt;
            }
        }
        cout<<n-sum<<endl;
        cout<<pos;
        for(int i = 1; i < n; i++)
            cout<<" "<<pos+i;
        cout<<endl;
    }
    return 0;
}




你可能感兴趣的:(ZOJ 3365)