zoukankan      html  css  js  c++  java
  • atcoder Educational DP Contest 解题报告

    前言


    传送门

    本套题基本上包含了比较常见的dp类型的基础题。

    可以见识一下不同种类的dp,值得dp非常弱的同学一做。

    但是网上关于这套题的中文题解基本都不全,大部分写满26题的题解都是日文的,所以补题门槛有点高。而且日本人的技能树有点奇怪,有些方法可能跟国内的一些做法上还是有差异的。


    A


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e5+7;
    
    int h[N],dp[N];
    
    int main(){
    	int n=input();
    	for(int i=1;i<=n;i++){
    		h[i]=input();
    	}
    	dp[1]=0,dp[2]=abs(h[1]-h[2]);
    	for(int i=3;i<=n;i++){
    		dp[i]=min(dp[i-1]+abs(h[i-1]-h[i]),dp[i-2]+abs(h[i-2]-h[i]));
    	}
    	cout<<dp[n]<<endl;
    } 
    

    B


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e5+7;
    
    int h[N],dp[N];
    
    int main(){
    	int n=input(),k=input();
    	for(int i=1;i<=n;i++){
    		h[i]=input();
    	}
    	dp[1]=0;
    	for(int i=2;i<=n;i++){
    		dp[i]=0x3f3f3f3f;
    		for(int j=1;j<=k;j++){
    			if(i-j<1) break; 
    			dp[i]=min(dp[i-j]+abs(h[i-j]-h[i]),dp[i]);
    		}
    	}
    	cout<<dp[n]<<endl;
    } 
    

    C


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e5+7;
    
    int a[N],b[N],c[N];
    int dp[N][3];
    
    int main(){
    	int n=input();
    	for(int i=1;i<=n;i++){
    		a[i]=input(),b[i]=input(),c[i]=input();
    		dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a[i];
    		dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b[i];
    		dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c[i];
    	}
    
    	cout<<max(dp[n][0],max(dp[n][1],dp[n][2]))<<endl;
    }
    

    D


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e5+7;
    
    ll dp[N];
    
    int main(){
    	int n=input(),W=input();
    
    	for(int i=1;i<=n;i++){
    		ll w=input(),v=input();
    		for(int j=W;j>=w;j--){
    			dp[j]=max(dp[j-w]+v,dp[j]);
    		}
    	}
    	cout<<dp[W]<<endl;
    }
    

    E


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e5+7;
    
    ll dp[N],v[N],w[N];
    
    int main(){
    	ll n=input(),W=input();
    
    	int V=0;
    
    	for(int i=1;i<=n;i++){
    		w[i]=input(),v[i]=input();
    		V+=v[i];
    	}
    
    	fill(dp+1,dp+1+V,0x3f3f3f3f);
    
    	int Ans=0;
    	dp[0]=0;
    
    	for(int i=1;i<=n;i++){
    		for(int j=V;j>=0;j--){
    			if(j+v[i]>V) continue;
    			dp[j+v[i]]=min(dp[j]+w[i],dp[j+v[i]]);
    		}
    	}
    	for(int i=1;i<=V;i++){
    		if(dp[i]<=W) Ans=i;
    	}
    	
    	cout<<Ans<<endl;
    }
    

    F


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=3007;
    
    int dp[N][N];
    string s,t;
    string res;
    
    int main(){
    	cin>>s>>t;
    
    	for(int i=0;i<s.length();i++){
    		for(int j=0;j<t.length();j++){
    			if(s[i]==t[j]) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
    			dp[i+1][j+1]=max(dp[i+1][j+1],max(dp[i+1][j],dp[i][j+1]));
    		}
    	}
    
    	int x=s.length(),y=t.length();
    	while(x>0&&y>0){
    		if(dp[x][y]==dp[x-1][y]) x--;
    		else if(dp[x][y]==dp[x][y-1]) y--;
    		else res=s[x-1]+res,x--,y--;
    	}
    
    	cout<<res<<endl;
    }
    

    G


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    #define ll long long
    const int N=1e5+7;
    
    vector <int> G[N];
    int n,m;
    stack <int> s;
    int d[N],dis[N];
    int Ans=0;
    
    void bfs(){
    	queue <int> q;
    	for(int i=1;i<=n;i++){
    		if(!d[i]) q.push(i);
    	}
    	while(!q.empty()){
    		int u=q.front();q.pop();
    		for(auto v:G[u]){
    			d[v]--;
    			if(d[v]==0)
    				dis[v]=dis[u]+1,q.push(v);
    		}
    	}
    }
    
    int main(){
    	n=input(),m=input();
    	for(int i=1;i<=m;i++){
    		int u=input(),v=input();
    		G[u].push_back(v);
    		d[v]++;
    	}
    	bfs();
    	for(int i=1;i<=n;i++){
    		Ans=max(Ans,dis[i]);
    	}
    	printf("%d
    ",Ans);
    }
    

    H


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1007;
    const ll mod=1e9+7;
    
    int n,m;
    int dp[N][N];
    string s[N];
    
    int main(){
    	n=input(),m=input();
    	for(int i=1;i<=n;i++){
    		cin>>s[i];
    	}
    
    	dp[1][1]=1;
    
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			if(s[i][j-1]=='#') continue;
    			if(s[i][j-1]=='.')
    				dp[i][j]+=dp[i][j-1],
    				dp[i][j]%=mod;
    			if(s[i-1][j-1]=='.')
    				dp[i][j]+=dp[i-1][j],
    				dp[i][j]%=mod;
    		}
    	}
    
    	printf("%d
    ",dp[n][m]);
    }
    

    I


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=3007;
    
    int n;
    double p[N];
    double dp[N][N];
    double Ans;
    
    int main(){
    	n=input();
    	for(int i=1;i<=n;i++){
    		scanf("%lf",&p[i]);
    	}
    
    	dp[0][0]=1;
    
    	for(int i=1;i<=n;i++){
    		for(int j=0;j<=i;j++){
    			int x=j,y=i-j;
    			if(x) dp[x][y]+=dp[x-1][y]*p[i];
    			if(y) dp[x][y]+=dp[x][y-1]*(1-p[i]);
    		}
    	}
    	int l=n,r=0;
    	while(l>r){
    		Ans+=dp[l][r];
    		l--,r++;
    	}
    	printf("%.10lf
    ",Ans);
    }
    

    J


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=307;
    int a[4],n;
    double dp[N][N][N];
    
    double dfs(int a,int b,int c){
    	if(dp[a][b][c]) return dp[a][b][c];
    	if(a==0&&b==0&&c==0) return 0.0;
    	double res=0;
    	if(a) res+=dfs(a-1,b,c)*a;
    	if(b) res+=dfs(a+1,b-1,c)*b;
    	if(c) res+=dfs(a,b+1,c-1)*c;
    	res+=1.0*n,res/=(a+b+c);
    	return dp[a][b][c]=res;
    }
    
    int main(){
    	n=input();
    	for(int i=1;i<=n;i++)a[input()]++;
    	printf("%.10lf
    ",dfs(a[1],a[2],a[3]));
    }
    

    K


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e5+7;
    int a[107];
    int dp[N];
    
    int main(){
    	int n=input(),k=input();
    	for(int i=1;i<=n;i++){
    		a[i]=input();
    	}
    
    	for(int i=1;i<=k;i++){
    		for(int j=1;j<=n;j++){
    			if(i-a[j]>=0)
    				dp[i]|=!(dp[i-a[j]]);
    		}
    	}
    
    	if(dp[k]) printf("First
    ");
    	else printf("Second
    ");
    }
    

    L


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=3007;
    
    ll dp[N][N],a[N];
    
    int main(){
    	int n=input();
    	for(int i=1;i<=n;i++) a[i]=input();
    
    	for(int i=1;i<=n;i++){
    		for(int j=i;j<=n;j++){
    			int l=j-i+1,r=j;
    			dp[l][r]=max(a[l]-dp[l+1][r],a[r]-dp[l][r-1]);
    		}
    	}
    	printf("%lld
    ",dp[1][n]);
    }
    

    M


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=107,M=1e5+7;
    const ll mod=1e9+7;
    
    ll dp[N][M],sum[M];
    int a[N];
    
    int main(){
    	int n=input(),k=input();
    	
    	for(int i=0;i<n;i++) a[i]=input();
    	for(int i=0;i<=n;i++) dp[i][0]=1;
    	for(int i=1;i<=n;i++){
    		sum[0]=0;
    		for(int j=1;j<=k+1;j++) sum[j]=sum[j-1]+dp[i-1][j-1];
    		for(int j=1;j<=k;j++) dp[i][j]=(sum[j+1]-sum[max(0,j-a[i-1])])%mod;
    	}
    
    	printf("%lld
    ",dp[n][k]);
    }
    

    N


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=407;
    const ll inf=0x3f3f3f3f3f3f3f;
    
    int a[N];
    ll dp[N][N],sum[N];
    
    int main(){
    	int n=input();
    	for(int i=1;i<=n;i++){
    		a[i]=input();
    		sum[i]=sum[i-1]+a[i];
    	}
    
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n-i;j++){
    			int k=j+i;
    			dp[j][k]=inf;
    			ll tmp=sum[k]-sum[j-1];
    			for(int o=j;o<=k;o++){
    				dp[j][k]=min(dp[j][k],dp[j][o]+dp[o+1][k]+tmp);
    			}
    		}
    	}
    
    	printf("%lld
    ",dp[1][n]);
    }
    

    O


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=22;
    const int mod=1e9+7;
    
    int n,a[N][N],dp[1<<N];
    
    int main(){
    	n=input();
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			a[i][j]=input();
    		}
    	}
    
    	dp[0]=1;
    
    	for(int i=0;i<(1<<n);i++){
    		int t= __builtin_popcount(i);
    		for(int j=1;j<=n;j++){
    			if(i&(1<<(j-1))&&a[t][j]){
    				dp[i]=(dp[i]+dp[i^(1<<(j-1))])%mod;
    			}
    		}
    	}
    	printf("%d
    ",dp[(1<<n)-1]);
    }
    

    P


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    #define pb push_back
    
    const int N=1e5+7,mod=1e9+7;
    
    vector <int> G[N];
    ll dp[N][2];
    
    void dfs(int u,int fa){
    	dp[u][0]=dp[u][1]=1;
    	for(auto v:G[u]){
    		if(v==fa) continue;
    		dfs(v,u);
    		dp[u][0]*=(dp[v][0]+dp[v][1]),dp[u][0]%=mod;
    		dp[u][1]*=dp[v][0],dp[u][1]%=mod;
    	}
    }
    
    int main(){
    	int n=input();
    	for(int i=1;i<n;i++){
    		int u=input(),v=input();
    		G[u].pb(v),G[v].pb(u);
    	}
    
    	dfs(1,0);
    
    	printf("%lld
    ",(dp[1][0]+dp[1][1])%mod);
    }
    

    Q


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    #define lowbit(x) x&-x
    
    const int N=2e5+7;
    
    ll dp[N];
    ll a[N],h[N],t[N];
    int n;
    
    void update(int x,ll y){for(;x<=n;x+=lowbit(x)) t[x]=max(t[x],y);}
    ll query(int x){ll res=0;for(;x;x-=lowbit(x)) res=max(res,t[x]);return res;}
    
    int main(){
    	n=input();
    	for(int i=1;i<=n;i++) h[i]=input();
    	for(int i=1;i<=n;i++) a[i]=input();
    
    	for(int i=1;i<=n;i++){
    		dp[i]=query(h[i])+a[i];
    		update(h[i],dp[i]);
    	}
    	printf("%lld
    ",query(n));
    }
    

    R


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=57;
    const ll mod=1e9+7;
    ll n,k;
    struct mat{
    	ll a[N][N];
    	mat(){memset(a,0,sizeof(a));}
    };
    
    mat operator *(mat a,mat b){
    	mat res;
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			for(int k=1;k<=n;k++){
    				res.a[i][j]=(res.a[i][j]+(a.a[i][k]*b.a[k][j])%mod)%mod;
    			}
    		}
    	}
    	return res;
    }
    
    mat powmod(mat a,ll k){
    	mat res;
    	for(int i=1;i<=n;i++) res.a[i][i]=1;
    	while(k){
    		if(k&1) res=res*a;
    		a=a*a;
    		k>>=1;
    	}
    	return res;
    }
    
    
    int main(){
    	n=input(),k=input();
    	mat a;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    			a.a[i][j]=input();
    
    	a=powmod(a,k);
    
    	ll Ans=0;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=n;j++)
    			Ans=(Ans+a.a[i][j])%mod;
    
    	printf("%lld
    ",Ans);
    }
    

    S


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e4+7,M=107;
    const ll mod=1e9+7;
    
    int K[N],D;
    ll dp[N][M],vis[N][M];
    int n;
    
    ll dfs(int pos,int sum,int lim){
    	if(!lim&&vis[pos][sum]) return dp[pos][sum];
    	if(pos==n+1){if(!sum) return 1;return 0;}
    	int mx=lim? K[pos]:9;ll res=0;
    	for(int i=0;i<=mx;i++){
    		res=(res+dfs(pos+1,(sum+i)%D,lim&&(i==mx)))%mod;
    	}
    	if(!lim) dp[pos][sum]=res,vis[pos][sum]=1;
    	return res;
    }
    
    int main(){
    	char ch=getchar();
    	while(ch>='0'&&ch<='9') K[++n]=ch-'0',ch=getchar();
    	D=input();
    	printf("%lld
    ",(dfs(1,0,1)-1+mod)%mod);
    }
    

    T


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=3007;
    const ll mod=1e9+7;
    
    ll dp[N],pre[N],suf[N];
    
    int main(){
    	int n=input();
    	dp[1]=suf[1]=pre[1]=1;
    	for(int i=2;i<=n;i++){
    		char ch=getchar();
    		if(ch=='<') for(int j=1;j<=i;j++) dp[j]=pre[j-1];
    		else for(int j=1;j<=i;j++) dp[j]=suf[j];
    		for(int j=1;j<=i;j++) pre[j]=(pre[j-1]+dp[j])%mod;
    		for(int j=i;j>=1;j--) suf[j]=(suf[j+1]+dp[j])%mod;
    	}
    	printf("%lld
    ",pre[n]);
    }
    

    U


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=20,M=1<<N;
    
    ll st[M],dp[M];
    int a[N][N];
    
    int main(){
    	int n=input();
    	for(int i=0;i<n;i++)
    		for(int j=0;j<n;j++)
    			a[i][j]=input();
    
    	for(int t=0;t<(1<<n);t++){
    		for(int i=0;i<n;i++)
    			for(int j=i+1;j<n;j++){
    				if((t&(1<<i))&&(t&(1<<j)))
    					st[t]+=a[i][j];
    			}
    	}
    
    	for(int t=0;t<(1<<n);t++){
    		for(int t2=t;t2>0;t2=(t2-1)&t){
    			ll tmp=dp[t-t2]+st[t2];
    			dp[t]=max(dp[t],tmp);
    		}
    	}
    
    	printf("%lld
    ",dp[(1<<n)-1]);
    }
    

    V


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    #define pb push_back
    
    const int N=1e5+7;
    
    ll n,mod;
    vector <int> G[N];
    ll dp[N],Ans[N];
    
    void dfs1(int u,int fa){
    	dp[u]=1;
    	for(auto v:G[u]){
    		if(v==fa) continue;
    		dfs1(v,u);
    		dp[u]=dp[u]*(dp[v]+1)%mod;
    	}
    }
    
    void dfs2(int u,int fa){
    	vector<ll> suf;
    	ll tmp=1;
    	suf.pb(1);
    	for(int i=G[u].size()-1;i>=0;i--){
    		tmp=tmp*(dp[G[u][i]]+1)%mod;
    		suf.pb(tmp);
    	}
    
    	tmp=1;
    	int it=suf.size()-2;
    
    	for(auto v:G[u]){
    		Ans[v]=dp[v]*(tmp*suf[it]%mod+1)%mod;
    		if(v!=fa){
    			ll tt=dp[u];
    			dp[u]=tmp*suf[it]%mod;
    			dfs2(v,u);
    			dp[u]=tt;
    		}
    		tmp=tmp*(dp[v]+1)%mod;
    		it--;
    	}
    }
    
    int main(){
    	n=input(),mod=input();
    	for(int i=1;i<n;i++){
    		int u=input(),v=input();
    		G[v].pb(u);
    		G[u].pb(v);
    	}
    
    	dfs1(1,0);
    	dfs2(1,0);
    	Ans[1]=dp[1];
    	
    	for(int i=1;i<=n;i++){
    		printf("%lld
    ",Ans[i]);
    	}
    }
    

    W


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=2e5+7;
    
    ll t[N<<2],lazy[N<<2];
    
    void putdown(int rt){
    	if(lazy[rt]){
    		t[rt<<1]+=lazy[rt];
    		t[rt<<1|1]+=lazy[rt];
    		lazy[rt<<1]+=lazy[rt];
    		lazy[rt<<1|1]+=lazy[rt];
    		lazy[rt]=0;
    	}
    }
    
    void put(int rt){
    	t[rt]=max(t[rt<<1],t[rt<<1|1]);
    }
    
    ll query(int rt,int l,int r,int ql,int qr){
    	if(ql<=l&&r<=qr) return t[rt];
    	putdown(rt);
    	int mid=(l+r)>>1;
    	if(qr<=mid) return query(rt<<1,l,mid,ql,qr);
    	if(mid<ql) return query(rt<<1|1,mid+1,r,ql,qr);
    	return max(query(rt<<1,l,mid,ql,qr),query(rt<<1|1,mid+1,r,ql,qr));
    }
    
    void update(int rt,int l,int r,int ul,int ur,ll x){
    	if(ul<=l&&r<=ur){t[rt]+=x,lazy[rt]+=x;return;}
    	putdown(rt);
    	int mid=(l+r)>>1;
    	if(ul<=mid) update(rt<<1,l,mid,ul,ur,x);
    	if(ur>mid) update(rt<<1|1,mid+1,r,ul,ur,x);
    	put(rt);
    }
    
    struct opr{
    	int l,r,c;
    }a[N];
    bool cmp(opr a,opr b){
    	return a.r<b.r;
    }
    
    int main(){
    	int n=input(),m=input();
    	for(int i=1;i<=m;i++){
    		a[i].l=input(),a[i].r=input(),a[i].c=input();
    	}
    
    	sort(a+1,a+1+m,cmp);
    
    	ll p=0;
    
    	for(int i=1;i<=n;i++){
    		update(1,0,n,i,i,query(1,0,n,0,i-1));
    		while(p<m&&a[p+1].r==i) p++,update(1,0,n,a[p].l,a[p].r,a[p].c);
    	}
    	printf("%lld
    ",query(1,0,n,0,n));
    }
    

    X


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    const int N=1e4+7;
    
    ll dp[N];
    struct node{
    	ll w,s,v;
    }a[1007];
    
    bool cmp(node a,node b){
    	return a.s-b.w>b.s-a.w;
    }
    
    int main(){
    	int n=input();
    	for(int i=1;i<=n;i++){
    		a[i].w=input(),a[i].s=input(),a[i].v=input();
    	}
    
    	sort(a+1,a+1+n,cmp);
    
    	for(int i=1;i<=n;i++){
    		for(int j=a[i].w;j<=10000;j++)
    			dp[min(j-a[i].w,a[i].s)]=max(dp[min(j-a[i].w,a[i].s)],dp[j]+a[i].v);
    		dp[a[i].s]=max(dp[a[i].s],a[i].v);
    	}
    	ll Ans=0;
    	for(int i=0;i<=10000;i++) Ans=max(Ans,dp[i]);
    	printf("%lld
    ",Ans);
    }
    

    Y


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    #define PII pair<ll,ll>
    #define X first
    #define Y second
    #define mp make_pair
    
    const int N=2e5+7;
    const ll mod=1e9+7;
    
    ll fac[N],inv[N];
    
    void pre(){
    	fac[0]=fac[1]=inv[0]=inv[1]=1;
    	for(int i=2;i<N;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    	for(int i=2;i<N;i++) inv[i]=inv[i-1]*inv[i]%mod,fac[i]=fac[i-1]*i%mod;
    }
    
    ll C(int m,int n){
    	return fac[m]*inv[m-n]%mod*inv[n]%mod;
    }
    
    int n,m,k,dp[3007];
    PII a[3007];
    
    int main(){
    	pre();
    	n=input(),m=input(),k=input();
    
    	for(int i=1;i<=k;i++){
    		a[i].X=input(),a[i].Y=input();
    		if((a[i].X==1&&a[i].Y==1)||(a[i].X==n&&a[i].Y==m)) printf("0
    "),exit(0);
    	}
    
    	a[++k].X=n,a[k].Y=m;
    	sort(a+1,a+1+k);
    
    	for(int i=1;i<=k;i++){
    		dp[i]=C(a[i].X+a[i].Y-2,a[i].X-1);
    		for(int j=1;j<i;j++){
    			if(a[j].X<=a[i].X&&a[j].Y<=a[i].Y){
    				ll x=a[i].X-a[j].X,y=a[i].Y-a[j].Y;
    				dp[i]=(dp[i]-dp[j]*C(x+y,x)%mod+mod)%mod;
    			}
    		}
    	}
    	printf("%lld
    ",dp[k]);
    }
    

    Z


    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define ll long long
    ll input(){
    	ll x=0,f=0;char ch=getchar();
    	while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
    	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    	return f? -x:x;
    }
    
    
    struct Line
    {
        ll m,b;
        ll get(ll x)
        {
            return m*x+b;
        }
    };
    
    struct ConvexHull
    {
        int size;
        vector<Line> hull;
        ConvexHull(int maxSize)
        {
            hull=vector<Line>(maxSize+1);
            size=0;
        }
        bool isBad(int l1,int l2,int l3)
        {
            double left=1.0*(hull[l3].b-hull[l1].b)/(hull[l1].m-hull[l3].m);
            double right=1.0*(hull[l1].b-hull[l2].b)/(hull[l2].m-hull[l1].m);
            return left<right;
        }
        void addLine(ll m,ll b)
        {
            hull[size++]=Line{m,b};
            while(size>2&&isBad(size-3,size-2,size-1))
            {
                hull[size-2]=hull[size-1];
                --size;
            }
        }
        ll query(ll x)
        {
            int l=0,r=size-1;
            while(l<r)
            {
                int m=(l+r)/2;
                if(hull[m].get(x)>=hull[m+1].get(x))
                    l=m+1;
                else
                    r=m;
            }
            return hull[l].get(x);
        }
    };
    
    const int N=2e5+7;
    ll n,c,h[N],dp[N];
    
    int main(){
    	n=input(),c=input();
    	for(int i=1;i<=n;i++){
    		h[i]=input();
    	}
    
    	ConvexHull t(N);
    	dp[1]=0;
    	t.addLine(-2*h[1],h[1]*h[1]+dp[1]);
    
    	for(int i=2;i<=n;i++){
    		dp[i]=t.query(h[i])+h[i]*h[i]+c;
    		t.addLine(-2*h[i],h[i]*h[i]+dp[i]);
    	}
    
    	printf("%lld
    ",dp[n]);
    }
    

  • 相关阅读:
    java_oop_方法2
    POJ 3276 Face The Right Way(反转)
    POJ 3276 Face The Right Way(反转)
    POJ 2566 Bound Found(尺取法,前缀和)
    POJ 2566 Bound Found(尺取法,前缀和)
    POJ 3320 Jessica's Reading Problem(尺取法)
    POJ 3320 Jessica's Reading Problem(尺取法)
    POJ 3061 Subsequence(尺取法)
    POJ 3061 Subsequence(尺取法)
    HDU 1222 Wolf and Rabbit(欧几里得)
  • 原文地址:https://www.cnblogs.com/-aether/p/12491551.html
Copyright © 2011-2022 走看看