HDU 1160

来源:互联网 发布:商业数据 200年历史 编辑:程序博客网 时间:2024/05/28 23:22

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12520    Accepted Submission(s): 5496
Special Judge


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],..., 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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
 

Sample Output
44597
 

Source
Zhejiang University Training Contest 2001 

要求严格的递增序列和严格的递减序列

西乡一下,其实只要将一个序列排序,再对另一个序列进行处理即可

所以该题可以用dp做

另外,写代码姿势太渣,导致我出现了一个错误,花了好久才查出来

当我更新区间的时候,最初我写的是

dp[k]=max(dp[k],dp[j]+1);
pre[k]=j;

没认真看还真是发现不了

为了这个错还是值得写一次题解的


#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define F first
#define S second
#define PI acos(-1.0)
#define E  exp(1.0)
#define pq pr__int64f("#");
#define ppp pr__int64f(" ");
#define INF 0xFFFFFFF
#define MAX 3
#define len(a) (__int64)strlen(a)
#define mem0(a) (memset(a,0,sizeof(a)))
#define mem1(a) (memset(a,-1,sizeof(a)))
using namespace std;
__int64 gcd(__int64 a, __int64 b) {
return b ? gcd(b, a % b) : a;
}
__int64 lcm(__int64 a, __int64 b) {
return a / gcd(a, b) * b;
}
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
struct pp
{
int w,s,id;
};
pp p[1010];
bool cmp(pp a,pp b){return a.w<b.w;}
int dp[1010],pre[1010],flag[1010];
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n=0;
while(scanf("%d%d",&p[n].w,&p[n].s)!=EOF)
{
p[n].id=n+1;
n++;
}
for(int i=0;i<n;i++)
{
dp[i]=1;
pre[i]=i;
}
sort(p,p+n,cmp);
for(int k=1;k<n;k++)
{
for(int j=0;j<k;j++)
{
if(p[j].w<p[k].w&&p[j].s>p[k].s)
{
if(dp[j]+1>dp[k]){dp[k]=dp[j]+1;pre[k]=j;}         //!!!!!!!!!!!!!!!!!!!!!!!
}
}
}
int maxx=0,pos=0;
for(int i=0;i<n;i++)
{
if(maxx<dp[i])
{
pos=i;
maxx=dp[i];
}
}
int k=0;
printf("%d\n",maxx);
while(pre[pos]!=pos)
{
flag[k++]=pos;
pos=pre[pos];
}
flag[k]=pos;
for(int i=k;i>=0;i--)
{
printf("%d\n",p[flag[i]].id);
}
return 0;
}

0 0
原创粉丝点击