HDU

来源:互联网 发布:淘宝dw全屏代码怎么用 编辑:程序博客网 时间:2024/06/07 19:19


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 m11, m22,..., mnnthen it must be the case that 

Wm[1m[1] < Wm[2m[2] < ... < Wm[nm[n

and 

Sm[1m[1] > Sm[2m[2] > ... > Sm[nm[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


说是给你一些老鼠的重量和速度,在每一行中。。问能够找到最长的体重成递增,速度成递减的一个mice集合。

因为毕竟不知道具体有几只老鼠,所以直接用while循环进行输入操作,用一个变量记录mice的数量,便于dp时for循环的操作。

首先是用sort 对所有的老鼠进行一个最优条件存在下的排序,尽量按体重从小到大,速度从大到小进行排序。

最后输出最长的结果,还要再输出这个集合中的的每一个老鼠的坐标,就是在dp过程中对当前节点的上一个结点进行一个保存操作,最后统一保存。

最重要的是,while中如果用scanf();进行输入会直接超时,要换成cin!!!


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <vector>#include <cmath>#include <algorithm>using namespace std;#define MAX_N 100005#define INF 0x3f3f3f3f#define Mem(a,x) memset(a,x,sizeof(a))#define ll long longstruct qxx{    int w;    int s;    int num;    int pre;}e[1005];int dp[1005],m[1005];bool cmp(qxx a,qxx b) {    if(a.w == b.w)        return a.s>b.s;    return a.w<b.w;}int main() {    int n = 0;    Mem(dp,0);    Mem(m,0);    while(cin>>e[n].w>>e[n].s) {        e[n].num = n+1;        e[n].pre = 0;        n++;    }    sort(e,e+n,cmp);    int ans = 0;    int cur = 0;   // cout<<n<<endl;    for(int i = 0; i<n; i++) {            dp[i] = 1;        for(int j = 0; j<i; j++) {            if(e[j].w < e[i].w && e[j].s > e[i].s) {                if(dp[i] < dp[j] + 1) {                    e[i].pre = j;                    dp[i] = dp[j]+1;                }            }        }        if(ans < dp[i]) {            cur = i;            ans = dp[i];        }    }    cout<<ans<<endl;    int y = 0;    while(cur) {        m[y++] = cur;        cur = e[cur].pre;    }    for(int i = ans-1; i>=0; i--) {        cout<<e[m[i]].num<<endl;    }    return 0;}




0 0
原创粉丝点击