Problem Population Size

题目大意

  给一个长度为n的序列,由 -1 和正整数组成,-1表示任意的正整数。

  将序列分成若干段,使得任意段都是等差数列,求最少段数。

解题分析

  可以发现对于某一段序列,越长越好。贪心加点,保证每段都是最长就可以了。

  Tips:一段相同的数也可以算是等差数列。

参考程序

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std; #define N 200008
#define M 50008
#define LL long long
#define lson l,m,rt<<1
#define rson m,r+1,rt<<1|1
#define clr(x,v) memset(x,v,sizeof(x));
const int mo = ;
const int inf = 0x3f3f3f3f;
const int INF = ;
/**************************************************************************/
int n;
int a[N],r[N]; int main(){
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
int x=n+;
for (int i=n;i>=;i--){
r[i]=x;
if (~a[i]) x=i;
} int i=,j,k,d,num1,num2,ans=; while (i<=n){
//printf("%d\n",i );
if (a[i]==-){
j=r[i]; k=r[j];
if (j==n+) { ans++; break; } else num1=j-i;
num2=k-j;
if (k==n+){
/*if (min(num1,num2)>=a[j]) { ans+=2; break; }
else { ans++; break; }
*/
ans++; break;
}
else{
int flag=;
if ( (a[k]-a[j]) % (k-j)==){
d=(a[k]-a[j]) / (k-j);
if ( 1ll*a[j] - 1ll*d * (j-i) > ) flag =;
}
if (flag){
long long x=a[k];
for (i=k+;i<=n;i++){
x+=d;
if (x<=) break;
if (a[i]!=- && a[i]!=x) break;
}
ans++;
}
else{
/*if (min(num1,num2)>=a[j]) i=j+a[j]; else i=k;*/
ans++; i=k;
}
}
}
else
{
j=r[i];
if (j==n+) { ans++; break; } else num1=j-i-; int flag=;
if ( (a[j]-a[i]) % (j-i)==){
d=(a[j]-a[i]) / (j-i);
flag =;
}
if (flag){
long long x=a[j];
for (i=j+;i<=n;i++){
x+=d;
if (x<=) break;
if (a[i]!=- && a[i]!=x) break;
}
ans++;
}
else{
i=j;
ans++;
}
}
}
printf("%d\n",ans);
}

CodeForces 416D (贪心)的更多相关文章

  1. Population Size CodeForces - 416D (贪心,模拟)

    大意: 给定$n$元素序列$a$, 求将$a$划分为连续的等差数列, 且划分数尽量小. $a$中的$-1$表示可以替换为任意正整数, 等差数列中必须也都是正整数. 贪心策略就是从前到后尽量添进一个等差 ...

  2. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  4. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心

    Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...

  5. CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  6. C - Ordering Pizza CodeForces - 867C 贪心 经典

    C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...

  7. Codeforces 570C 贪心

    题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...

  8. Codeforces 732e [贪心][stl乱搞]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...

  9. Codeforces 721D [贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...

随机推荐

  1. 详细解读Jquery各Ajax函数

    $.get(),$.post(),$.ajax(),$.getJSON() 一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表,callba ...

  2. js 时间处理

    1.格式化时间 function GetDateTimeFormatter(value) {        if (value == undefined) {            return &q ...

  3. RestSharp .net 轻量级rest客户端

    RestSharp Simple REST and HTTP API Client for .NET 官网:http://restsharp.org/ GiHub: https://github.co ...

  4. Objective-C:@property参数详解

    格式:@property(param1, param2) 类型 属性名 参数可有可无 三类参数: 1.读写属性:readwrite/readonly readwrite:产生setter\getter ...

  5. PHP换行符详解 PHP_EOL,<br />

    1.PHP_EOL:用于文本的换行符,经常用于日志文件记录.可以解决: 在windows中\r\n是换行在Mac中\r是换行在Liunx中\n是换行 不统一的问题. 2. <br /> : ...

  6. Hadoop作业JVM堆大小设置优化 [转]

    前段时间,公司Hadoop集群整体的负载很高,查了一下原因,发现原来是客户端那边在每一个作业上擅自配置了很大的堆空间,从而导致集群负载很高.下面我就来讲讲怎么来现在客户端那边的JVM堆大小的设置.我们 ...

  7. 创建论坛Discuz

    下载discuz! mkdir /data/wwwcd /data/wwwwget  http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_GB ...

  8. oracle查询语句大全

    1.查处oracle数据库的字符编码格式 select * from v$nls_parameters t where t.PARAMETER='NLS_CHARACTERSET'; 如果查出的是ZH ...

  9. [css3]搜索框focus时变长

    结构: <form class="demo-a">    <input placeholder="Search" type="sea ...

  10. 启动项目报错Error: listen EADDRINUSE

    我在使用elasticsearch的kibana插件时候,有一次启动,遇到这个错误: Error: listen EADDRINUSE 它的意思是,端口5601被其他进程占用. 故而,需要kill掉那 ...