zoukankan      html  css  js  c++  java
  • Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp

    E. Pillars
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Input

    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).

    Output

    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.

    Examples
    input
    5 2
    1 3 6 7 4
    output
    4
    1 2 3 5
    input
    10 3
    2 1 3 6 9 11 7 3 20 18
    output
    6
    1 4 6 7 8 9
    Note

    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.

    题意:

    题解:

      1 #pragma comment(linker, "/STACK:102400000,102400000")
      2 #include <bits/stdc++.h>
      3 #include <cstdlib>
      4 #include <cstdio>
      5 #include <iostream>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <algorithm>
      9 #include <cmath>
     10 #include <cctype>
     11 #include <map>
     12 #include <set>
     13 #include <queue>
     14 #include <bitset>
     15 #include <string>
     16 #include <complex>
     17 #define LL long long
     18 #define mod 1000000007
     19 using namespace std;
     20 #define N 200005
     21 #define M 2000006
     22 #define INF 1e18
     23 struct SGT
     24 {
     25     int maxx[N<<2];
     26     void build(int l,int r,int pos)
     27     {
     28         memset(maxx,0,sizeof(maxx));
     29     }
     30     void update(int p,int c,int l,int r,int pos)
     31     {
     32         if(l==r)
     33         {
     34             maxx[pos]=c;
     35             return;
     36         }
     37         int mid=(l+r)>>1;
     38         if(p<=mid)update(p,c,l,mid,pos<<1);
     39         else update(p,c,mid+1,r,pos<<1|1);
     40         maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]);
     41     }
     42     int query(int L,int R,int l,int r,int pos)
     43     {
     44         if(L<=l&&r<=R)return maxx[pos];
     45         int mid=(l+r)>>1;
     46         int ans=0;
     47         if(L<=mid)ans=max(ans,query(L,R,l,mid,pos<<1));
     48         if(R>mid)ans=max(ans,query(L,R,mid+1,r,pos<<1|1));
     49         return ans;
     50     }
     51 }tree;
     52 LL a[N],b[N];
     53 int n;
     54 LL k;
     55 int big(LL x)
     56 {
     57     int pos=lower_bound(b,b+2+n,x)-b;
     58     return pos;
     59 }
     60 int low(LL x)
     61 {
     62     int pos=upper_bound(b,b+2+n,x)-b-1;
     63     return pos;
     64 }
     65 int dp[N];
     66 vector<int>ans;
     67 int main()
     68 {
     69     scanf("%d%lld",&n,&k);
     70     for(int i=1;i<=n;i++)
     71     {
     72         scanf("%lld",&a[i]);
     73         b[i]=a[i];
     74     }
     75     sort(b+1,b+1+n);
     76     b[n+1]=INF;
     77     b[0]=-INF;
     78     for(int i=1;i<=n;i++)
     79     {
     80         LL pre=a[i]-k;
     81         LL nex=a[i]+k;
     82         int pos1=low(pre);
     83         int pos2=big(nex);
     84         int maxx=0;
     85         if(pos1>=1)maxx=max(maxx,tree.query(1,pos1,1,n,1));
     86         if(pos2<=n)maxx=max(maxx,tree.query(pos2,n,1,n,1));
     87         dp[i]=maxx+1;
     88         tree.update(low(a[i]),maxx+1,1,n,1);
     89     }
     90     int maxx=0,pre=-1;
     91     for(int i=1;i<=n;i++){
     92         if(dp[i]>maxx){
     93             maxx=dp[i];
     94             pre=i;
     95         }
     96     }
     97     ans.push_back(pre);
     98     for(int i=pre-1;i>=1;i--){
     99         if(abs(a[i]-a[pre])>=k&&dp[pre]==dp[i]+1){
    100             pre=i;
    101             ans.push_back(pre);
    102         }
    103     }
    104     printf("%d
    ",maxx);
    105     for(int i=maxx-1;i>=0;i--)
    106         printf("%d ",ans[i]);
    107     return 0;
    108 }
  • 相关阅读:
    css的position:absolute
    css元素的margin,padding
    Python---Flask--04--SQLAlchemy
    Python---Flask--03--Web表单
    Python---Flask--02--模板
    Python---Flask--01
    国外程序员整理的 PHP 资源大全
    PHP7 通过yum安装
    Node的安装和进程管理
    在php中实现Redis的订阅与发布
  • 原文地址:https://www.cnblogs.com/hsd-/p/7260809.html
Copyright © 2011-2022 走看看