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,,,所以希望能看出我哪错的大神可以跟我说一下~~谢谢~
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; }
相关文章推荐
- Linux_shell_基础
- Linux C 信号处理
- maven学习五:maven集成tomcat插件发布web项目
- 当nginx 500 伪静态错误时,记录解决方法rewrite or internal redirection cycle while processing
- ulimit -c unlimited
- Linux下编译FFmpeg
- 从A页面单击 window.open 打开了B页面 B页面没有关闭再次点击A页面的这个链接, 让他刷新B页面而不是在打开一个B页面可以做到吗?
- 多重bash登入的history写入问题
- centos ifcfg-eth0
- linux:usermod 和 chage命令
- linux:/etc/default/useradd 和 /etc/login.defs
- Linux进程管理工具——supervisor
- Linux软件安装及卸载
- dot(.) is a synonym of `source`
- linux 代码分析工具 gprof - 以wpa_supplicant为例
- Tomcat8源码分析(三)
- BitmapFactory.Options.inSampleSize(避免OOM)
- Linux常用命令
- 【Linux内核层】深入netfilter编程
- Linux下操作Mysql类