FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17255 Accepted Submission(s): 7642
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.
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.
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
Source
- 非常朴素的n^2的dp
- 对于w和s先对w排序,在w升序的基础上对s降序
- 之后对于当前状态只要看之前的状态就好
- 详见代码
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + 10 ;
const int inf = 0x3f3f3f3f ;
const int npos = -1 ;
const int mod = 1e9 + 7 ;
const int mxx = 100 + 5 ;
const double eps = 1e-6 ;
const double PI = acos(-1.0) ;
struct node{
int w, s, id, lth, pre;
};
bool cmp(const node &l, const node &r){
if(l.w!=r.w)
return l.w<r.w;
else
return l.s>r.s;
}
node dp[maxn];
int a, c, i, n, b[maxn], ans, idx, tot;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
i=0;
while(~scanf("%d %d",&a,&c)){
i++;
dp[i].w=a;
dp[i].s=c;
dp[i].id=i;
dp[i].lth=1;
dp[i].pre=0;
}
n=i;
sort(dp+1,dp+1+n,cmp);
ans=0;
for(i=1;i<=n;i++)
for(int j=1;j<i;j++)
if(dp[j].w<dp[i].w && dp[j].s>dp[i].s && dp[j].lth+1>dp[i].lth){
dp[i].lth=dp[j].lth+1;
dp[i].pre=j;
if(dp[i].lth>ans){
ans=dp[i].lth;
idx=i;
}
}
printf("%d\n",ans);
tot=0;
while(idx){
b[tot++]=dp[idx].id;
idx=dp[idx].pre;
}
for(i=tot-1;i>=0;i--)
printf("%d\n",b[i]);
return 0;
}