Analog school race (c) (9.24)

Tucao: la la la, hale and vegetables and weak, fill CF recently convinced they're silly question

Little odd mining 2 (mining) 
[title] background 
little bit strange spacecraft opens up unlimited endurance + precise acquisition mode! To this it shipped ore to ore market sources floodlight, the upgrade to unlimited non-probabilistic spacecraft engines. 

[Problems] Description 
is now m + . 1 planet, from left to right numbered 0 to m, the first planet small odd number 0. 
N has the ore, ore body at the i-th unit ore ai, bi on the first planet. 
As the spacecraft using an older engine jump every time it can only move from planet to No. x of x number + x + planet or 7th planet. Every planet a little odd will go all the ore mining on the planet, seeking the maximum number of ore can be taken to the odd small. 
Note that small odd number m does not have to reach the final planet. 

[Input format 
of the first row two integers n, m. 
Next n lines of two integers ai, bi. 
 
[] Output format 
output line an integer representing the required results. 

[Sample input] 
. 3  13 is 
100  . 4 
10  . 7 
. 1  . 11 

[output] Sample 
101 

[explain] Sample 
first from 0 to 4, from 4 to 11 times a second, to a total of 101 units ore mining. 

Range [] data 
For a 20 % of the data n =. 1 , m <= 10 ^ . 5 
for a 40 % data n-<= 15 , m <= 10 ^ . 5 
to 60 percent of the data m <= 10 ^ . 5 
to 100 % of the data n-<= 10 ^ . 5 , m <= 10 ^ . 9 , . 1 <= AI <= 10 ^ . 4 , . 1 <= BI <= m
View Code

first question:

Obviously sixtieth white to, the DP will be studied. . .

Talk about positive solutions and let us go up at first I felt like jumping the stones that question, so we all consider to coordinate compression, which significantly in emmmm

So we suddenly idea came, is not like Oscar doubts, hey, this is I think the most exquisite places

We turn first to step out two numbers can be represented 4,7 out, we found out that he does not represent the minimum is 17

So take this opportunity to carry out violent transfer, consider how to transfer previous over, maintain a maximum array can qwq

Time complexity of O (n);

#include<bits/stdc++.h>
#define I inline
using namespace std;
#define LL long long
const int N=1e5+7;
LL mx[N],dp[N],w[N];
int m,n;
LL ans;
struct node
{
    int pos,cost;
    bool operator<(const node &x) const
    {
        return pos<x.pos;
    }
} t[N];
bool vis[19];
I int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
    freopen("mining.in","r",stdin);
    freopen("mining.out","w",stdout);
    n=read();m=read();
    for (int i=1;i<=n;i++) t[i].cost=read(),t[i].pos=read();
    sort(t+1,t+n+1);
    vis[0]=true;
    for (int i=1;i<=19;i++)
    if (vis[i-4]||vis[i-7]) vis[i]=true;
    for (int i=1;i<=n;i++)
    {
        int k=1;
        while (true)
        {
            int p=i-k;k++;
            if (p<0) break;
            int dis=t[i].pos-t[p].pos;
            if (dis>=18) 
            {
                dp[i]=max(dp[i],mx[p]+t[i].cost);
                break;
            }
            else if (vis[dis]) dp[i]=max(dp[i],dp[p]+t[i].cost);
            
        }
        mx[i]=max(mx[i-1],dp[i]);
    }
    LL ans=0;
    for (int i=1;i<=n;i++) ans=max(ans,mx[i]);
    printf("%lld\n",ans);
    return 0;
}
View Code

The second question:

At first I only wrote a violent search is expected thirtieth,

But they looked at the size of the matrix element inside, Oh, very strange, only 30 or less, it must be a breakthrough

So we are also ready to come out solution

dp [i] [j] [k] represents the arrival coordinates (i, j) at this point, the sum of the square of the element as the element k and

We have to get rid of his average after-effects model, the original formula for transformation

Then the answer is difficult Release: min ((n + m + 1) * dp [i] [j] [k] -k * k)

Transfer to direct violence

 

