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.
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.
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.
6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900
4 4 5 9 7
【题解】 老鼠,,越胖,跑的越慢,现在有不知道数量的老鼠,输入截止到文件尾。
给定老鼠的体重和速度,要你求一个体重严格递增而速度 严格递减的最长序列,输出这个序列序列在原序列中的位置,,意思是说体重要排序。。
这题有个坑点就是都是严格递增或递减的,这点很重要。
所以先把体重排好序,小到大,保证递增,然后在里面找速度严格递减的,注意判断条件中一定要有node[i].w>node[j].w (体重严格递增),,不然你选出来的可能是速度递减而体重相同的。
【AC代码】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int N=10005;
struct tree
{
int w;
int sp;
int index;
}node[10005];
bool cmp(const tree a,const tree b)
{
return a.w<b.w;
}
int main()
{
std::ios::sync_with_stdio(false);
int k=1;
int a,b;
int dp[10010],path[10005];
int ans=0;
int p=0;
while(cin>>a>>b)
{
node[k].w=a;
node[k].sp=b;
node[k].index=k;
k++;
}
sort(node+1,node+k,cmp);
for(int i=1;i<=k;i++)
path[i]=i;
for(int i=1;i<k;i++)
{
dp[i]=1;
for(int j=1;j<i;j++)
{
if(node[i].sp<node[j].sp)
{
if(node[i].w>node[j].w && dp[i]<dp[j]+1)// 体重的排序在前面已经有了 但是题目要求体重要严格递减 所以这里必须挖去这种情况
{
dp[i]=dp[j]+1;
path[i]=j;
if(dp[i]>ans)
{
ans=dp[i];
p=i;
}
}
}
}
}
printf("%d\n",ans);
stack<int>t;//因为路径是递归保存的,所以用栈来输出
while(p!=path[p]) {t.push(node[p].index),p=path[p];}
t.push(node[p].index);
while(!t.empty())
{
cout<<t.top()<<endl;
t.pop();
}
return 0;
}