Java算法技巧

P8755 [蓝桥杯 2021 省 AB2] 负载均衡

1.使用优先队列对于类进行排序,需要接入Comparable的接口
2.读入优化:

static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in = new StreamTokenizer(ins);
import java.io.*;
import java.util.*;

public class Main {
	static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in = new StreamTokenizer(ins);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	static int N=200005;
	static int n,m,v[]=new int[N],ti[]=new int[N];
	
	public static void main(String[] args) throws IOException
	{
		in.nextToken();n=(int)in.nval;
		in.nextToken();m=(int)in.nval;
		for(int i=1;i<=n;i++) 
		{
			in.nextToken();v[i]=(int)in.nval;
		}
		PriorityQueue<Node> q=new PriorityQueue<>();
		for(int i=1;i<=m;i++)
		{
			int a,b,c,d;
			in.nextToken();a=(int)in.nval;in.nextToken();b=(int)in.nval;
			in.nextToken();c=(int)in.nval;in.nextToken();d=(int)in.nval;
			while(!q.isEmpty()) {
				Node tmp=q.peek();
				//out.println(tmp.id+" "+tmp.up+" "+tmp.x);
				if(a>=tmp.up) {
					q.poll();
					v[tmp.id]+=tmp.x;
				}
				else 
					break;
			}
			if(d<=v[b]){
				ti[b]=a+c;
				v[b]=v[b]-d;
				out.println(v[b]);
				q.add(new Node(b,a+c,d));
			}
			else {
				out.println("-1");
			}
		}
		out.close();
	}
}
class Node implements Comparable<Node>
{
	int id,up,x;
	public Node(int a,int b,int c) {
		this.id=a;this.up=b;this.x=c;
	}
	@Override
	public int compareTo(Node e1) {
	    return Integer.compare(this.up, e1.up);
	}
}

P8744 [蓝桥杯 2021 省 A] 左孩子右兄弟

洛谷真的看不起蓝桥杯。。。
树形dp,考虑儿子的数量和所有儿子中最大深度

import java.util.*;
import java.io.*;

public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	
	static int N=100005;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
	static int n,dep,dp[]=new int[N];
	static boolean vis[]=new boolean[N];
	static List<Integer> e[]=new List[N];
	public static void dfs(int u)
	{
		int cnt=0;
		for(int v:e[u])
		{
			if(!vis[v])
			{
				dfs(v);
				cnt++;
				dp[u]=Math.max(dp[u],dp[v]);
			}
		}
		dp[u]+=cnt;
	}
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		for(int i=1;i<=n;i++)
			e[i]=new ArrayList<>();
		for(int i=2;i<=n;i++)
		{
			in.nextToken();int x=(int)in.nval;
			e[x].add(i);
		}
		dfs(1);
		out.print(dp[1]);
		out.close();
	}
}

P8599 [蓝桥杯 2013 省 B] 带分数

手写哥全排列。用两个指针分割排列,进行判断。

import java.util.*;
import java.io.*;


public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	
	static int N=100005;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
	static int n,ans,num=9,a[]=new int[15];
	static boolean vis[]=new boolean[15];
	public static void dfs(int x)
	{
		if(x==num) 
		{
			int k=0;
			for(int i=0;i<num-2;i++)
			{
				k=k*10+a[i];
				int g=0;
				for(int j=i+1;j<num-1;j++)
				{
					g=g*10+a[j];
					int p=0;
					for(int d=j+1;d<num;d++)
						p=p*10+a[d];
					if(g%p==0&&k+g/p==n)
						ans++;
				}
			}
			return;
		}
		for(int i=1;i<=num;i++)
		{
			if(!vis[i]) {
				a[x]=i;
				vis[i]=true;
				dfs(x+1);
				vis[i]=false;
			}
		}
	}
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		dfs(0);
		out.println(ans);
		out.close();
	}
}

P8687 [蓝桥杯 2019 省 A] 糖果

一个状压DP,先放一下,后面会再去学的。暴力敲了一篇,过部分样例

import java.util.*;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

import java.awt.geom.Area;
import java.io.*;
import java.text.DateFormatSymbols;


public class Main
{
	static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer in=new StreamTokenizer(br);
	static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
	
	static int N=100005;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
	static int n,m,k,ans=105,a[]=new int[105];
	static List<Integer> e[]=new List[105];
	static Set<Integer> set=new HashSet<>();
	static boolean vis[]=new boolean[105],p[]=new boolean[105];
	public static void dfs(int x)
	{
		if(x==n) {
			for(int i=1;i<=m;i++) p[i]=false;
			for(int i=1;i<=n;i++) 
			{
				int id=a[i-1];
				for(int j:e[id]) p[j]=true;
				int flag=0;
				for(int j=1;j<=m;j++)
				{
					if(!p[j]) {
						flag=1;break;
					}
				}
				if(flag==0) {
					ans=Math.min(ans, i);
				}
			}
			return;
		}
		for(int i=1;i<=n;i++)
		{
			if(!vis[i]) {
				a[x]=i;
				vis[i]=true;
				dfs(x+1);
				vis[i]=false;
			}
		}
	}
	public static void main(String[] args) throws IOException 
	{
		in.nextToken();n=(int)in.nval;
		in.nextToken();m=(int)in.nval;
		in.nextToken();k=(int)in.nval;
		for(int i=1;i<=n;i++) 
		{
			e[i]=new ArrayList<>();
			for(int j=1;j<=k;j++)
			{
				in.nextToken();int x=(int)in.nval;
				e[i].add(x);
			}
		}
		dfs(0);
		out.println(ans);
		out.close();
	}
}

你可能感兴趣的:(java,算法,蓝桥杯)