dp&& Backpack _ Template __dp Backpack _ template

Source: Internet
Author: User

1. Multiple backpacks

void Zoreonepack (int val,int cost)
{for
    (int i=v;i>=cost;i--)
    {
        if (dp[i-cost]+val>dp[i))
        {
            dp[i]=dp[i-cost]+val;
}
}} void Completepack (int val,int cost)
{for
    (int i=cost;i<=v;i++)
    {
        Dp[i]=max (Dp[i],dp[i-cost] +val);
    }
void multipack (int val,int cost,int num)
{
    if (num*cost>=v)
    {
        completepack (val,cost);
    }
    else
    {
        int k=1;
         while (K<num)
         {
             zoreonepack (k*val,k*cost);
             num-=k;k+=k;
         }
         Zoreonepack (num*val,num*cost);
    }


2.O (n^2) TSP

#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace
Std
#define INF 0x3f3f3f3f int n,d[1005],dp[1005][1005];
    int dis (int a,int b) {int tmp=abs (d[a]-d[b]);
return min (tmp,360-tmp);
    int TSP_DP () {Dp[2][1]=dis (1,2);

        for (int i = 3; I <= n + 1; i++) {dp[i][i-1] = INF;
            for (int j = 1; J < I-1 J + +) {dp[i][i-1] = min (dp[i][i-1], dp[i-1][j] + dis (i, j));
        DP[I][J] = dp[i-1][j] + dis (i, i-1);
    int ans = INF;
    for (int i = 1; I <= n; i++) ans = min (ans, dp[n+1][i] + dis (n+1, i));
return ans;
    int main () {int t;
    scanf ("%d", &t);
        while (t--) {d[1]=0;
        int ans=0;
        scanf ("%d", &n);
            for (int i=2;i<=n+1;i++) {int A;
            scanf ("%d%d", &a,&d[i]);
            if (i==n+1) ans+=a*800;
        ans+=10;
        } ANS+=TSP_DP (); PriNTF ("%d\n", ans); }
}

3. Packet backpack


        Memset (Dp,0,sizeof (DP));  
        for (int i=1;i<=1005;i++)  
        {  
            memset (dp2,0,sizeof (DP2));  
            for (int j=0;j<=1005;j++) dp2[j]=dp[j];  
            for (int j=0;j<mp[i].size (); j + +)  
            {  
                int num=mp[i][j];  
                for (int k=v;k>=a[num].cost;k--)  
                {  
                    Dp2[k]=max (dp2[k],dp[k-a[num].cost]+a[num].val);  
                }  
            }  
            for (int j=0;j<=1005;j++) Dp[j]=max (Dp[j],dp2[j]);  
        printf ("%d\n", Dp[v]);  

4. Multiply LCA Online


void Dfs (int u,int from)
{for
    (int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if (v==from) continue;
        d[v]=d[u]+1;
        DIST[V]=DIST[U]+E[I].W;
        P[v][0]=u;
        Dfs (V,u);
    }
void init ()
{for
    (int j=1; (1<<j) <=n;j++)
    {for
        (int i=1;i<=n;i++)
        {
            P[i] [J]=p[p[i][j-1]][j-1];
        }
    }
LCA int (int x,int y)
{
    if (D[x]>d[y]) swap (x,y);
    int f=d[y]-d[x];
    for (int i=0; (1<<i) <=f;i++)
    {
        if ((1<<i) &f) y=p[y][i];
    }
    if (x!=y)
    {for
        (int i= (int) log2 (N); i>=0;i--)
        {
            if (P[x][i]!=p[y][i])
            {
                X=p[x][i] ;
                Y=p[y][i];
            }
        X=P[X][0];
    }
    return x;
}

5. Slope Optimization DP

#include <stdio.h> #include <string.h> using namespace std;  
int que[650000];  
int a[650000];  
int sum[650000];  
int dp[650000];  
int n,m;  
int A (int j,int k) {return (Dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k]);  
int B (int j,int k) {return 2* (sum[j]-sum[k]);  
int Val (int i,int j) {return dp[j]+ (Sum[i]-sum[j]) * (SUM[I]-SUM[J)) +m;  
        int main () {while (~scanf ("%d%d", &n,&m)) {sum[0]=0;  
        Memset (Dp,0,sizeof (DP));  
        memset (que,0,sizeof (que));  
        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);  
        for (int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];  
        int head=0,tot=0;  
        que[tot++]=0; for (int i=1;i<=n;i++) {while (Head+1<tot&&a (Que[head+1],que[head)) <=sum[i]*b (que[  
            Head+1],que[head]) head++;  
            Dp[i]=val (I,que[head]); while (Head+1<tot&&a (I,que[tot-1]) *b (que[tot-1],que[tot-2)) <=A (Que[tot-1],que[tot-2]) *b (i,que[tot-1]) tot--;  
        Que[tot++]=i;  
    printf ("%d\n", Dp[n]);   }  
}

monotonic queues

        for (int i=1; i<=n; i++)
        {while
            (s<=e&&q[s].pos<i-m+1) s++;
            while (S<=e&&q[e].val>a[i]) e--;
            E++,q[e].pos=i,q[e].val=a[i];
        }

6.RMQ query interval Maximum minimum value

#include <stdio.h> #include <string.h> #include <cmath> #include <iostream> using namespace St  
D  
int n,q;  
int maxn[200005][20];  
int minn[200005][20];  
    void ST () {int Len=floor (log10 double (n))/log10 (double (2)); for (int j=1;j<=len;j++) {for (int i=1;i<=n+1-(1&LT;&LT;J); i++) {maxn[i][j]=  
            Max (maxn[i][j-1],maxn[i+ (1<< (j-1))][j-1]);  
        Minn[i][j]=min (minn[i][j-1],minn[i+ (1<< (j-1))][j-1]);  
    int main () {scanf ("%d%d", &n,&q);  
        for (int i=1;i<=n;i++) {int tmp;  
        scanf ("%d", &tmp);  
    maxn[i][0]=minn[i][0]=tmp;  
    ST ();  
        for (int i = 1; I <= Q; ++i) {int a,b;  
        scanf ("%d%d", &a, &b);  
        if (a>b) swap (A, b);  
        int len= Floor (log10 double (b-a+1))/log10 (double (2))); printf ("%d\n", Max (Maxn[a][len), maxn[b-(1<<len) +1][len])-min (Minn[a][len], minn[b-(1<<len) +1][len));  
return 0;
 }

7.nlogn LIS


    Memset (Dp,0,sizeof (DP));
    memset (F,0x7f,sizeof (f));
    for (int i = 1; I <= n; ++i)
    {
        int k = Lower_bound (f + 1, f+ 1 + N, a[i])-F;
        Dp[i] = k;
        F[K] = A[i];
    }

The longest descending subsequence


int find (int n,int key)
{
	int left=0;
	int right=n;
	while (Left<=right)
	{
		int mid= (left+right)/2;
		if (Res[mid]>key)
		{
			left=mid+1;
		}
		else
		{
			right=mid-1;
		}
	}
	return to left;
}

int Lis (int a[],int n)
{
	int r=0;
	RES[R]=A[0];
	r++;
	for (int i=1;i<n;i++)
	{
		if (Res[r-1]>a[i])
		{
			res[r]=a[i];
			r++;
		}
		else
		{
			int loc=find (r,a[i]);
			Res[loc]=a[i];
		}
	return r;
}

The longest not diminishing


int slove (int n)
{
    int c=0;
    for (int i=1; i<=n; i++)
    {
        int t=a[i];
        if (i==1) f[++c]=t;
        else
        {
            if (t>=f[c]) f[++c]=t;
            else
            {
                int pos=upper_bound (f+1,f+c,t)-f;//binary finds the address of the first element in the array that is larger than T.
                f[pos]=t
    }
    }} return c;
}


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.