UVa 10131 Is Bigger Smarter? (DP&LIS)

10131 - Is Bigger Smarter?

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1072

Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ's are decreasing.

The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.

Say that the numbers on the i-th data line are W[i] and S[i]. Your program should output a sequence of lines of data; the first line should contain a numbern; the remaining n lines should each contain a single positive integer (each one representing an elephant). If thesen integers are a[1], a[2],..., a[n] then it must be the case that

   W[a[1]] < W[a[2]] < ... < W[a[n]]

and

   S[a[1]] > S[a[2]] > ... > S[a[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 IQs 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

怎么排序?可以按照first和second均从大到小排,first优先。

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

完整代码:

/*0.016s*/

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1001;  

struct node
{
    int wei, iq, id;
    bool operator < (const node& a) const
    {
        return wei > a.wei || wei == a.wei && iq > a.iq;
        ///iq也这么排是为了方便后面求dp时不用判断wei是否相等~
    }
} e[MAXN];
int dp[MAXN], path[MAXN];  

void print(int x)
{
    printf("%d\n", e[x].id);
    if (path[x]) print(path[x]);
    else printf("%d\n", e[0].id);
}  

int main()
{
    int n = 0, ans = 0, i, j;
    while (~scanf("%d%d", &e[n].wei, &e[n].iq))
        e[n].id = n + 1, dp[n++] = 1;
    sort(e, e + n);
    for (i = 1; i < n; ++i)
        for (j = 0; j < i; ++j)
            if (e[j].iq < e[i].iq && dp[j] + 1 > dp[i])
                path[i] = j, dp[i] = dp[j] + 1;
    for (i = 1; i < n; ++i)
        if (dp[ans] < dp[i])
            ans = i;
    printf("%d\n", dp[ans]);
    print(ans);
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索for
, data
, and
, The
should
10131 uva、en10131是什么材料、en10131、乐高10131、en10131标准,以便于您获取更多的相关知识。

时间: 2024-08-30 00:49:27

UVa 10131 Is Bigger Smarter? (DP&amp;LIS)的相关文章

UVA 10029 Edit Step Ladders(dp)

Problem C: Edit Step Ladders An edit step is a transformation from one word x to another word y such that x and y are words in the dictionary, and x can be transformed to y by adding, deleting, or changing one letter. So the transformation from dig t

UVa 147 Dollars:经典DP&amp;amp;硬币组合数&amp;amp;整数拆分

147 - Dollars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 和UVa 357一样. 注意所有数除以5再算. 完整代码: /*0.019s*/ #include<cstdio> #include<cstring> const int coin[11

UVa 10739 String to Palindrome (DP)

10739 - String to Palindrome Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1680 思路:对于每个区间[i, j]: 若str[i] == str[j],dp[i][j] = dp[i + 1][j - 1]; 若str[i]

UVa 10651 Pebble Solitaire:DP&amp;amp;bitset

10651 - Pebble Solitaire Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1592 化为二进制进行状态转移,详见代码. 完整代码: /*0.016s*/ #include<bits/stdc++.h> using names

UVa 10404 Bachet&#039;s Game (DP&amp;amp;博弈)

10404 - Bachet's Game Time limit: 6.666 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1345 博弈~ 规则1:一个状态是必败状态当且仅当它所有后继是必败状态 规则2:一个状态是必胜状态当且仅当它至少有一个后继是必败状态 边界:这一题0是必败状态 思路

UVa 10405 Longest Common Subsequence (DP&amp;amp;LCS)

10405 - Longest Common Subsequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 Sequence 1: Sequence 2: Given two sequences of characters, print

UVa 11703 sqrt log sin (DP)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2750 不用加eps了,floor才是神器! 完整代码: 01./*0.382s*/ 02. 03.#include<cstdio> 04.#include<cmath> 05.const int mod = 1000000; 06. 0

算法:uva 10859 Placing Lampposts (树形dp)

题目大意 给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮. 每盏灯将照亮以它为一个端点的所有边. 在灯的总数最小的前提下,被两盏灯同时被照亮的边数应该 尽量大. 思路 这是LRJ<训练指南>上的例题. 这题教会了我一个很有用的技巧:有 两个所求的值要优化,比如让a尽量小,b也尽量小 那么可以转化为让 M*a+b尽量小,其中M应该是 一个比"a的最大值和b的最小值之差"还要大的数 最终的答案为ans/M, ans%M 回到这题,要 求放的灯总数最小

算法:uva 1407 Caves (树形背包dp)

题意 一棵n个节点的树,树的边有正整数权,表示两个节点之间的距离.你的任务是回答这样的询问:从跟 节点出发,走不超过x单位的距离, 最多能经过多少节点?同一个节点经过多次, 只能算一个. 思路 这题同样是多天前看的, 在今天才想出解法的. 动态规划就是这么有意思 :) 遍历n个节点, 有两种情 况, 第一种是遍历完之后不回到出发点, 第二种是要回到出发点. 两种都可能会重复经过某些边, 但是显 然还是第二种遍历的花费会更大 在这一题中, 遍历之后不需要回到出发点. f(i, j, 0): 表示遍