蓝桥杯官网练习题(平均)

问题描述

有一个长度为 n 的数组( n 是 10 的倍数),每个数 ai 都是区间 [0,9] 中的整数。小明发现数组里每种数出现的次数不太平均,而更改第 i 个数的代价为 bi,他想更改若干个数的值使得这 10 种数出现的次数相等(都等于 n/10),请问代价和最少为多少。

输入格式

输入的第一行包含一个正整数 n。

接下来 n 行,第 i 行包含两个整数 ai,bi ,用一个空格分隔。

输出格式

输出一行包含一个正整数表示答案。

样例输入

复制代码
10 
1 1 
1 2 
1 3 
2 4 
2 5 
2 6 
3 7 
3 8 
3 9 
4 10

样例输出

复制代码
27

样例说明

只更改第 1,2,4,5,7,8 个数,需要花费代价 1+2+4+5+7+8=27。

评测用例规模与约定

对于 20% 的评测用例, n≤1000;

对于所有评测用例, n≤10^5,0<bi≤2×10^5。

java 复制代码
import java.util.*;

public class Main {
  public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n=scan.nextInt();
		int n1=n;
		int a[]=new int[10];
		int b[][]=new int[10][n];
		int index0=0,index1=0,index2=0,index3=0,index4=0,
				index5=0,index6=0,index7=0,index8=0,index9=0;
		int p=n/10;
		int ans=0;
		while(n1>0) {
			n1--;
			int k=scan.nextInt();
			a[k]++;
			if(k==0) {
				b[k][index0]=scan.nextInt();
				index0++;
			}
			else if(k==1) {
				b[k][index1]=scan.nextInt();
				index1++;
			}
			else if(k==2) {
				b[k][index2]=scan.nextInt();
				index2++;
			}
			else if(k==3) {
				b[k][index3]=scan.nextInt();
				index3++;
			}
			else if(k==4) {
				b[k][index4]=scan.nextInt();
				index4++;
			}
			else if(k==5) {
				b[k][index5]=scan.nextInt();
				index5++;
			}
			else if(k==6) {
				b[k][index6]=scan.nextInt();
				index6++;
			}
			else if(k==7) {
				b[k][index7]=scan.nextInt();
				index7++;
			}
			else if(k==8) {
				b[k][index8]=scan.nextInt();
				index8++;
			}
			else if(k==9) {
				b[k][index9]=scan.nextInt();
				index9++;
			}
		}
		
		
		for(int i=0;i<=9;i++) {
			if(a[i]>p) {
				int qv=a[i]-p;
				Arrays.sort(b[i]);
				while(qv>0) {
					qv--;
					if(i==0) {
						ans+=b[i][n-index0];
						index0--;
					}
					else if(i==1) {
						ans+=b[i][n-index1];
						index1--;
					}
					else if(i==2) {
						ans+=b[i][n-index2];
						index2--;
					}
					else if(i==3) {
						ans+=b[i][n-index3];
						index3--;
					}
					else if(i==4) {
						ans+=b[i][n-index4];
						index4--;
					}
					else if(i==5) {
						ans+=b[i][n-index5];
						index5--;
					}
					else if(i==6) {
						ans+=b[i][n-index6];
						index6--;
					}
					else if(i==7) {
						ans+=b[i][n-index7];
						index7--;
					}
					else if(i==8) {
						ans+=b[i][n-index8];
						index8--;
					}
					else {
						ans+=b[i][n-index9];
						index9--;
					}
				}
			}
		}
		System.out.println(ans);
	}
}
相关推荐
咖啡Beans44 分钟前
使用OpenFeign实现微服务间通信
java·spring cloud
我不是混子1 小时前
说说单例模式
java
间彧3 小时前
SimpleDateFormat既然不推荐使用,为什么java 8+中不删除此类
java
间彧3 小时前
DateTimeFormatter相比SimpleDateFormat在性能上有何差异?
java
间彧3 小时前
为什么说SimpleDateFormat是经典的线程不安全类
java
MacroZheng3 小时前
横空出世!MyBatis-Plus 同款 ES ORM 框架,用起来够优雅!
java·后端·elasticsearch
CoovallyAIHub4 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
用户0332126663674 小时前
Java 查找并替换 Excel 中的数据:详细教程
java
间彧4 小时前
ThreadLocal实现原理与应用实践
java