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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值