codeforces 416D Population Size

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,,,所以希望能看出我哪错的大神可以跟我说一下~~谢谢~

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

int main ()
{
    int n;
    while(cin>>n)
    {
        ll a[n],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;
}
### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值