蓝桥杯国赛训练 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);
    }
    
}
相关推荐
博一波4 分钟前
Redis 集群:连锁银行的 “多网点智能协作系统”
数据库·redis·缓存
晚安里7 分钟前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
HashData酷克数据10 分钟前
官宣:Apache Cloudberry (Incubating) 2.0.0 发布!
数据库·开源·apache·cloudberry
秋难降10 分钟前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
爱隐身的官人18 分钟前
新后端漏洞(上)- Aapache Tomcat AJP 文件包含漏洞(CVE-2020-1938)
java·tomcat·ajp
@CLoudbays_Martin1129 分钟前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php
TDengine (老段)40 分钟前
TDengine 时间函数 TODAY() 用户手册
大数据·数据库·物联网·oracle·时序数据库·tdengine·涛思数据
四谎真好看42 分钟前
Java 学习笔记(进阶篇2)
java·笔记·学习
码界奇点1 小时前
KingbaseES一体化架构与多层防护体系如何保障企业级数据库的持续稳定与弹性扩展
数据库·架构·可用性测试
上官浩仁1 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring