FatMouse's Speed
2016-03-04 22:24
274 查看
FatMouse's Speed
Problem Description
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],..., mthen it must be the case that
W[m[1]] < W[m[2]] < ... < W[m
]
and
S[m[1]] > S[m[2]] > ... > S[m
]
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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900Sample Output
44597Source
#include<cstdio>#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 1005
struct node
{
int w,s,num;
}n[MAXN];
int dp[MAXN],r[MAXN],res[MAXN];
bool cmp(node a,node b)
{
if(a.w!=b.w)
return a.w<b.w;
else return a.s>b.s;
}
int main()
{
int k=1;
while(scanf("%d%d",&n[k].w,&n[k].s)!=EOF)
{
n[k].num=k;
k++;
}
k--;
sort(n+1,n+k+1,cmp);
for(int i=1;i<=k;i++)
{
dp[i]=1;r[i]=0;
}
for(int i=1;i<=k;i++)
for(int j=1;j<i;j++)
{
if(n[i].w>n[j].w&&n[i].s<n[j].s&&dp[i]<dp[j]+1)
{dp[i]=dp[j]+1;
r[i]=j;
}
}
int max1=dp[1],en;
for(int i=1;i<=k;i++)
{
if(dp[i]>max1)
{max1=dp[i];
en=i;
}
}
int i=1;
printf("%d\n",max1);
while(en)
{
res[i++]=en;
en=r[en];
}
i--;
while(i)
{
printf("%d\n",n[res[i]].num);
i--;
}
}
相关文章推荐
- [leetcode 318]Maximum Product of Word Lengths--判断两个字符串是否有相同的字符
- 二柱子
- fork子进程之间传输信息的方法(包含子进程与子进程之间区分的问题的解决)
- github自己收藏的Android开源项目(不断更新)
- 今日BBC
- HDU Common Subsequence
- android 读写sd卡的权限设置
- C++内存管理
- shared_ptr and "<"
- html基础
- c++ primer 第五版的Sales_data类
- iOS block 小结
- CefSharp .net
- WEB前端面试重点
- 带有同步清0、同步置1的D触发器模块描述及其Testbench测试
- ubuntu不能登录图形用户界面,游客身份可登陆,命令行可登陆
- 【BZOJ2243】【SDOI2011】染色(树链剖分+线段树)
- java中ArrayList和class简介
- 【drp 1】使用易宝实现在线支付
- 【drp 1】使用易宝实现在线支付