Hdu 3450 tree-like array optimized DP

Source: Internet
Author: User


Test instructions will not say, hdu2227 almost only this problem is not incremental but the adjacent difference not more than D is actually the same, there are differences;

This problem does not say the range and the size of the relationship between the tree has an effect on the results, so the tree array according to the original ID to seek, because the value is very large, it is still used discretization here the discretization of the feeling and mapping is similar, discretization to find the ID;

When the ID of num "i" is x Yes, the DP "I" indicates the scheme number is obviously DP "I" =sun (dp "J", J<i&&abs (num "J"-num "I") <=d) here in the tree array is the sum "1 to num" I "+d "-sum" 1 to Num "I"-D "is clearly identified by the mapping relationship between num" I "+d and num" I "-D ID (two points find their own written two points feel good rub) again update on OK

#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream>using namespaceStd;#define MOD 9901intNum1[100010],Num2[100010],Mark[100010],Tt,N; int Coun[100010]; int Update (int A ,int B ) {  for(int I=A;I<=N;I+=(I&-I)) {Coun[I]+=B;Coun[I]%=MoD; }  return 0;} int Find (int A ) {  int S=0;  for (int I=A;I>=1;I-=(I&-I)) {S+=Coun[I];S%=MoD; }  return S;} int Searchr (int A ) {  int Left=1,Right=Tt,Now;  while (Left<=Right ) {  int Mid=(Left+Right)/2; if (Mark[Mid]<=A) {Now=Mid;Left=Mid+1; }  Else  { Right=Mid-1; }}  return Now;} int Searchl (int A ) {  int Left=1,Right=Tt;  while (Left<Right ) {  int Mid=(Left+Right)/2; if (Mark[Mid]<A)Left=Mid+1; else if (Mark[Mid]==A )  return Mid; Else Right=Mid; }  return Left;} int Main () {  int I,J,D;  while (~scanf("%d%d",&N,&D)  {  for( I=1;I<=N;I++) {scanf("%d",&Num1[I]);Num2[I]=Num1[I]; }Sort(Num2+1,Num2+1+N);Tt=0;Num2[0]=-1;  for (I=1;I<=N;I + +) {  if( Num2[I]!=Num2[I-1]) {Tt++;Mark[Tt]=Num2[I]; } }Memset(Coun,0 ,sizeof( Coun)); int Sum=0; int X=Searchl(Num1[1]);  for (I=1;I<=N;I + +) {  int Left=Searchl(Num1[I]-D)-1; int Right=Searchr(Num1[I]+D); int Now=Searchl(Num1[I]); int S=Find(Right)-Find(Left);S=(S%MoD+MoD)%MoD;Sum+=S;Sum%=MoD;Update(Now,S+1); }Printf("%d\n",Sum); }  return 0;}

Hdu 3450 tree-like array optimized DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.