您的位置:首页 > 运维架构

codeforces 416D Population Size

2015-12-04 23:41 316 查看
Description

Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions.

Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence a1, a2, …, an, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8, 6, 4, 2, 1, 4, 7, 10, 2) can be considered as a sequence of three arithmetic progressions (8, 6, 4, 2), (1, 4, 7, 10) and (2), which are written one after another.

Unfortunately, Polycarpus may not have all the data for the n consecutive years (a census of the population doesn’t occur every year, after all). For this reason, some values of ai ​​may be unknown. Such values are represented by number -1.

For a given sequence a = (a1, a2, …, an), which consists of positive integers and values ​​-1, find the minimum number of arithmetic progressions Polycarpus needs to get a. To get a, the progressions need to be written down one after the other. Values ​​-1 may correspond to an arbitrary positive integer and the values ai > 0 must be equal to the corresponding elements of sought consecutive record of the progressions.

Let us remind you that a finite sequence c is called an arithmetic progression if the difference ci + 1 - ci of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression.

Input

The first line of the input contains integer n (1 ≤ n ≤ 2·105) — the number of elements in the sequence. The second line contains integer values a1, a2, …, an separated by a space (1 ≤ ai ≤ 109 or ai =  - 1).

Output

Print the minimum number of arithmetic progressions that you need to write one after another to get sequence a. The positions marked as -1 in a can be represented by any positive integers.

Sample Input

Input

9

8 6 4 2 1 4 7 10 2

Output

3

Input

9

-1 6 -1 2 -1 4 7 -1 2

Output

3

Input

5

-1 -1 -1 -1 -1

Output

1

Input

7

-1 -1 4 5 1 2 3

Output

2

题意:输入一个n个数的序列,求最少可以划分成多少个子的等差序列,-1代表任意值,但是一定要大于0.

贪心思路,首先先找到最前两个不为任意值的数,判断是否可以组成等差数列,判断是否能组成,则要记录第一个数前面有几个-1,确定公差后会不会导致前面任意值变成负数。然后确定公差之后,只需要记录公差和前一项的值即可,如果新的数不能并入序列,则新开序列,按照上面的方法重新计算。

按照这个思路我写了一下代码:注意!!!!代码是错误的!!!

过的21组样例,可是还没过了;因为这组数据:

5

-1 1 7 -1 5

我们看应该输出2,可按照思路是3,,,所以希望能看出我哪错的大神可以跟我说一下~~谢谢~

[code]#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main ()
{
    int n;
    while(cin>>n)
    {
        ll a
,cx1=0,cx2=0,bx=0,le,ri,flag=0,num=0;
        for(int i=0; i<n; i++)
            cin>>a[i];
        int t=0,x=0,y=0;
        while(y<2)
        {
            for(int i=t; i<n; i++)
            {
                t=i;
                if(x==0)
                {
                    if(a[i]==-1&&flag==0)
                    {
                        cx1++;
                    }
                    else if(a[i]==-1&&flag==1)
                    {
                        cx2++;
                    }
                    else if(a[i]!=-1&&flag==0)
                    {
                        le=a[i];
                        flag++;
                    }
                    else if(a[i]!=-1&&flag==1)
                    {
                        ri=a[i];
                        flag++;
                    }
                    if(flag==2)
                    {
                        num++;
                        bx=(ri-le)/(cx2+1);
                        if(bx*(cx2+1)==(ri-le))
                        {
                            ll ss=0;
                            if(cx1!=0)
                                for(ss=0; ss<cx1; ss++)
                                {
                                    le-=bx;
                                    if(le<=0)
                                        break;
                                }
                            if(ss!=cx1)
                                num++;
                            flag=0;
                            cx1=0;
                            cx2=0;
                            x=1;
                            le=0;
                        }
                        if(bx*(cx2+1)!=(ri-le))
                        {
                            ll ss=0;
                            if(cx1!=0)
                                for(ss=0; ss<cx1; ss++)
                                {
                                    le-=bx;
                                    if(le<=0)
                                        break;
                                }
                            if(ss!=cx1)
                                num++;
                            flag=0;
                            cx1=0;
                            cx2=0;
                            le=0;
                        }
                    }
                }
                else if(x==1)
                {
                    ri+=bx;
                    if(ri<=0)
                    {
                        x=0;
                        break;
                    }
                    if(a[i]!=ri&&a[i]!=-1)
                    {
                        x=0;
                        break;
                    }
                }
            }
            if(t==n-1)
                y++;
        }
        if(cx1!=0||le!=0)
            num++;
        cout<<num<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: