结构体排序-IO优化-贪心-最优值

最优值

题目

已知有NN个单词,其排列为P,编号从1到N。

每个单词都有一个价值Value,该价值与单词的首字母ch、单词的长度LL,以及该单词在排列中的编号ID有关。

Value = |ch| * L * ID;Value=∣ch∣∗L∗ID;  

|ch|∣ch∣ 表示字母chch对应的数字 a -> 1, b -> 2, ……, z -> 26a−>1,b−>2,……,z−>26

现在,wlxsq想知道,该如何排列这NN个单词,使得其(NN个单词)总价值和最大。请输出最大价值之和。

思路

不用说,贪心。主要是输入这里java必须要用io优化,不然会超时,肯定是测试点的字符串太长了,奈何我不会
拿pq写有点脱裤子放屁,但写起来顺手哈哈

代码

import java.util.*;

public class 最优值 {
	static class word implements Comparable<word> {
		String str;
		int weight;

		public word(String str) {
			this.str = str;
			this.weight = (str.charAt(0) - 'a' + 1) * str.length();
		}

		@Override
		public int compareTo(word o) {
			return this.weight - o.weight;
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		PriorityQueue<word> pq = new PriorityQueue<word>();
		for (int i = 0; i < n; i++) {
			pq.offer(new word(sc.nextLine()));
		}
		int count = 0;

		for (int i = 1; i <= n; i++) {
			count+=(pq.poll().weight*i);
		}
		System.out.println(count);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值