codeforces 271E 离散化+线段树,dp优化
来源:互联网 发布:airplay镜像 windows 编辑:程序博客网 时间:2024/05/16 15:25
Marmot found a row with n pillars. The i-th pillar has the height of hi meters. Starting from one pillar i1, Marmot wants to jump on the pillars i2, ..., ik. (1 ≤ i1 < i2 < ... < ik ≤ n). From a pillar i Marmot can jump on a pillar j only if i < j and |hi - hj| ≥ d, where |x| is the absolute value of the number x.
Now Marmot is asking you find out a jump sequence with maximal length and print it.
The first line contains two integers n and d (1 ≤ n ≤ 105, 0 ≤ d ≤ 109).
The second line contains n numbers h1, h2, ..., hn (1 ≤ hi ≤ 1015).
The first line should contain one integer k, the maximal length of a jump sequence.
The second line should contain k integers i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n), representing the pillars' indices from the maximal length jump sequence.
If there is more than one maximal length jump sequence, print any.
5 21 3 6 7 4
41 2 3 5
10 32 1 3 6 9 11 7 3 20 18
61 4 6 7 8 9
In the first example Marmot chooses the pillars 1, 2, 3, 5 with the heights 1, 3, 6, 4. Another jump sequence of length 4 is 1, 2, 4, 5.
#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <functional>#include <sstream>#include <iomanip>#include <cmath>#include <cstdlib>#include <ctime>#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;//typedef pair<int,int> pii; #define INF 1e16#define MAXN 100005#define MAXM 100const int maxn = 100005;const int mod = 1000000007 ;#define eps 1e-6#define PI 3.1415926535897932384626433#define rep(i,n) for(int i=0;i<n;i++)#define rep1(i,n) for(int i=1;i<=n;i++)#define scan(n) scanf("%d",&n)#define scan2(n,m) scanf("%d%d",&n,&m)#define scans(s) scanf("%s",s);#define ini(a) memset(a,0,sizeof(a))#define FILL(a,n) fill(a,a+maxn,n)#define out(n) printf("%d\n",n)//ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}using namespace std;typedef pair<int,int> pii;#define mk(n,m) make_pair(n,m)#define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1ll h[maxn],val[maxn];int dp[maxn];int pre[maxn];pii Max[maxn<<2];void pushup(int rt){if(Max[rt<<1].first <= Max[rt<<1|1].first)Max[rt] = Max[rt<<1|1];elseMax[rt] = Max[rt<<1];}void update(int L,int R,int c,int ps,int l,int r,int rt){if (L <= l && r <= R) {Max[rt] = mk(c,ps);return;}int m = (l+r)>>1;if(L <= m) update(L,R,c,ps,lson);if(R > m) update(L,R,c,ps,rson);pushup(rt);}pii query(int L,int R,int l,int r,int rt){if( R < L) return mk(0,0);if (L <= l && r <= R) {return Max[rt];}int m = (l+r)>>1;pii ret = mk(0,0);if(L <= m) ret = max(ret,query(L,R,lson));if(R > m) ret = max(ret, query(L,R,rson));return ret;}int n,d;vector<int> ans;int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endifwhile(~scanf("%d%d",&n,&d)){ini(Max);ini(dp);ini(pre);rep1(i,n){scanf("%I64d",&h[i]);val[i] = h[i];}sort(val+1,val+1+n);int V = unique(val+1,val+1+n) - val;rep1(i,n){int l = upper_bound(val+1,val+1+V,h[i]-d) - val - 1;int r = lower_bound(val+1,val+1+V,h[i]+d) - val;pii ret = max(query(1,l,1,V,1),query(r,V,1,V,1));dp[i] = ret.first + 1;pre[i] = ret.second;int pos = lower_bound(val+1,val+1+V,h[i]) - val;update(pos,pos,dp[i],i,1,V,1);}int mx = 0, mxid;rep1(i,n){if(mx < dp[i]){mx = dp[i];mxid = i;}}cout<<mx<<endl;ans.clear();ans.push_back(mxid);while(pre[mxid] != 0){ans.push_back(pre[mxid]);mxid = pre[mxid];}for(int i=mx-1;i>=0;i--){printf("%d%c",ans[i],i==0?'\n':' ');}}return 0;}
- codeforces 271E 离散化+线段树,dp优化
- codeforces 777E dp+线段树+离散化
- Codeforces Round #271 (Div. 2) E 离散化+线段树
- Codeforces 474E Pillars【Dp+线段树+二分+离散化】好题~
- 【dp+离散化+线段树优化】Paint
- codeforces #343 div2 D. Babaei and Birthday Cake(DP+离散化+线段树优化)
- codeforces E. Trains and Statistic 线段树优化dp
- Codeforces Round #271 (Div. 2) E Pillars(dp+线段树优化)
- Codeforces Round #271 (Div. 2)E. Pillars(dp+线段树优化)
- Codeforces Round #271 (Div. 2) E. Pillars(线段树优化DP)
- UESTC 1501 - Defense Lines 离散化+线段树优化DP
- hdu 3450 离散化+dp+线段树优化
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+离散化优化DP)
- codeforces 629D-Babaei and Birthday Cake(dp && 线段树或树状数组离散优化)
- codeforces 474E Pillars(线段树+dp)
- Codeforces 474E Pillars dp+线段树
- Codeforces 474E Pillars dp+线段树
- Codeforces Round #111 (Div. 2) E题 Buses and People 线段树+离散化
- 黑马程序员_Java基础篇(四)——内部类总结
- mfc中DC相关
- mysql与Java连接 以及有关操作
- ARM bluez协议栈移植
- Instagram工程师教你如何改善App的性能
- codeforces 271E 离散化+线段树,dp优化
- Java数据结构和算法(五)——队列
- 茄子快传使用测评
- 在android中创建bitmap避免内存不足的方法
- 存储圈装逼必上TOP 10网站
- java-正则表达式判断手机号
- hdu4429 Split the Rectangle(LCA)
- nginx学习十一 nginx启动流程
- Acdream 1126 Beautiful People(最长上升子序列,dp+线段树优化)