java学习18天

洛谷题目p2015

#include<iostream>

using namespace std;
int son[105][105],f[105][105];
int n,m,w[105][105],cnt[105],vis[105];

void dfs(int k)
{
	vis[k]=1;
	for(int i=1;i<=cnt[k];i++)
	{
		int ny=son[k][i];
		if(vis[ny]==1)continue;
		vis[ny]=1;
		dfs(ny);
		for(int j=m;j>=1;j--) 
			for(int g=j-1;g>=0;g--)  
			{
				f[k][j]=max(f[k][j],f[ny][g]+f[k][j-g-1]+w[k][ny]);
			
			}
	}
	return;
}

int main()
{
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		w[x][y]=w[y][x]=z;
		son[x][++cnt[x]]=y;
		son[y][++cnt[y]]=x;
	}
	dfs(1);
	cout<<f[1][m]<<endl;
}

p2085

#include<bits/stdc++.h>

using namespace std;
priority_queue<int> q;


int main(){
	int n,m,a,b,c,ans[100005];
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&a,&b,&c);
		for(int j=1;j<=m;j++){
			int k;
			k=a*j*j+b*j+c;
			if(i==1) q.push(k);
			else{
				if(k<q.top()){
					q.push(k);
					q.pop();
				}
				else break;
      
			}
		}
	}
	for(int i=1;i<=m;i++){
		ans[i]=q.top();
		q.pop();
	}
	
	for(int i=m;i>=1;i--)
		printf("%d ",ans[i]);	
	return 0;
}

p2104

#include<bits/stdc++.h>
using namespace std;
stack<int> q;
char x[500005];
int main()
{
	stack<int> q;
    char x[500005];
	int a,b;
	char c;
	scanf("%d%d",&a,&b);
	getchar();
	scanf("%s",x+1);
	for(int i=1;i<=a;i++)
		q.push(int(x[i]-'0'));
	getchar();
	scanf("%s",x+1);
	for(int i=1;i<=b;i++)
	{
		c=x[i];
		if(c=='*') q.push(0);
		if(c=='/') q.pop();
		if(c=='+')
		{
			int e=0;
			while(q.top()==1)
				e++,q.pop();
			q.pop();q.push(1);
			for(int i=1;i<=e;i++)
				q.push(0);
		}
		if(c=='-')
		{
			int e=0;
			while(q.top()==0)
				e++,q.pop();
			q.pop();q.push(0);
			for(int i=1;i<=e;i++)
				q.push(1);
		}
	}
	string s;
	while(!q.empty()) s+=char(q.top()+'0'),q.pop();
	for(int i=s.size()-1;i>=0;i--)
		putchar(s[i]);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值