牛牛偶像养成记(贪心)

 

 

为了拯救因入学人数骤降,面临废弃的学校,牛牛决定成为偶像啦。当然,作为一个偶像,肯定是要上台表演的。

已知牛牛拿到了n个上台表演的机会,第i次表演的上台时间为ti时刻,需要表演mi这么长的时间。

牛牛为了提高自己的知名度,肯定要取得最多的上场次数。请问,牛牛最多能上场多少次呢?

输入描述:

第一行输入一个数字n(1≤n≤100000),表示牛牛获得的上台表演的机会
接下来n行,每行输入两个数字ti(1≤ti≤108)和mi(1≤mi≤108), 表示第i次表演机会的上台时间和该次表演需要的总时间(表演途中不能中断表演退出)。

输出描述:

牛牛最多能上场的次数。

示例1

输入

3
1 2
3 2
5 2

输出

3

1)

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int n = sc.nextInt();
			Node[] node = new Node[n];
			for(int i = 0 ; i < n ; i++) {
				node[i] = new Node();
				node[i].start = sc.nextInt();
				node[i].end = node[i].start + sc.nextInt();
			}
			System.out.println(perform(node,n));
		}
		sc.close();
	}
	private static int perform(Node[] node,int n) {
		Arrays.sort(node, new Comparator<Node>() {
			@Override
			public int compare(Node o1, Node o2) {
				// TODO Auto-generated method stub
				return o1.end - o2.end;
			}
		});
		int result = 0, lastEnd = 0;
		for(int i = 0 ; i < n ; i++) {
			if(node[i].start >= lastEnd) {
				result++;
				lastEnd = node[i].end;
			}
		}
		return result;
	}
}
class Node{
	int start;
	int end;
}
import java.util.*;
public class Main implements Comparable<Main> {//See Main as Act
  int startTime, endTime;
  
  Main(int startTimeInit, int duration) {
    this.startTime = startTimeInit;
    this.endTime = startTimeInit + duration;
  }
  
  public int compareTo(Main other) {return this.endTime - other.endTime;}
  
  public static void main(String[] args) {
    PriorityQueue<Main> pq = new PriorityQueue<>();
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt(), cnt = 0, lastEndTime = 0;
  
    while (n-- != 0)
      pq.add(new Main(sc.nextInt(), sc.nextInt()));
    while (pq.size() != 0) {
      if (pq.peek().startTime >= lastEndTime) {
        cnt++;
        lastEndTime = pq.peek().endTime;
      }
      pq.poll();
    }
    System.out.println(cnt);
  }
}

看懂大佬的答案都要好久,菜是本源啊

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值