uvaoj 10131 Is Bigger Smarter? 最长上升子序列(LIS)

大象的体重和智商是不是成正比?现在要找到反例,给定一些大象的体重和智商,找出一些大象,这些大象的体重严格上升,智商严格下降,就是一个最长上升子序列的模型,要记录路径,所以只能使用n^2的算法.要先将大象按照体重智商升序排序,然后求一下智商的最长严格下降子序列就可以了.
代码如下:
/*************************************************************************
	> File Name: 10131.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2014年11月12日 星期三 18时21分11秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 1010

struct Node {
	int w, s, idx;

	int input(int c)
	{
		idx = c + 1;
		return scanf("%d%d", &w, &s);
	}
}node[N];

int dp[N], pre[N], ans[N];

//依次按体重
bool cmp(Node u, Node v)
{
	return u.w < v.w;
}

bool check(Node u, Node v)
{
	return u.s < v.s;
}

//最长上升子序列,要记录路径
int main(int argc, char *argv[])
{
	int c = 0;
	while (node[c].input(c) != EOF) {
		++c;
	}
	sort(node, node + c, cmp);
	dp[0] = 1;
	pre[0] = -1;
	for (int i = 1; i < c; ++i) {
		dp[i] = 1;
		pre[i] = -1;
		for (int j = 0; j < i; ++j) {
			if (check(node[i], node[j]) && dp[i] < dp[j] + 1) {
				dp[i] = dp[j] + 1;
				pre[i] = j;
			}
		}
	}
	int pos = 0;
	for (int i = 0; i < c; ++i) {
		if (dp[pos] < dp[i]) {
			pos = i;
		}
	}
	printf("%d\n", dp[pos]);
	int cnt = 0;
	int cur = pos;
	while (cur != -1) {
		ans[cnt++] = node[cur].idx;
		cur = pre[cur];
	}
	for (int i = cnt - 1; i >= 0; --i) {
		printf("%d\n", ans[i]);
	}

	return 0;
}

/*
Is Bigger Smarter?

The Problem
Some people think that the bigger an elephant is, the smarter it is.
To disprove this, you want to take the data on a collection of elephants
and put as large a subset of this data as possible into a sequence so
that the weights are increasing, but the IQ's are decreasing.

The input will consist of data for a bunch of elephants, one elephant
per line, terminated by the end-of-file. The data for a particular elephant
will consist of a pair of integers: the first representing its size in
kilograms and the second representing its IQ in hundredths of IQ points.
Both integers are between 1 and 10000. The data will contain information
for at most 1000 elephants. Two elephants may have the same weight, the same
IQ, or even the same weight and IQ.

Say that the numbers on the i-th data line are W[i] and S[i]. Your program
should output a sequence of lines of data; the first line should contain
a number n; the remaining n lines should each contain a single positive
integer (each one representing an elephant). If these n integers are
a[1], a[2],..., a[n] then it must be the case that
	W[a[1]] < W[a[2]] < ... < W[a[n]]
and
	S[a[1]] > S[a[2]] > ... > S[a[n]]
In order for the answer to be correct, n should be as large as
possible. All inequalities are strict: weights must be strictly increasing,
and IQs must be strictly decreasing. There may be many correct outputs
for a given input, your program only needs to find one.

Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output
4
4
5
9
7
*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在引用中的代码段中,给定了一个整数数组。这段代码通过遍历数组中的元素,找到每个元素的下一个更大的数,并将其存储在数组中。具体的实现是通过维护一个栈来存储数组元素的下标。当遍历到一个元素时,如果栈不为空且当前元素大于栈顶元素所代表的元素,则将栈顶元素的下标出栈,并将其对应的数组元素赋值为当前元素。这样可以找到每个元素的下一个更大的数。如果栈为空或者栈顶元素所代表的元素大于当前元素,则将当前元素的下标入栈。最后,对于找不到下一个更大数的元素,将其对应的数组元素赋值为-1。通过这个算法,我们得到了以下结果:。这就是给定数组中每个元素的下一个更大的数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [找出数组中下一个大数](https://blog.csdn.net/f81892461/article/details/8681986)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [phoenix upsert ERROR:MutationState size is bigger than maximum allowed number of bytes](https://blog.csdn.net/u012551524/article/details/81773851)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值