uva 10131 - Is Bigger Smarter?

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.

Input

The input will consist of data for a bunch of elephants, one elephant per line, terminated by the endof-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.

Output

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

 

题意:找出一段不需按照原定顺序的子列使其满足条件

排序一次然后再最小上升(或下降)子序列

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n=1,m,w[1005],s[1005],dp[1005],lat[1005];
struct B{
	int w,s,id;
}a[1005];
bool cmp(B i,B j)
{
	if(i.s == j.s)
	  return i.w < j.w ;
	return i.s > j.s ;
}
void ready()
{
	ios::sync_with_stdio(false),cin.tie(0);
	while(cin>>a[n].w >>a[n].s)
	{
		a[n].id=n;
		n++;
	}
	n--;
	sort(a+1,a+n+1,cmp);
 
}
void work_for_w()
{
	dp[1]=1; 
	for(int i=2;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		{
			if(a[i].w>a[j].w && dp[j]+1>dp[i])
			{
				dp[i]=dp[j]+1;
				lat[i]=j;
			}
		}
	}
	int k=0,ans=0;
	for(int i=1;i<=n;i++)
	  if(dp[i]>dp[k])
	    k=i;
	while(k)
	{
		dp[++ans]=a[k].id;
		k=lat[k];
	}
	cout<<ans<<endl;
	for(int i=ans;i>=1;i--)
	  cout<<dp[i]<<endl;
}
int main()
{ 
	ready();
	work_for_w();
	return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:46
点赞 评论 收藏
分享
秋招进行到现在终于能写总结了。完全没想到战线会拉这么长,过程会如此狼狈,不过更应该怪自己太菜了。好在所有的运气都用在了最后,也是有个去处。背景:双2本硕科班,无竞赛,本科一段研究所实习,硕士一段大厂暑期实习但无转正。技术栈是C++&nbsp;&amp;&nbsp;Golang,实习是客户端音视频(而且是鸿蒙端开发),简历两个C++项目一个Golang项目。主要投递岗位:后端,cpp软开,游戏服务端,测开,以及一些不拘泥于Java的岗位。从8月起总共投递123家公司,笔试数不清了,约面大约30家。offer/oc/意向:友塔游戏(第一个offer,面试体验很好,就是给钱好少南瑞继保(计算机科班点击就送(限男生),不...
乡土丁真真:佬很厉害,羡慕~虽然我还没有到校招的时候,也想讲一下自己的看法:我觉得不是CPP的问题,佬的背书双2,技术栈加了GO,有两段实习。投了123,面了30.拿到11个offer。这个数据已经很耀眼了。这不也是CPP带来的吗?当然也不止是CPP。至少来说在这个方向努力过的也会有好的结果和选择。同等学历和项目选java就会有更好的吗?我个人持疑问态度。当然CPP在方向选择上确实让人头大,但是我觉得能上岸,至于最后做什么方向,在我看来并不重要。至于CPP特殊,有岗位方向的随机性,java不是不挑方向,只是没得选而已。也希望自己以后校招的时候能offer满满
点赞 评论 收藏
分享
牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务