UVa 10131 - Is Bigger Smarter

/*UVa 10131 - Is Bigger Smarter?
 * 变形的LCS 先将大象按体重增长排序,若大象体重相同,按IQ降序排列,
 * 排序后问题转化为求IQ的一个最长递减子序列
 * d[i]表示以i为终点的重量上升速度下降的序列
 * */
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Elephant {
	int w;
	int s;
	int id;

	public Elephant(int w, int s, int id) {
		this.w = w;
		this.s = s;
		this.id = id;
	}
}

class EleComparator implements Comparator<Elephant> {

	@Override
	public int compare(Elephant o1, Elephant o2) {
		if (o1.w < o2.w)
			return -1;
		else if (o1.w > o2.w)
			return 1;
		else {
			if (o1.s > o2.s)
				return -1;
			else if (o1.s < o2.s)
				return 1;
			else
				return 0;
		}
	}
}

class BiggerSmarter {
	static final int MAXN = 1005;
	Elephant[] e = new Elephant[MAXN];
	int[] d = new int[MAXN];
	int[] fa = new int[MAXN];
	int index = 0;
	int count = 0;

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		BiggerSmarter m = new BiggerSmarter();
		int n = 0;
		while (scanner.hasNext()) {
			m.e[++n] = new Elephant(scanner.nextInt(), scanner.nextInt(), n);
		}
		Arrays.fill(m.fa, -1);
		Arrays.sort(m.e, 1, n + 1, new EleComparator());
		m.dp(n);
		System.out.println(m.count);
		m.print(m.index);
	}

	private void dp(int n) {
		for (int i = 1; i <= n; i++) {
			d[i] = 1;
			for (int j = 1; j < i; j++) {
				if (e[j].w < e[i].w && e[j].s > e[i].s) {
					if (d[i] < d[j] + 1) {
						d[i] = d[j] + 1;
						fa[i] = j;
					}
				}
				if (count < d[i]) {
					count = d[i];
					index = i;
				}
			}
		}
	}

	private void print(int x) {
		int y = fa[x];
		if (y == -1) {
			System.out.println(e[x].id);
			return;
		}
		print(y);
		System.out.println(e[x].id);
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值