HDU 1160 FatMouse‘s Speed【C++练习题】

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
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 a mouse). If these n integers are m[1], m[2],…, m[n] then it must be the case that

W[m[1]] < W[m[2]] < … < W[m[n]]

and

S[m[1]] > S[m[2]] > … > S[m[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 speeds 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计数会好很多
动态规划
找LIS一样的:要么接,要么新开
之后要记录序号啊,路径啊,用数组序号记录就好
开心啊,一遍AC,我感觉又多了些自信
因为判断了边界条件:什么也没有
要特判一下,不能输出两个0了

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 1005
using namespace std;
struct Mouse{
    
    
	int w,s,num,last,sum;//重量,速度,代号,上一个找到的老鼠,总共多少个 
}m[N];//存老鼠 
bool mycmp(Mouse a,Mouse b){
    
    
	return a.w<b.w;
}
void print(int now){
    
    
	if(m[now].last){
    
    
		print(m[now].last);
	}
	if(now)printf("%d\n",m[now].num);//特判0
}
int main()
{
    
    
	//FILE*fp=fopen("text.in","r");//------记得最后注释掉!! 
	mem(m,0);
	//输入
	int cnt=0;
	int tw,ts;
	while(~scanf("%d%d",&tw,&ts)){
    
    
	//while(~fscanf(fp,"%d%d",&tw,&ts)){
    
    
		cnt++;
		m[cnt].num=cnt;
		m[cnt].w=tw;
		m[cnt].s=ts;
	}
	
	//排重量,递增(同重量最好速度最快,或者速度递增方便) 
	sort(m+1,m+cnt+1,mycmp);
	
	//找最长递减速度
	//m[i].sum=max(m[j(i前,i.s小于j.s)].sum)+1; m[i].last=j; 
	m[1].sum=1;
	for(int i=2;i<=cnt;i++){
    
    
		//找m[mj] 
		int mj=0;
		for(int j=1;j<i;j++){
    
    
			if(m[i].s<m[j].s){
    
    
				if(m[mj].sum<m[j].sum){
    
    
					mj=j;
				}
			}
		}
		//接上(接0表新开) 
		m[i].sum=m[mj].sum+1;
		m[i].last=mj;
	}
	
	//找到max的cnt输出,输出序号,递归 m[j].num
	int mi=0;
	for(int i=1;i<=cnt;i++){
    
    
		if(m[mi].sum<m[i].sum){
    
    
			mi=i;
		}
	}
	printf("%d\n",m[mi].sum);
	print(mi); 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51945248/article/details/113955186