Codeforces Round #278 (Div. 2) d

/**
 * @brief Codeforces Round #278 (Div. 2) d
 * @file d.c
 * @author 面码
 * @created 2014/11/26 10:07
 * @edited  2014/11/26 10:07
 * @type dp 
 * @note
 *      自己的TL了,看了别人代码写的
 *      该代码主要是在dp的基础上使用stl来提速
 *      dp需辅助提速,但内存又不能爆掉是该题目的卡点 = =
 */
#include <cstdio>
#include <set>
#include <utility>
#include <string.h>

using namespace std;

#define max(a, b)  ((a) > (b) ? (a) : (b))
#define min(a, b)  ((a) > (b) ? (b) : (a)) 
#define abs(a)     ((a) >  0  ? (a) : (0 - (a)))
#define CLR(vec)   memset(vec, 0, sizeof(vec))

#define MAXN 100010
#define MAXS 1000000010
#define MAXl 100010

typedef pair<int, int> bbq;
int dp[MAXN];
int in[MAXN];
set<bbq> R, S;
int n, s, l;

int main()
{
    int i, j;
#ifdef DEBUG
    freopen("./in",  "r", stdin);
    freopen("./out", "w", stdout);
#endif
    scanf("%d%d%d", &n, &s, &l);
    for(i = 1; i <= n; i++)
        scanf("%d", &in[i]);
    j = 1;
    for(i = 1; i <= n; i++){
        S.insert(bbq(in[i], i));
        while(!S.empty() && S.rbegin()->first - S.begin()->first > s ){
            S.erase(bbq(in[j], j));
            j++;
        }
        if(i >= l && -1 != dp[i - l])
            R.insert(bbq(dp[i - l], i - l));
        while(!R.empty() && R.begin()->second < j - 1)
            R.erase(R.begin());
        dp[i] = R.empty() ? -1 : R.begin()->first + 1;
    }
    printf("%d\n", dp[n]);
    return 0;
}
时间: 2024-08-27 18:13:53

Codeforces Round #278 (Div. 2) d的相关文章

Codeforces Round #278 (Div. 2) b

/**  *  * @brief Codeforces Round #278 (Div. 2) b  * @file b.c  * @author mianma  * @created 2014/11/24 17:52  * @edited  2014/11/18 17:52  * @type brute  *   * @note   *          declare k >= 0;  *              then   *                  x1 = k  *   

Codeforces Round #278 (Div. 2) c

/**  * @brief Codeforces Round #278 (Div. 2) c  * @file c.c  * @author 面码  * @created 2014/11/25 14:15  * @edited  2014/11/25 14:15  * @type brute  *  */ #include <stdio.h> #define max(a, b)  ((a) > (b) ? (a) : (b)) #define min(a, b)  ((a) > (

Codeforces Round #278 (Div. 1)

A A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF). During the battle, every second the monster's H

CodeForces Round #278 (Div.2) (待续)

A 这么简单的题直接贴代码好了. 1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 5 bool islucky(int a) 6 { 7 a = abs(a); 8 while(a) 9 { 10 if(a % 10 == 8) return true; 11 a /= 10; 12 } 13 return false; 14 } 15 16 int main(void) 17 { 18 int a,

Codeforces Round #278 (Div. 2)

A直接暴力好了. 1 #include<bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 5 int pan(ll x) 6 { 7 while (x) 8 { 9 if (x%10==8) return 1; 10 x/=10; 11 } 12 return 0; 13 } 14 15 int main() 16 { 17 ll n; 18 cin>>n; 19 for (int i=1;i<=1

Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]

哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058                 2014-11-22 06:52:58     njczy2010     B - Candy Boxes             GNU C++     Accepted 31 ms 4 KB 8795016                 2014-11-22 06:48:15     njczy2010     B - Candy Boxes             GNU C+

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