2020-2021 ACM-ICPC, Asia Nanjing Regional Contest (XXI Open Cup, Grand Prix of Nanjing)

M. Monster Hunter
树形背包dp dp[i][j][k] 表示结点i的子树中有j个节点存活且当前节点i的状态为(0/1)
转移方程:

dp[x][i+j][0]=min(dp[x][i+j][0],dp[x][i][0]+min(dp[to][j][0],dp[to][j][1]));
dp[x][i+j][1]=min(dp[x][i+j][1],dp[x][i][1]+min(dp[to][j][1]+hp[to],dp[to][j][0]));
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e3+10;
vector<int> v[N];
int hp[N],n,w[N],t,dp[N][N][2],sz[N];
void dfs(int x){
	dp[x][1][1]=hp[x];dp[x][0][0]=0;
	for(int i=0;i<v[x].size();i++) dfs(v[x][i]);
	sz[x]=1;
	for(auto to:v[x]){
		for(int i=sz[x];i>=0;i--){
			for(int j=sz[to];j>=0;j--){
				dp[x][i+j][0]=min(dp[x][i+j][0],dp[x][i][0]+min(dp[to][j][0],dp[to][j][1]));
				dp[x][i+j][1]=min(dp[x][i+j][1],dp[x][i][1]+min(dp[to][j][1]+hp[to],dp[to][j][0]));
			}
		}
		sz[x]+=sz[to];
	}
}
int32_t main(){
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&n);
		for(int i=1;i<=n;i++) v[i].clear();
		for(int i=2;i<=n;i++){int x;scanf("%lld",&x);v[x].push_back(i);}
		for(int i=1;i<=n;i++) scanf("%lld",&hp[i]);
		memset(dp,0x3f,sizeof dp);
		dfs(1);
		for(int i=n;i>=0;i--){
			printf("%lld",min(dp[1][i][0],dp[1][i][1]));
			if(i!=0) printf(" ");
		}cout<<endl;
	}
}

Evil Coordinate
这题全排列然后判断一下就好了
贴一份错误代码(找不到错误

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int t,mx,my;
string s;
int cnt[4];
void move(int &x,int &y,char ch){
	if(ch=='R') x++,cnt[0]++;
	else if(ch=='U') y++,cnt[1]++;
	else if(ch=='L') x--,cnt[2]++;
	else y--,cnt[3]++;
}
bool check(string s){
	int x=0,y=0;
	for(int i=0;i<s.size();i++){
		move(x,y,s[i]);
		if((x==mx&&y==my)) return 0;
	}
	return 1;
}
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&mx,&my);
		cin>>s;memset(cnt,0,sizeof cnt);
		int x=0,y=0;
		for(int i=0;i<s.size();i++){
			move(x,y,s[i]);
		}
		if((x==mx&&y==my)||(mx==0&&my==0)){
			cout<<"Impossible"<<endl;
			continue;
		}
		string ans;
		if(mx==0){
			if(my<y){
				for(int i=1;i<=cnt[0];i++) ans+='R';
				for(int i=1;i<=cnt[1];i++) ans+='U';
				for(int i=1;i<=cnt[3];i++) ans+='D';
				for(int i=1;i<=cnt[2];i++) ans+='L';
			}else{
				for(int i=1;i<=cnt[0];i++) ans+='R';
				for(int i=1;i<=cnt[3];i++) ans+='D';
				for(int i=1;i<=cnt[1];i++) ans+='U';
				for(int i=1;i<=cnt[2];i++) ans+='L';
			}
		}else if(my==0){
			if(mx<x){
				for(int i=1;i<=cnt[1];i++) ans+='U';
				for(int i=1;i<=cnt[0];i++) ans+='R';
				for(int i=1;i<=cnt[2];i++) ans+='L';
				for(int i=1;i<=cnt[3];i++) ans+='D';
			}else{
				for(int i=1;i<=cnt[1];i++) ans+='U';
				for(int i=1;i<=cnt[2];i++) ans+='L';
				for(int i=1;i<=cnt[0];i++) ans+='R';
				for(int i=1;i<=cnt[3];i++) ans+='D';
			}
		}else if(mx==x){
			for(int i=1;i<=cnt[1];i++) ans+='U';
			for(int i=1;i<=cnt[3];i++) ans+='D';
			for(int i=1;i<=cnt[0];i++) ans+='R';
			for(int i=1;i<=cnt[2];i++) ans+='L';
		}else if(my==y){
			for(int i=1;i<=cnt[0];i++) ans+='R';
			for(int i=1;i<=cnt[2];i++) ans+='L';
			for(int i=1;i<=cnt[1];i++) ans+='U';
			for(int i=1;i<=cnt[3];i++) ans+='D';
		}else{
			for(int i=1;i<=cnt[0];i++) ans+='R';
			for(int i=1;i<=cnt[2];i++) ans+='L';
			for(int i=1;i<=cnt[1];i++) ans+='U';
			for(int i=1;i<=cnt[3];i++) ans+='D';
		}
		if(check(ans)){
				cout<<ans<<endl;
		}else{
			cout<<"Impossible"<<endl;
		}
	}
} 

Fireworks
三分求极值

#include<bits/stdc++.h>
#define debug(a,b) printf("%s = %d\n",a,b);
typedef long long ll;
using namespace std;
long double fun(int k,int n,int m,long double p){
	return ((long double)k*n+m)/((long double)1.0-pow(1.0-p,k));
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		double p;
		cin>>n>>m>>p;
		p*=(1e-4);
		int l=1,r=0x3f3f3f3f;
		while(l<r){
			int mid1=l+(r-l)/3;
			int mid2=r-(r-l)/3;
			if(fun(mid1,n,m,p)<fun(mid2,n,m,p))r=mid2-1;
			else l=mid1+1;
		}
		printf("%.10Lf\n",fun(l,n,m,p));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值