• Java实现蓝桥杯 算法训练 Professor Monotonic's Network


    试题 算法训练 Professor Monotonic’s Network

    资源限制
    时间限制:1.0s 内存限制:256.0MB
    问题描述
      无聊的教授最近在做一项关于比较网络的实验。一个比较网络由若干个含两个输入端和两个输出端的比较器组成。如下图,一个比较器将会比较它的两个输入端的值i1和i2,把它们防止在输出端o1和o2上使得o1<=o2。

    一个比较网络有n个输入端a1,a2,…,an和n个输出端b1,b2,…,bn。对于每个比较器,它的输入端要么直接连在比较网络的输入端上,要么连在另一个个比较器的输出端上。这样的关系组成的有向图是无环的。下图给出了一个拥有4个输入端、4个输出端、5个比较器的比较网络。
    在这里插入图片描述
      在比较网络运行时,值从输入端输入,然后比较器开始工作。当然,一个比较器仅当它的所有输入值已经被算出来后才能开始工作。假设一个比较器需要1单位时间来工作。那么,上面的例子中的比较网络需要3单位时间来计算输出值。图中的comp-1和comp-2是并行工作的,comp-3和comp-4也是,comp-5只有等comp-3和comp-4完成后才能工作。
      无聊的教授现在需要帮助确定那些比较网络是排序网络,并且它们需要多长时间来计算输出值。一个排序网络是一个比较网络,并且无论输入如何,它的输出都单调增。上面的例子也是一个比较网络。因为对于每一种可能的输入,输出值都存在关系b1<=b2<=b3<=b4。
    输入格式
      第一行两个整数n,k,分别表示输入值的个数和比较器的个数。
      它们满足1<=n<=12,0<=k<=150。
      之后从左到右依次给出每个比较器的信息
      之后每行两个整数x,y表示第i个比较器将ax,ay做了比较,把其中较小的数放回ax,较大的数放回ay。
    输出格式
      第一行输出YES或NO表示该比较网络是否是排序网络
      第二行输出一个整数表示该比较网络的运行时间。
    样例输入
    样例1:
    4 5
    1 2
    3 4
    1 3
    2 4
    2 3
    样例2:
    8 0
    样例3:
    3 3
    1 2
    2 3
    1 2
    样例输出
    样例1:
    YES
    3
    样例2:
    NO
    0
    样例3:
    YES
    3
    样例说明
      样例1即图片上所给的比较网络。

    package 第九次模拟;
    
    import java.util.Scanner;
    
    public class 排序 {
    	public static class Node{
    		int first;
    		int second;
    	}
    	static 	int n=0,m=0,x=0,y=0;
    	static 	int N=15;
    	static int []a = new int [N];
    	static 	int [] num = new int [N];
    	  	static 	int res=0;
    	static int cnt=0;
    	static 	Node [] T = new Node[155];
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		
    		while(m-->0){
    			  x= sc.nextInt();
    			  y = sc.nextInt();
    			T[++cnt]=new Node();
    			T[cnt].first=x;
    			T[cnt].second=y;
    			int t =Math.max(num[x], num[y])+1;
    			num[x]=t;
    			num[y]=t;
    			res=Math.max(res, t);
    		}
    		sc.close();
    		boolean yes=true;
    		for(int i=0;i<(1<<n);i++){
    			for(int j=0;j<n;j++){
    				a[j+1]=(i>>j&1);
    			}
    			for(int j=1;j<=cnt;j++){
    				x=T[j].first;
    				y=T[j].second;
    				int tt=Math.min(a[x],a[y]);
    				a[y]=Math.max(a[x],a[y]);
    				a[x]=tt;
    			}
    			for(int j=1;j<n;j++)
    				if(a[j]==1&&a[j+1]==0){
    					yes=false;break;
    				}
    			if(!yes)
    				break;
    		}
    		if(yes)
    			System.out.println("YES");
    		else
    			System.out.println("NO");
    			
    		System.out.println(res);
    	}
    
    }
    
    
    package 第九次模拟;
    
    import java.util.Scanner;
    
    public class 排序 {
    	//转自https://blog.csdn.net/a1439775520/article/details/104653580
    	public static class Node{
    		int first;
    		int second;
    	}
    	 
    	public static void main(String[] args) {
    	 	 int n=0,m=0,x=0,y=0;
    	     int N=15;
    	    
    	     int [] num = new int [N];
    	  	 int res=0;
    	     int cnt=0;
    	     int t=0;
    	     Node [] T = new Node[155];
    	     Scanner sc = new Scanner(System.in);
    		  n = sc.nextInt();
    		  m = sc.nextInt();
    		
    		while(m-->0){
    			  x= sc.nextInt();
    			  y = sc.nextInt();
    			  T[++cnt]=new Node();
    			  T[cnt].first=x;
    			  T[cnt].second=y;
    			   t = Math.max(num[x], num[y])+1;
    			  num[x]=t;
    			  num[y]=t;
    			  res= Math.max(res, t);
    		}
    		sc.close();
    		 int []a = new int [N];
    	     int tt=0;
    //		boolean yes=true;
    		for(int i=0;i<(1<<n);i++){
    			for(int j=0;j<n;j++){
    				a[j+1]=(i>>j&1);
    			}
    			for(int j=1;j<=cnt;j++){
    				x=T[j].first;
    				y=T[j].second;
    				  tt= Math.min(a[x],a[y]);
    				a[y]= Math.max(a[x],a[y]);
    				a[x]=tt;
    			}
    			for(int j=1;j<n;j++)
    				if(a[j]==1&&a[j+1]==0){
    					System.out.println("NO");
    					System.out.println(res);return;
    				}
    //			if(!yes)
    //				break;
    		}
    //		if(yes)
    			System.out.println("YES");
    //		else
    //			System.out.println("NO");
    			
    		System.out.println(res);
    	}
    //	public static int min(int a,int b){
    //		return a<b?a:b;
    //	}
    //	public static int max(int a,int b){
    //		return a<b?b:a;
    //	}
    
    }
    
    
  • 相关阅读:
    web页面与多页应用(布局示例普通文档流)
    web页面与多页应用(一)
    Flutter,webview里面实现上传和下载的功能
    Flutter项目删除了相关的dart文件之后运行flutter run或者 F5编译运行时会报这个错误.... were declared as an inputs, but did not exist. Check the definition of target:kernel_snapshot for errors
    vue项目中,点击输入框的时候,弹出的键盘挡住了输入框,需要把输入框展示在可见区域中,不被遮挡
    在IE浏览器上,min-hheight:unset/line-hight:unset不生效问题解决,把unset换成auto,问题只要时IE浏览器设置unset不生效
    本人修改了,需要把新的AndroidManifest.xml 覆盖原来的,AndroidManifest.xml 覆盖问题
    使用Filter来过滤掉需要排除的数组对象
    深拷贝和浅拷贝
    正则表达式
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075298.html
Copyright © 2020-2023  润新知