Cattle improve customer CSP-S group before training camp after the game summary 3

Goods collection

Binary answer. Complexity \ (O (n-\ log n-) \) .

Cargo grouping

With costs calculated in advance the idea, consider a new cargo box to install what will happen.

Obviously the cost will be added on the total weight of all goods behind.

\ (60 \) min \ (O (n-2 ^) \) the DP Code:

#include<bits/stdc++.h>
#define LL long long
const int SIZE=100005,INF=0x3F3F3F3F;
int n;
LL W,A[SIZE],sum[SIZE]; 
LL DP[SIZE];

using std::max;
using std::min;

struct Seg_Tree
{
    #define LC(x) (x<<1)
    #define RC(x) (x<<1|1)
    #define Mid ((L+R)>>1)
    LL Max[SIZE*4],Min[SIZE*4];
    void push_up(int x)
    {
        Max[x]=max(Max[LC(x)],Max[RC(x)]);
        Min[x]=min(Min[LC(x)],Min[RC(x)]);
    }
    void Build(int x,int L,int R)
    {
        if(L==R){Max[x]=Min[x]=A[L];return;}
        Build(LC(x),L,Mid);
        Build(RC(x),Mid+1,R);
        push_up(x);
    }
    LL Query_Max(int x,int L,int R,int X,int Y)
    {
        if(L>Y||R<X)return -INF;
        if(L>=X&&R<=Y)return Max[x];
        return max(Query_Max(LC(x),L,Mid,X,Y),Query_Max(RC(x),Mid+1,R,X,Y));
    }
    LL Query_Min(int x,int L,int R,int X,int Y)
    {
        if(L>Y||R<X)return INF;
        if(L>=X&&R<=Y)return Min[x];
        return min(Query_Min(LC(x),L,Mid,X,Y),Query_Min(RC(x),Mid+1,R,X,Y));
    }
}T;

int main()
{
    scanf("%d%lld",&n,&W);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&A[i]);
        sum[i]=sum[i-1]+A[i];
    }
    T.Build(1,1,n);
    memset(DP,0x3F,sizeof(DP));
    DP[0]=0;
    for(int i=1;i<=n;i++)
        for(int k=i-1;k>=0&&sum[i]-sum[k]<=W;k--)
                DP[i]=min(DP[i],DP[k]+sum[n]-sum[k]+T.Query_Max(1,1,n,k+1,i)-T.Query_Min(1,1,n,k+1,i));
    printf("%lld",DP[n]);
    return 0;
}

Terrain computing

Routine questions acyclic directed graph three-membered ring / four yuan it. Because I was too dishes, but also to learn for three / four-membered ring.

[Notes] && three-membered ring counted four-membered ring - LuitaryiJack Park's blog

Statistics The main idea of three / four-membered ring is Meet in at The Middle , that is, put a ring split into two parts. From a start point mark half of the ring , and then tries to match the other half . If you encounter in the process of trying to match the to a marked point, the two halves can then makes up a ring.

(Here should be the BGM at The Middle )

Such a point of view, the time complexity is \ (O (n ^ 2) \) , but if in a certain order, then ring come, the time complexity can be reduced to \ (O (m \ sqrt { m}) \) .

We can give all the ranking points according to the following rules:

  1. Small degree nodes at the front of a large degree of the node.
  2. Same node degree, number of small top surface.

Then, for each of the non-directional edge.

  • If you find the three-membered ring, from rank to rank large even smaller .
  • If you are looking for four-membered ring, from the ranks of even small to large ranking .

Finally, the answer to the statistics, in accordance with the rules directed above, each ring will only be counted once.

  • If looking three-membered ring, then for every \ (U \) , it marks the point \ (V \) . Then enumerate point \ (V \) , then enumeration \ (V \) is the point \ (W \) , if the \ (W \) is labeled, the \ ((u, v, w ) \) form a three-membered ring.

  • If looking four-membered ring, then for each point \ (U \) , enumeration picture in \ (U \) is the point \ (V \) , then enumeration directed graph in \ (V \) in the point \ (W \) . tag \ (W \) . then again the enumeration picture in \ (U \) is the point \ (V \) , enumeration directed graph in \ (V \) is the point \ (W \) , if the \ (W \) is labeled, is formed ( possibly more than one ) four-membered ring. exchange "picture" and "directed graph" enumeration orders are possible. However, in order to ensure that each four-membered ring is only counted once, the above operation necessarily required Rank[w]>Rank[u].

This problem will only count four-membered ring into a four-membered ring summation, the nature of the algorithm does not change, but when "Mark" and the number of weights can be changed.

#include<bits/stdc++.h>
using namespace std;
const int SIZE=100005,Mod=1e9+7;
#define LL long long
#define pb push_back
LL A[SIZE];

int n,m,Deg[SIZE],ID[SIZE],Rnk[SIZE],Cnt[SIZE];
LL Ans,C[SIZE];
vector<int>G1[SIZE],G2[SIZE];

bool cmp(int A,int B)
{
    return Deg[A]==Deg[B]?A<B:Deg[A]<Deg[B];
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%lld",&A[i]);
    int u,v;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        G1[u].pb(v);
        G1[v].pb(u);
    }
    for(int i=1;i<=n;i++)
    {
        ID[i]=i;
        Deg[i]=G1[i].size();
    }
    sort(ID+1,ID+1+n,cmp);
    for(int i=1;i<=n;i++)
        Rnk[ID[i]]=i;
    for(int u=1;u<=n;u++)
        for(int v:G1[u])
            if(Rnk[v]>Rnk[u])
                G2[u].pb(v);
    for(int u=1;u<=n;u++)
    {
        for(int v:G1[u])
            for(int w:G2[v])
                if(Rnk[w]>Rnk[u])
                {
                    Ans=(Ans+C[w]+1LL*Cnt[w]*A[v])%Mod;
                    C[w]=(C[w]+A[u]+A[w]+A[v])%Mod;
                    ++Cnt[w];
                }
        for(int v:G1[u])
            for(int w:G2[v])
                if(Rnk[w]>Rnk[u])
                {
                    C[w]=0;
                    Cnt[w]=0;
                }       
    }
    printf("%lld",Ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/TaylorSwift13/p/11819317.html