蓝桥杯国赛训练 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_736927044 分钟前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
java·数据库·sql·postgresql
在云上(oncloudai)4 分钟前
Amazon ElastiCache 全解析:打造高性能的智能缓存架构
缓存·架构
lang201509285 分钟前
MySQL 8.0.29 及以上版本中 SSL/TLS 会话复用(Session Reuse)
数据库·mysql
Jabes.yang7 分钟前
Java面试大作战:从缓存技术到音视频场景的探讨
java·spring boot·redis·缓存·kafka·spring security·oauth2
hbqjzx8 分钟前
记录一个自动学习的脚本开发过程
开发语言·javascript·学习
Query*21 分钟前
Java 设计模式——适配器模式进阶:原理深挖、框架应用与实战扩展
java·设计模式·适配器模式
Sirens.30 分钟前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github
Meteors.31 分钟前
23种设计模式——中介者模式 (Mediator Pattern)详解
java·设计模式·中介者模式
望获linux33 分钟前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
焰火199940 分钟前
[Java]基于Spring的轻量级定时任务动态管理框架
java·后端