codeforces #271(div2) E

题意:给出n,d,和一个数列,求一个最长子序列,相邻的两个数绝对值>=d;,输出长度和位置。

思路:dp[i]表示以当前这个数字为终点形成的最大长度,这样的话找位置的时候就是往前找dp[i]-j(1<=j<dp[i]),这里用到线段树来优化

  对于一个数字A,他的前一个数字的区间在(1,A-d),(A+d,10^15),所以我们按照数字的大小排序,hsah一下,求个区间最大值,

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 const int INF = 0x3f3f3f3f;
  5 const int N = 100005;
  6
  7 ll h[N],hh[N];
  8 struct node{
  9     int l,r,Max;
 10 }e[N*4];
 11 map<ll ,int >a;
 12 map<ll ,int >::iterator it;
 13 ll dp[N];
 14
 15 void build(int L,int R,int x){
 16     e[x].l=L;
 17     e[x].r=R;
 18     e[x].Max=0;
 19     if(L==R) return ;
 20     int mid=(L+R)>>1;
 21     build(L,mid,2*x);
 22     build(mid+1,R,2*x+1);
 23 }
 24
 25 void update(int y,int val,int x){
 26     if(e[x].l==y&&e[x].r==y) {
 27         e[x].Max=val;return ;
 28     }
 29     int mid=(e[x].l+e[x].r)>>1;
 30     if(y<=mid) update(y,val,2*x);
 31     else update(y,val,2*x+1);
 32     e[x].Max=max(e[x*2].Max,e[2*x+1].Max);
 33 }
 34
 35 ll query(int L,int R,int x){
 36     if(e[x].l>=L&&e[x].r<=R) {
 37         return e[x].Max;
 38     }
 39
 40     int mid=(e[x].l+e[x].r)>>1;
 41
 42     if(mid>=R) return query(L,R,2*x);
 43     else if(L>mid) return query(L,R,2*x+1);
 44     else return max(query(L,mid,2*x),query(mid+1,R,2*x+1));
 45     e[x].Max=max(e[x*2].Max,e[2*x+1].Max);
 46 }
 47 int wei[N];
 48 int main(){
 49     int n,d;
 50     scanf("%d%d",&n,&d);
 51     for(int i=1;i<=n;i++){
 52         scanf("%lld",&h[i]);
 53         hh[i]=h[i];
 54     }
 55     sort(h+1,h+1+n);
 56     int x=0;
 57     for(int i=1;i<=n;i++){
 58         if(a[h[i]]==0) a[h[i]]=++x;
 59     }
 60     build(1,x,1);
 61     for(int i=1;i<=n;i++){
 62         dp[i]=1;
 63         it=a.upper_bound(hh[i]-d);//得找hh[i]-d及以前的数造成的最大值,upper指向第一个>该值的位置//
 64         if(it!=a.begin()){
 65             it--;
 66             int xx=it->second;
 67             ll Max=query(1,xx,1);
 68             dp[i]=max(dp[i],Max+1);
 69         }
 70         it=a.lower_bound(hh[i]+d);//得找hh[i]+d及后面的数字,lower指向第一个>=该值的位置//
 71         if(it!=a.end()){
 72             int xx=it->second;
 73             ll Max=query(xx,x,1);
 74             dp[i]=max(dp[i],Max+1);
 75         }
 76         update(a[hh[i]],dp[i],1);
 77     }
 78     ll ss=1;
 79     int z;
 80     for(int i=1;i<=n;i++){
 81         if(dp[i]>ss){
 82             ss=dp[i];z=i;
 83         }
 84     }
 85     printf("%d\n",ss);
 86     int t=0;
 87     wei[++t]=z;
 88     for(int i=n;i>=1;i--){
 89      // cout<<dp[i]<<" "<<hh[i]<<" "<<
 90         if(dp[i]==ss-1&&abs(hh[z]-hh[i])>=d){
 91             wei[++t]=i;
 92             z=i;
 93             ss--;
 94         }
 95     }
 96     for(int i=t;i>=1;i--){
 97         printf("%d ",wei[i]);
 98     }
 99     printf("\n");
100     return 0;
101 }
时间: 2024-08-29 12:56:20

codeforces #271(div2) E的相关文章

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

Codeforces #246(div2)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

Codeforces #245(div2)

A:A. Points and Segments (easy) 题目看了n久,开始觉得尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让你找一种选择方案使得m个区间内的蓝球和红球数量之差不超过1. 开始想过用dfs,不过这只是div2的A题而已.. 然后想了下,直接输出010101序列不就可以么. 交了一发,发现要先排个序,再输出就可以了. AC代码: #include<iostream> #include<cstdio&g

codeforces#327 div2

codeforces#327 div2 这场状态不好有点可惜,题目都不难,而且很好.. A题:水题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef lo

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

Codeforces 258 Div2

A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R使得变成单调递增. 如果一开始就是递增的,那么直接输出L...R就是1 1,交换一个就行了:否则判断中间是否有且一段单调递减,且两端交换后使得数组递增. 代码: 1 //Template updates date: 20140718 2 #include <iostream> 3 #include

Captain Marmot(Codeforces Round #271 div2) C

Captain Marmot Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, e

Codeforces #263 div2 解题报告

比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注册的时候很想好好的做一下,但是网上喝了个小酒之后,也就迷迷糊糊地看了题目,做了几题,一觉醒来发现rating掉了很多,那个心痛啊! 不过,后来认真的读了题目,发现这次的div2并不是很难! 官方题解:http://codeforces.com/blog/entry/13568 A. Appleman and Easy Task 解析: 一个水题,判断每个细胞周围是否都是有偶数个相邻细胞.   代码