#include<bits/stdc++.h>
using namespace std;
int n,m,T;
int a[50][50];
int dp[50][50][2000];
int ans,maxn;
void init()
{
    ans=1<<30;
    memset(dp,0x3f,sizeof(dp));
}
int sqr(int x) {return x*x;} 
int main()
{
    freopen("matrix.in","r",stdin); 
    freopen("matrix.out","w",stdout); 
    scanf("%d",&T);
    while(T--)
    {
        init(); 
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        dp[1][1][a[1][1]]=sqr(a[1][1]);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                for(int k=a[i][j];k<=1800;k++)
                    dp[i][j][k]=min(dp[i][j][k],min(dp[i-1][j][k-a[i][j]],dp[i][j-1][k-a[i][j]])+sqr(a[i][j]));
        for(int i=a[n][m];i<=1800;i++)
            if(dp[n][m][i]<1061109567)
                ans=min(ans,dp[n][m][i]*(n+m-1)-i*i);
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

Third question:

To be honest, really can not write out. . . . . . . . (Hale is silly the X- )

We consider it part points

For three out: for each point violent direct calculations, the time complexity of O (n ^ 2);

For the case of M = 1: covert we consider the effect of this tree for an edge, each reduction was not difficult to find, on both sides of the current difference between the left and right sides of the node

        We handpicked 1 as the root node, the first violence find answers to your questions, you can then transfer

        Time complexity: O (n) 

#include<bits/stdc++.h>
#define I inline
using namespace std;
const int N=2e5+7;
int m,n,ans[N],cnt,head[N],M,size[N];
struct edge
{
    int nx,to,dis;
} e[N];
void add_edge(int a,int b,int dist)
{
    cnt++;e[cnt].nx=head[a];e[cnt].to=b;e[cnt].dis=dist;head[a]=cnt;
}
I int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
I void write(int x)
{
    if(x<0){putchar('-');x=-x;}
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
void dfs1(int x,int fa,int dis,int p)
{
    for (int i=head[x];i;i=e[i].nx)
    {
        int y=e[i].to;
        if (y==fa) continue;
        ans[p]+=(dis+e[i].dis)^M;
        dfs1(y,x,dis+e[i].dis,p);
    }
}
void solve1()
{
    for (int i=1;i<=n;i++)
    dfs1(i,0,0,i);
    for (int i=1;i<=n;i++) write(ans[i]),puts("");
}
void dfs(int x,int fa)
{
    size[x]=1;
    for (int i=head[x];i;i=e[i].nx)
    {
        int y=e[i].to;
        if (y==fa) continue;
        dfs(y,x);
        size[x]+=size[y];
    }
}
void dfs2(int x,int fa)
{
    for (int i=head[x];i;i=e[i].nx)
    {
        int y=e[i].to;
        if (y==fa) continue;
        ans[y]=ans[x]+e[i].dis*size[y];
        dfs2(y,x);
    }
}
void solve2()
{
    dfs1(1,0,0,1);
    dfs(1,0);
    for (int i=1;i<=n;i++) size[i]=n-2*size[i];
    dfs2(1,0);
    for (int i=1;i<=n;i++) write(ans[i]),puts("");
}
int main()
{
    freopen("warehouse.in","r",stdin);
    freopen("warehouse.out","w",stdout);
    n=read(),M=read();
    for (int i=1;i<n;i++)
    {
        int x=read(),y=read(),z=read();
        add_edge(x,y,z);add_edge(y,x,z);
    }
    if (M!=0) solve1();
    else solve2();
    return 0;
}
View Code

 

:( positive solutions really not, Gangster code of the last film )

We still consider that a side impact to a tree, XOR affects only the last four, so we directly to the side of the right side demolished

And then finally sum up just fine, ( so retarded things do not come out ...)

#include<bits/stdc++.h>
#define LL long long
#define I inline
using namespace std;
const int N=2e5+7;
int M,n,cnt,head[N];
int ans[N],g[N][16],dp[N],size[N];
I int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void write(int x)
{
    if(x<0){putchar('-');x=-x;}
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
struct edge
{
    int nx,to,dis;
} e[N];
void add_edge(int a,int b,int dis)
{
    cnt++;e[cnt].nx=head[a];e[cnt].to=b;e[cnt].dis=dis;head[a]=cnt;
}
void dfs1(int x,int fa)
{
    for (int i=head[x];i;i=e[i].nx)
    {
        int y=e[i].to;if (y==fa) continue;
        dfs1(y,x);
        dp[x]+=dp[y]+e[i].dis/16;
        g[x][e[i].dis%16]++;
        for (int j=0;j<16;j++)
        {
            int k=j+e[i].dis;
            dp[x]+=k/16*g[y][j];
            g[x][k%16]+=g[y][j];
        }
    }
}
void dfs2(int x,int fa)
{
    for (int i=head[x];i;i=e[i].nx)
    {
        int y=e[i].to;if (y==fa) continue;
        int tmp=dp[x]-dp[y];
        for (int j=0;j<16;j++)
        {
            int k=j+e[i].dis;
            tmp-=(k/16)*g[y][j];
            size[k%16]=g[x][k%16]-g[y][j];
        }
        size[e[i].dis%16]--;
        dp[y]+=tmp;
        g[y][e[i].dis%16]++;
        for (int j=0;j<16;j++)
        {
            int k=j+e[i].dis;
            dp[y]+=k/16*size[j];
            g[y][k%16]+=size[j];
        }
        dfs2(y,x);
    }
}
int main()
{
    freopen("warehouse.in","r",stdin);
    freopen("warehouse.out","w",stdout);
    n=read(),M=read();
    for (int i=1;i<n;i++)
    {
        int x=read(),y=read(),z=read();
        add_edge(x,y,z);add_edge(y,x,z);
    }
    dfs1(1,0);
    dfs2(1,0);
    for (int i=1;i<=n;i++)
    {
        LL ans=dp[i]*16;
        for (int j=0;j<16;j++) ans+=(j^M)*g[i][j];
        write(ans);puts("");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Hale522520/p/11590380.html