L2-009 抢红包 - java

L2-009 抢红包


时间限制
300 ms
内存限制
64 MB


题目描述:

没有人没抢过红包吧…… 这里给出N个人之间互相发红包、抢红包的记录,请你统计一下他们抢红包的收获。

输入格式:
输入第一行给出一个正整数N(≤ 1 0 4 10^{4} 104),即参与发红包和抢红包的总人数,则这些人从1到N编号。随后N行,第i行给出编号为i的人发红包的记录,格式如下:
K K K N 1 N_{1} N1 P 1 P_{1} P1 N K N_{K} NK P K P_{K} PK

其中K(0≤K≤20)是发出去的红包个数, N i N_{i} Ni 是抢到红包的人的编号, P i P_{i} Pi(>0)是其抢到的红包金额(以分为单位)。注意:对于同一个人发出的红包,每人最多只能抢1次,不能重复抢。

输出格式:
按照收入金额从高到低的递减顺序输出每个人的编号和收入金额(以元为单位,输出小数点后2位)。每个人的信息占一行,两数字间有1个空格。如果收入金额有并列,则按抢到红包的个数递减输出;如果还有并列,则按个人编号递增输出。

输入样例:
10
3 2 22 10 58 8 125
5 1 345 3 211 5 233 7 13 8 101
1 7 8800
2 1 1000 2 1000
2 4 250 10 320
6 5 11 9 22 8 33 7 44 10 55 4 2
1 3 8800
2 1 23 2 123
1 8 250
4 2 121 4 516 7 112 9 10
输出样例:
1 11.63
2 3.63
8 3.63
3 2.11
7 1.69
6 -1.67
9 -2.18
10 -3.26
5 -3.26
4 -12.32


给定n个人发出的红包给谁抢走了
求出n个人按照收入金额从高到低的递减顺序输出每个人的编号和收入金额

如果收入金额有并列,则按抢到红包的个数递减输出;
如果还有并列,则按个人编号递增输出。


emmmmmmm

每次将每个人的抢到的红包加上 当然了 被抢的人要减去红包
还有每次每个人抢到的红包个数也要加上

最后对象排序一下即可

注: java 最后一个 数据有亿点多 T 了


import java.io.*;
import java.math.*;
import java.util.*;

public class Main
{
	static class edge implements Comparable<edge>
	{
		int sum, cnt, pos;

		public edge(int sum, int cnt, int pos)
		{
			this.sum = sum;
			this.cnt = cnt;
			this.pos = pos;
		}

		@Override
		public int compareTo(edge other)
		{
			if (this.sum != other.sum)
				return other.sum - this.sum;
			if (this.cnt != other.cnt)
				return other.cnt - this.cnt;
			return this.pos - other.pos;
		}

	}

	public static void main(String[] args)
	{
		int n = sc.nextInt();
		edge shu[] = new edge[n + 10];
		for (int i = 1; i <= n; i++)
			shu[i] = new edge(0, 0, i);
		for (int i = 1; i <= n; i++)
		{
			int k = sc.nextInt();
			while (k-- > 0)
			{
				int N = sc.nextInt();
				int P = sc.nextInt();
				shu[i].sum -= P;
				shu[N].sum += P;
				shu[N].cnt++;
			}
		}
		Arrays.sort(shu, 1, n + 1);

		for (int i = 1; i <= n; i++)
			out.printf("%d %.2f\n", shu[i].pos, shu[i].sum / 100.0);

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
}

c++

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e4;

struct edge
{
    int sum, cnt, pos;
    
    bool operator <(const edge &other) const
    {
        if(sum != other.sum)
            return sum > other.sum;
        if(cnt != other.cnt)
            return cnt > other.cnt;
        return pos < other.pos;
    }
} shu[N + 10];

int n;
int k;
int m, p;

int main()
{
    scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
	    shu[i].pos = i;
	    scanf("%d", &k);
		while (k-- > 0)
		{
		    scanf("%d%d", &m, &p);
			shu[i].sum -= p;
			shu[m].sum += p;
			shu[m].cnt ++;
		}
	}
	sort(shu + 1, shu + n + 1);

	for (int i = 1; i <= n; i++)
		printf("%d %.2f\n", shu[i].pos, shu[i].sum / 100.0);
    
    return 0;
}

对象数组排序

对象排序

对象排序


如果有说错的 或者 不懂的 尽管提 嘻嘻

一起进步!!!


闪现

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
根据提供的代码和输入输出样例,这段代码是一个解决L2-009 抢红包问题的程序。该程序接受一个整数n作为输入,表示参与抢红包的人数。然后依次输入每个人的编号和抢到的红包金额。程序会根据收入金额从高到低的递减顺序输出每个人的编号和收入金额(以元为单位,输出小数点后2位)。如果收入金额有并列,则按抢到红包的个数递减输出;如果还有并列,则按个人编号递增输出。 对于给定的输入样例10 3 2 22 10 58 8 125 5 1 345 3 211 5 233 7 13 8 101 1 7 8800 2 1 1000 2 1000 2 4 250 10 320 6 5 11 9 22 8 33 7 44 10 55 4 2 1 3 8800 2 1 23 2 123 1 8 250 4 2 121 4 516 7 112 9 10,程序的输出样例应为1 0.00 2 0.00 3 0.00 4 0.00 5 0.00 6 0.00 7 0.00 8 0.00 9 0.00 10 0.00。 请注意,这段代码使用了C++的标准库函数和算法,包括iostream、algorithm和using namespace std。它还定义了一个Person类,用于存储每个人的编号、收入金额和抢到红包的个数。程序通过对Person对象数组进行排序,按照要求输出每个人的编号和收入金额。 希望这个解释对你有帮助! #### 引用[.reference_title] - *1* *2* *3* [【GPLT 二阶题目集】L2-009 抢红包](https://blog.csdn.net/DeskOneRice/article/details/128783061)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢谢 啊sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值