蓝桥杯国赛训练 day1

目录

k倍区间

舞狮

交换瓶子


k倍区间

取模后算组合数就行

复制代码
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        solve();
    }
    
    public static void solve() {
        int n = sc.nextInt();
        long k = sc.nextLong();
        
//        System.out.println(calC(2,5));
        
        long[] arr = new long[n + 1];
        long[] pre = new long[n + 1];
        
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextLong();
            pre[i] = pre[i - 1] + arr[i];
        }
        
        HashMap<Long,Long>hm=new HashMap<>();

        for(int i=1;i<=n;i++) {
          long preKey=pre[i]%k;
          hm.put(preKey, hm.getOrDefault(preKey, 0L)+1);
        }
        
        long cnt=0;
        
        if(hm.containsKey(0L)) cnt+=hm.get(0L);
        
        for(long l:hm.keySet()) {
          long preVal=hm.get(l);
          if(preVal>=2) {
            cnt+=calC(2,preVal);
          } 
        }
        
        System.out.println(cnt);
    }
    
    /**
           *   计算组合数
     * @param m 上标
     * @param n 下标
     * @return
     */
    public static long calC(long m, long n) {
        m = Math.min(m, n - m); 
        long result = 1;
        for (long i = 1; i <= m; i++) {
            result *= (n - m + i) ;
            result /= i;
        }
        return result;
    }
}

组合数模版

复制代码
    /**
     * 计算组合数
     * @param m 上标
     * @param n 下标
     * @return
     */
    public static long calC(long m, long n) {
        m = Math.min(m, n - m); 
        long result = 1;
        for (long i = 1; i <= m; i++) {
            result *= (n - m + i) ;
            result /= i;
        }
        return result;
    }

舞狮

暴力就完了

不然应该是一个dfs 找环

复制代码
import java.util.*;

// xixi♡西
public class Main {

    static Scanner sc = new Scanner(System.in);

    
    public static void solve() {
    	
    	int n=sc.nextInt();
    	long arr[]=new long[n];
    	
    	for(int i=0;i<n;i++) {
    		arr[i]=sc.nextLong();
    	}
    	
    	Arrays.sort(arr);
    	
    	ArrayList<ArrayList<Long>>list=new ArrayList<>();
    	
    	loop:for(long num:arr) {
    		
    		boolean isAdd =false;
    		
    		for(ArrayList<Long> forList:list) {
    			if(num>forList.size()) {
    				isAdd=true;
    				forList.add(num);
    				continue loop;
    			}
    		}
    		
    		if(isAdd==false) {
    			ArrayList<Long>newList=new ArrayList<>();
    			newList.add(num);
    			list.add(newList);
    		}
    		
    	}
    	
    	System.out.print(list.size());
    	
    }

	public static void main(String[] args){

        int t = 1;
//        t = sc.nextInt();
        while (t-- > 0) {
            solve();
        }
        
    }

}

交换瓶子

复制代码
import java.util.*;

public class Main {
    static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        solve();
    }
    
    public static void solve() {
    	 int n = sc.nextInt();
         int[] arr = new int[n + 1];
         for(int i = 1; i <= n; i ++){
        	 arr[i] = sc.nextInt();
         }

         int count = 0;
         for(int i = 1; i <= n; i ++){
        	 if(arr[i] != i){
        		 int temp = arr[i];
        		 arr[i] = arr[temp];
        		 arr[temp] = temp; 
        		 count++;
        		 i = 1;
        	 }
         }
         System.out.println(count);
    }
    
}
相关推荐
m0_7369191026 分钟前
C++代码风格检查工具
开发语言·c++·算法
Coder_Boy_36 分钟前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_9449347337 分钟前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy44 分钟前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
invicinble1 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟1 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖1 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472462 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
XH华2 小时前
备战蓝桥杯,第九章:结构体和类
学习·蓝桥杯
TechWJ2 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto