CF1097B Petr and a Combination Lock
設f[i][j]表示前i個操作,是否可以組成j這個角度,其中j∈[0,360),可以從f[i-1][j-a[i]]和f[i-1][j+a[i]]轉移過來,每次取一下模即可。
#include<cstdio>
#include<iostream>
using namespace std;
int n;
const int mod=360;
int a[30],f[37][505];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
f[0][0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<360;j++){
f[i][j]|=f[i-1][(j+a[i])%mod];
f[i][j]|=f[i-1][(j-a[i]+mod)%mod];
}
}
if(f[n][0]) printf("YES");
else printf("NO");
return 0;
}
CF1081C Colorful Bricks
設f[i][j]表示前i個磚塊,有j個磚塊和前面的顏色不同,注意邊界,f[i][0]=m,轉移分兩種情況:i這個磚塊顏色和i-1顏色相同或不同:f[i][j]=f[i-1][j]+f[i-1][j-1]*(m-1)。
不要忘記開longlong
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll n,m,k;
ll f[2005][2005];
int main(){
scanf("%lld%lld%lld",&n,&m,&k);
for(int i=1;i<=n;i++){
f[i][0]=m;
for(int j=1;j<=min((ll)i-1,k);j++){
f[i][j]=f[i-1][j]+f[i-1][j-1]*(m-1);
f[i][j]%=mod;
}
}
printf("%lld",f[n][k]);
return 0;
}
CF1083A The Fair Nut and the Best Path
樹形dp,在dp過程中可以保證不會出現題目所說的負的,因爲如果有,我們也不會選。
f[u]表示鏈頂爲u的最大值。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=300005;
int n,tot,head[N],max1,max2;
ll f[N],ans,a[N];
struct node{
int to,next,w;
}edge[N<<1];
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].w=w;
head[u]=tot++;
}
void dfs(int u,int fa){
f[u]=a[u];
ans=max(ans,a[u]);
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa) continue;
dfs(v,u);
ans=max(ans,f[u]+f[v]-edge[i].w);
f[u]=max(f[u],a[u]+f[v]-edge[i].w);
}
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
}
dfs(1,0);
printf("%lld",ans);
return 0;
}
CF1092F Tree with Maximum Cost
換根dp,首先以1爲根節點求出答案,然後換根,這樣在換根的時候保證了換來的位置算出的答案是正確的,然後轉移即可。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N=200005;
typedef long long ll;
int n,tot,head[N];
ll sz[N],a[N];
ll f[N];
ll ans;
struct node{
int to,next;
}edge[N<<1];
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int fa,int dep){
sz[u]=a[u];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa) continue;
f[1]+=a[v]*dep;
dfs(v,u,dep+1);
sz[u]+=sz[v];
}
}
void dfs2(int u,int fa){
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa) continue;
f[v]=f[u]-sz[v]+sz[1]-sz[v];
dfs2(v,u);
}
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs(1,0,1);
dfs2(1,0);
for(int i=1;i<=n;i++) if(f[i]>ans) ans=f[i];
printf("%lld",ans);
return 0;
}
P2986 [USACO10MAR]Great Cow Gathering G
換根dp
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=100005;
typedef long long ll;
int n,c[N];
int head[N],tot;
ll sz[N],f[N];
ll ans;
struct node{
int to,next,w;
}edge[N<<1];
void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].w=w;
head[u]=tot++;
}
void dfs(int u,int fa){
sz[u]=c[u];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa) continue;
dfs(v,u);
sz[u]+=sz[v];
f[u]+=f[v]+sz[v]*edge[i].w;
}
}
void dfs2(int u,int fa){
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa) continue;
f[v]=f[u]-edge[i].w*sz[v]+edge[i].w*(sz[1]-sz[v]);
dfs2(v,u);
}
}
int main(){
memset(head,-1,sizeof(head));
ans=1e17;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&c[i]);
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w); add(v,u,w);
}
dfs(1,0);
dfs2(1,0);
for(int i=1;i<=n;i++) if(ans>f[i]) ans=f[i];
printf("%lld",ans);
return 0;
}