codeforces 278Div1 B题
虚拟参赛的时候没想到是线段树,看到很多人都过了,也蛮着急的。
首先用二分+线段树的方法更新DP[i]:它表示以A[i]为结尾可以最前到哪个位置;
再用线段树计算ans[i]:它表示当前i个A元素可以最少分成多少个pieces,ans[i]=1+min(ans[j]),dp[i]-1<=j<=i-L。
over.
#define N 100000+5 int a[N],dp[N],Ans[N];
struct segment {
int l,r;
int Min,Max,ans;
} seg[*N]; struct diff {
int Max,Min;
diff(int x=,int n=):Max(x),Min(n) {}
}; void build(int p,int l,int r)
{
seg[p].l=l, seg[p].r=r, seg[p].ans=-;;
if (l==r) {
seg[p].Min = seg[p].Max = a[l];
return;
}
build(p<<,l,(l+r)>>);
build((p<<)+,((l+r)>>)+,r); seg[p].Min = min(seg[p<<].Min, seg[(p<<)+].Min),
seg[p].Max = max(seg[p<<].Max, seg[(p<<)+].Max);
} diff query1(int p,int l,int r)
{
if (l<=seg[p].l && seg[p].r<=r) {
return diff(seg[p].Max,seg[p].Min);
} diff t1,t2;
bool f1=false, f2=false;
if (seg[p<<].r>=l)
f1=true, t1=query1(p<<,l,r);
if (seg[(p<<)+].l<=r)
f2=true, t2=query1((p<<)+,l,r); if (f1) {
if (f2)
return diff(max(t1.Max,t2.Max), min(t1.Min,t2.Min));
else
return t1;
}
else
if (f2)
return t2;
return diff(,);
} int query2(int p,int l,int r)
{
if (l<=seg[p].l && seg[p].r<=r) {
return seg[p].ans;
} int t1=-,t2=-;
if (seg[p<<].r>=l)
t1=query2(p<<,l,r);
if (seg[(p<<)+].l<=r)
t2=query2((p<<)+,l,r); if (t1==-) {
return t2;
}
else {
if (t2==-)
return t1;
else return min(t1,t2);
} } void add(int p,int i,int newans)
{
if (seg[p].l==seg[p].r) {
seg[p].ans=newans;
return;
} if (i<=seg[p<<].r)
add(p<<,i,newans);
else
add((p<<)+,i,newans); if (seg[p<<].ans==-) {
seg[p].ans = seg[(p<<)+].ans;
}
else {
if (seg[(p<<)+].ans==-)
seg[p].ans=seg[p<<].ans;
else
seg[p].ans=min(seg[p<<].ans,seg[(p<<)+].ans);
}
} int main()
{
//freopen("b.txt","r",stdin); int n,s,l;
cin>>n>>s>>l;
for (int i=;i<=n;i++) scanf("%d",&a[i]);
build(,,n);
for (int i=;i<=n;i++) {
int L=,R=i-;
dp[i]=i;
while (L<=R) {
int mid = (L+R)>>;
diff tmp = query1(,mid,i);
if (tmp.Max-tmp.Min<=s) {
R=mid-;
dp[i]=mid;
}
else {
L=mid+;
}
}
} for (int i=;i<=n;i++) {
if (i<l) {
Ans[i]=-;
continue;
}
int tmp=-;
if (i-l> && dp[i]-<=i-l)
tmp=query2(,max(,dp[i]-),i-l);
if (dp[i]==) tmp=; if (tmp==-)
Ans[i]=-;
else
Ans[i]=tmp+, add(,i,Ans[i]);
}
cout<<Ans[n]<<endl; return ;
}
codeforces 278Div1 B题的更多相关文章
- Codeforces VP/补题小记 (持续填坑)
Codeforces VP/补题小记 1149 C. Tree Generator 给你一棵树的括号序列,每次交换两个括号,维护每次交换之后的直径. 考虑括号序列维护树的路径信息和,是将左括号看做 ...
- CodeForces - 427B (模拟题)
Prison Transfer Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- codeforces #261 C题 Pashmak and Buses(瞎搞)
题目地址:http://codeforces.com/contest/459/problem/C C. Pashmak and Buses time limit per test 1 second m ...
- Codeforces & Atcoder神仙题做题记录
鉴于Codeforces和atcoder上有很多神题,即使发呆了一整节数学课也是肝不出来,所以就记录一下. AGC033B LRUD Game 只要横坐标或者纵坐标超出范围就可以,所以我们只用看其中一 ...
- B - Save the problem! CodeForces - 867B 构造题
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
随机推荐
- [React] React Fundamentals: Using Refs to Access Components
When you are using React components you need to be able to access specific references to individual ...
- 【转】针对iOS VS. Android开发游戏的优劣——2013-08-25 17
http://game.dapps.net/gamedev/experience/8670.html 问题:如果你正在一个新工作室开发一款新的平板/手机游戏,你会选择iOS还是Android? 回答: ...
- 获取当前时间日期并格式化--JS
工作当中,总是遇到很多觉得不错的JS脚本.现在觉得还是找个地方记录下来,以后可以随时查看. /** *获取当前时间日期并格式化 */ function getNowDate(){ var mydate ...
- 数据库sharding(scale up to scale out)
sharding是将一个大数据库按照一定规则拆分成多个小数据库的一门技术. 当我们的应用数据量越来越多,访问量越来越大的时候,我们会作何选择?继续提升数据库服务器的性能还是采用一项技术让数据库平滑扩展 ...
- Eclipes中使用BASE64Encoder及BASE64Decoder报错
也没有提示让导包,这是因为Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Deco ...
- 一个transaction异常的处理
11-16 14:13:47.715: W/dalvikvm(16771): threadid=1: thread exiting with uncaught exception (group=0x4 ...
- Java_Iterator-------迭代器配合Listarray使用,具有更多的功能(转载)
转载自:http://www.cnblogs.com/amboyna/archive/2007/09/25/904804.html 迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可 ...
- cocoa pods
# cocoa pods * `CocoaPods` 是 iOS 最常用最有名的类库管理工具 * 作为 iOS 程序员,掌握 `CocoaPods` 的使用是必不可少的基本技能 ## pod 命令汇总 ...
- Codevs 2370 小机房的树
2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为 ...
- (hdu)5546 Ancient Go
Problem Description Yu Zhou likes to play Go with Su Lu. From the historical research, we found that ...