D - Strip

无奈,二十行代码看了几个小时。。 还不知道理解的对不对。。真完蛋。。

思路:

仔细想想,感觉难在怎么建立数据结构,存取状态。。

用set来保存状态,set<pair<int,int>> A,B,其中A.first保存的是数字的值,sceond保存的是数字位置。

set<pii> B的first保存的是要求的最小组数,second保存的是位置。

最主要的还是dp的思想,从前一最优状态求现在的最优状态

上代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<map>
 9 #include<iomanip>
10 #include<climits>
11 #include<string.h>
12 #include<numeric>
13 #include<cmath>
14 #include<stdlib.h>
15 #include<vector>
16 #include<stack>
17 #include<set>
18 #define FOR(x, b, e)  for(int x=b;x<=(e);x++)
19 #define REP(x, n)     for(int x=0;x<(n);x++)
20 #define mp            make_pair
21 #define INF 1e7
22 #define MAXN 100010
23 #define maxn 1000010
24 #define Mod 1000007
25 #define N 1010
26 using namespace std;
27 typedef long long LL;
28
29 set<pair<int,int> > sa,sb;
30 int dp[MAXN],a[MAXN];
31 int n, x, s, l;
32 int main()
33 {
34     cin >> n >> s >> l;
35     FOR(i, 1, n) cin >> a[i];
36     dp[0] = 0;
37     FOR(i, 1, n) {
38         //每次加入一个数字
39         sa.insert(mp(a[i],i));
40         //第i位置,若不符合题意,则删去当前位置最左端的状态、数字
41         while (sa.rbegin()->first - sa.begin()->first > s) {
42             sb.erase(mp(dp[x],x));
43             x++;
44             sa.erase(mp(a[x],x));
45         }
46         //满足要求的话,作为有效状态加入集合sb中
47         if (i >= l && sa.size() >= l && dp[i-l] >= 0)
48             sb.insert(mp(dp[i-l],i - l));
49         if (sb.empty())
50             dp[i] = -1;
51         else
52             dp[i] = sb.begin()->first + 1;
53     }
54     cout << dp[n] << endl;
55     return 0;
56 }
时间: 2024-08-27 21:50:03

D - Strip的相关文章

python中strip、startswith、endswith

strip(rm)用来删除元素内的空白符: rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符 startswith.endswith用来查找开头或结尾条件的元素 例子: 1 li = ["alec", " aric", "Alex", "Tony", "rain"] 2 tu = ("alec", " aric", "Alex&

Python误区之strip,lstrip,rstrip

最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用targetStr.lstrip(")"),发现在 将处理完的数据插入到数据库时会出现编码报错,于是在网上搜到了这个帖子.出现上述编码错误问题的原因 是我对lstrip函数的理解错误,权威的解释如下: str.lstrip([chars]) Return a copy of the string with leading characters removed. The chars argument is a string

Python str.strip()函数

下面的英文说明是官方给出: string.strip(s[, chars]) Return a copy of the string with leadingand trailing characters removed. If chars is omitted orNone, whitespace characters are removed. If given and not None, chars must be astring; the characters in the string

python strip()函数介绍

函数原型 声明:str为字符串,s为要删除的字符序列 str.strip(s)        删除str字符串中开头.结尾处,位于 s删除序列的字符 str.lstrip(s)       删除str字符串中开头处,位于 s删除序列的字符 str.rstrip(s)      删除str字符串中结尾处,位于 s删除序列的字符 注意: 当s为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ') 1 >>> str = "abaaba" 2 >

python print和strip

在使用这两个模块时犯过错误,总结如下: 1.print print在打印时会自动加上换行,例如: >>> for i in xrange(1,5): ... print i ... 1 2 3 4 如果想屏蔽换行,则在参数后加上逗号,,打印时会用空格分隔,例如: >>> for i in xrange(1,5): ... print i, ... 1 2 3 4 2.strip() split是用来去除字符串首位的空白字符的,空白字符包括tab.空格和换行,所以注意如果

裁剪可执行程序的体积strip

strip删除-g生成的程序调试连接表的信息对于可执行文件,使用命令strip之后,体积只有原来的九分之一strip命令 The strip command removes the symbol table SHT_SYMTAB and its associated string table, debugging information, and line number information from ELF object files. That  is, besides the symbol

python 中join()函数strip() 函数和 split() 函数的详解及实例

1.join()函数 Python中有join()和os.path.join()两个函数,具体作用如下: join():                连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 语法:  'sep'.join(seq) 参数说明sep:分隔符.可以为空seq:要连接的元素序列.字符串.元组.字典上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串 返回值:返回一个以分隔符sep连接各个元素后生成的字符串 os.p

python(5)字符串处理 (sub,replace,find,index,upper,strip)

一,sub和replace的用法 re.sub 函数进行以正则表达式为基础的替换工作 re.sub替换到目标字符串中的a,b或者c,并全部替换 1 >>> import re 2 >>> re.sub('[abc]','o','Mark') 3 'Mork' 4 >>> re.sub('[abc]','o','caps') 5 'oops' 6 >> replace 用法介绍: 1 >>> a 2 'asds23DFG34

python中strip()函数的理解

1.strip()函数 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) :删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) :删除s字符串中开头处,位于 rm删除序列的字符 s.rstrip(rm) :删除s字符串中结尾处,位于 rm删除序列的字符 现在来分析s.strip(rm)这个函数. 现在假设s='abcd' 则 s.strip('bd')----->'abc' 而s.strip('ba')和s.strip('ab')的结果是一样

strip 命令的使用方法

用途 通过除去绑定程序和符号调试程序使用的信息,降低扩展公共对象文件格式(XCOFF)的对象文件的大小. 语法 strip [ -V ] [ -r [ -l ] | -x [ -l ] | -t | -H | -e | -E ] [ -X {32 |64 |32_64 }] [ -- ] File ... 描写叙述 strip 命令降低 XCOFF 对象文件的大小.strip 命令从 XCOFF 对象文件里有选择地除去行号信息.重定位信息.调试段.typchk 段.凝视段.文件头以及全部或部分符