B. Strip

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/487/problem/B

Description

Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.

Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:

Each piece should contain at least l numbers.
    The difference between the maximal and the minimal number on the piece should be at most s.

Please help Alexandra to find the minimal number of pieces meeting the condition above.

⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.

There are three types of players.

Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it's difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.

There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.

The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).

The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).

Output

Output the minimal number of strip pieces.

If there are no ways to split the strip, output -1.

Sample Input

7 2 2
1 3 1 2 4 1 2

Sample Output

3

HINT

题意

给你n个数,你可以把这些数划分成很多个区间,要求每个区间的长度至少为L,区间内最大值减去最小值至少为S

问你区间最少划分为多少

题解:

就DP,DP啦

最简单的想法就是DP[i]表示从0-i最少能够划分多少块,那么

for(int j=i-1;j>=0;j--)

  if(check(j,i-1))

    dp[i]=min(dp[i],dp[j]+1);

这个是最简单的dp方程,但是,这个无脑转移是N^2的,那么肿么办?

你可以用线段树优化,也可以用multiset优化一下

代码

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<set>
using namespace std;
#define maxn 100005
int a[maxn];
int dp[maxn];
const int inf = 1e9;
multiset<int> Val;
multiset<int> Dp;
int main()
{
int n,s,l;
scanf("%d%d%d",&n,&s,&l);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
dp[i]=inf;
int L = ;
for(int i=;i<l-;i++)
Val.insert(a[i]);
dp[]=;
for(int i=l-;i<n;i++)
{
Dp.insert(dp[i-(l-)]);
Val.insert(a[i]);
while(L<=(i-(l-))&&(*Val.rbegin()-*Val.begin()>s))
{
Val.erase(Val.find(a[L]));
Dp.erase(Dp.find(dp[L]));
L++;
}
if(!Dp.empty())
dp[i+]=(*Dp.begin())+;
}
if(dp[n]>n)printf("-1\n");
else printf("%d\n",dp[n]);
}

Codeforces Round #278 (Div. 1) B. Strip multiset维护DP的更多相关文章

  1. Codeforces Round #278 (Div. 2) D. Strip 线段树优化dp

    D. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  2. Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列

    B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...

  3. Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp

    D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...

  4. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  5. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  6. Codeforces Round #278 (Div. 2)

    题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...

  7. Codeforces Round #278 (Div. 1)

    A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang an ...

  8. Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)

    B. Candy Boxes Problem's Link:   http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...

  9. Codeforces Round #278 (Div. 1) A. Fight the Monster 暴力

    A. Fight the Monster Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/ ...

随机推荐

  1. DB2之隔离级别和锁的论述

    在DB2数据库中, 是通过行级锁和表级锁协调作用来提供较好的并发性, 同时保证数据库中数据的安全. 在DB2中缺省情况下使用行级锁(当然需要IS/IX锁配合),只有当出现锁资源不足, 或者是用命令指定 ...

  2. TCP/IP详解学习笔记(3)-IP协议,ARP协议,RARP协议

    把这三个协议放到一起学习是因为这三个协议处于同一层,ARP协议用来找到目标主机的Ethernet网卡Mac地址,IP则承载要发送的消息.数据链路层可以从ARP得到数据的传送信息,而从IP得到要传输的数 ...

  3. T-SQL查询进阶-10分钟理解游标

    转:http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的 ...

  4. Webservice 调用方式整理

    前一段时间搞webservice,简单的记录了一下几种常用的调用方式,供大家参考. 第一种:Java proxy 1).用过eclipse的创建web service client来完成 2).在ec ...

  5. 修复duilib库UISlider控件的4个bug

    转载级请注明原出处,谢谢~· 昨天封装好一个音频类,我在为dulib做音频播放demo时发现了一些问题,由CSliderUI控件导致的,进而发现了这个控件的好几样不足,他无法满 足我们做一个播放器的进 ...

  6. 二分+叉积判断方向 poj 2318 2398

    // 题意:问你每个区域有多少个点 // 思路:数据小可以直接暴力 // 也可以二分区间 #include <cstdio> #include <cstring> #inclu ...

  7. Tkinter教程之Radiobutton篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811308 #Tkinter教程之Radiobutton篇#Radiobutton为单选按钮, ...

  8. Google C++ 编程规范总结

    一.头文件 #define 的保护 项目 foo 中的头文件 foo/src/bar/baz.h 按如下方式保护: #ifndef FOO_BAR_BAZ_H_ #define FOO_BAR_BAZ ...

  9. Hadoop-安装过程-单虚拟机版(伪分布式)(Ubuntu13.04版本下安装)

    由于新装的Ubutu默认情况下,系统只安装了SSH客户端,需要自行安装SSH服务端 如何确定是否安装了SSH服务端? 可以通过命令ssh localhost,结果如下,即未安装SSH服务端:   安装 ...

  10. Hadoop学习笔记2---配置详解

    配置系统是复杂软件必不可少的一部分,而Hadoop配置信息处理是学习Hadoop源代码的一个很好的起点.现在就从Hadoop的配置文件谈起. 一.Hadoop配置格式 Hadoop配置文件格式如下所示 ...