本文主要是介绍ACM DP FatMouse's Speed,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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.
OutputYour 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 1900Sample Output
4 4 5 9 7
题意:
给出每个老鼠的体重跟速度,对大限度的证明老鼠体重越大,速度越慢;
分析:
第一个难点就是DP本身啦,很容易想到上升子序列,还是先排序优化;
第二个难点就是记录路径了,还好之前做最短路径做过没用结构体记录:
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct MOU
{
int x;
int w;
int v;
}mou[1111];
struct node
{
int don;//在序列中的编号
int per;//该序列上一个点
}dp[1111];
bool cmp(const MOU &a,const MOU &b)
{
if(a.w!=b.w) return a.w<b.w ;
else if(a.v!=b.v) return a.v>b.v;
else return 0;
}
int main()
{
int i,j,n=0,res,t,m[1111];
while(scanf("%d %d",&i,&j)!=EOF)
{
mou[n].x=n+1;//注意要+1;
mou[n].w=i;
mou[n].v=j;
n++;
}
sort(mou,mou+n,cmp);
res=0;
for(i=0;i<n;i++)
{
dp[i].don=1;//自己一定以自己为序列的第一个
dp[i].per=0;//自己为序列前一个就是无(0)
}
t=1;
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(mou[j].v>mou[i].v&&mou[j].w<mou[i].w)
{
if(dp[i].don<dp[j].don+1)//如果i在之前的序列中的位置没有进入到j这个序列中后的位置靠后,那就说明j这个//序列更长,所以加入
{
dp[i].don=dp[j].don+1;
dp[i].per=j;
}
}
}
if(dp[i].don>res)
{
res=dp[i].don;
t=i;
}
}
printf("%d\n",res);
for(i=1;i<=res;i++)
{
m[i]=t;//用数组倒着记录下每个点前面的点的序列
t=dp[t].per;
}
for(i=res;i>=1;i--)
printf("%d\n",mou[m[i]].x);
return 0;
}
这篇关于ACM DP FatMouse's Speed的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!