uva 10131 Is Bigger Smarter?(DAG最长路)
题目连接:10131 - Is Bigger Smarter?
题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输出最长值和方案, 方案不唯一的时候任意输出一种。
解题思路:DAG无定点的最长路问题, 记忆化搜索,并记录当前最有解的前驱。
#include <stdio.h>
#include <string.h>
const int N = 10005; struct State {
int w;
int s;
}tmp[N];
int n, dp[N], vis[N]; int find(int cur) {
if (dp[cur]) return dp[cur];
int a;
vis[cur] = cur;
State& now = tmp[cur];
for (int i = 0; i < n; i++) {
if (tmp[i].w < now.w && tmp[i].s > now.s) {
a = find(i);
if (dp[cur] <= a) {
dp[cur] = a;
vis[cur] = i;
}
}
}
return ++dp[cur];
} void solve() {
int Max = 0, id = 0, ans[N], cnt = 0;
for (int i = 0; i < n; i++) {
if (!dp[i]) find(i);
if (Max < dp[i]) {
Max = dp[i];
id = i;
}
} printf("%d\n", Max);
while (id != vis[id]) {
ans[cnt++] = id + 1;
id = vis[id];
}
ans[cnt++] = id + 1; for (int i = cnt - 1; i >= 0; i--)
printf("%d\n", ans[i]); } int main() {
// Init;
n = 0;
memset(tmp, 0, sizeof(tmp));
memset(vis, 0, sizeof(vis));
memset(dp, 0, sizeof(dp)); // Read;
while(scanf("%d%d", &tmp[n].w, &tmp[n].s) == 2)
n++; solve(); return 0;
}
uva 10131 Is Bigger Smarter?(DAG最长路)的更多相关文章
- uva 10051 Tower of Cubes(DAG最长路)
题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...
- UVA 10131 Is Bigger Smarter?(DP最长上升子序列)
Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...
- UVA 10131 - Is Bigger Smarter? (动态规划)
Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. ...
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- UVA 10131 Is Bigger Smarter?(DP)
Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to t ...
- UVa 10131: Is Bigger Smarter?
动态规划题.类似UVa103 Stacking Box,都是题目给一种判断嵌套的方法然后求最长序列.提前对数据排序可以节省一些时间开销. 我的解题代码如下: #include <iostream ...
- 简单Dp----最长公共子序列,DAG最长路,简单区间DP等
/* uva 111 * 题意: * 顺序有变化的最长公共子序列: * 模板: */ #include<iostream> #include<cstdio> #include& ...
- NYOJ16 矩形嵌套(DAG最长路)
矩形嵌套 紫书P262 这是有向无环图DAG(Directed Acyclic Graph)上的动态规划,是DAG最长路问题 [题目链接]NYOJ16-矩形嵌套 [题目类型]DAG上的dp & ...
随机推荐
- ADB logcat 过滤方法(抓取日志)
1. Log信息级别 Log.v- VERBOSE : 黑色 Log.d- DEBUG : 蓝色 Log.i- INFO : 绿色 Log.w- WARN : 橙色 Log.e- ERROR ...
- pthread_detach(pthread_self())
pthread_detach(pthread_self()) 将状态改为unjoinable状态,确保资源的释放.其实简单的说就是在线程函数头加上 pthread_detach(pthread_sel ...
- Qt核心剖析: moc
前面我们说过,Qt 不是使用的“标准的” C++ 语言,而是对其进行了一定程度的“扩展”.这里我们从Qt新增加的关键字就可以看出来:signals.slots 或者 emit.所以有人会觉得 Qt 的 ...
- 如何使用springmvc的@requestbody 返回json数据
先配置 XXX_ servletxml <!-- 整合jackson 返回一个json格式 --><bean class="org.springframework.web. ...
- 13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例
#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> # ...
- zoj 1730 / poj 1455 Crazy Tea Party
这阵子都没怎么写代码,由于开学,忙于各种琐碎的事情,现在静下来了开始跟着暑假的节奏刷题了. 这道题一开是没看清题目-在寝室刷题就是效率不高... 后来才知道,题目意思是,一个环形序列,1minute可 ...
- perl学习(10) 字符串处理函数和排序
1.1.index Perl 查找子串第一次在大字符串中出现的地方,返回第一个字符的位置. . . my $stuff = “Howordy world!”; my $where3 = index($ ...
- 高焕堂《android从程序员到架构师之路》 YY讲坛直面大师学习架构设计
<android从程序员到架构师之路>YY讲坛活动: sundy携手高焕堂老师全程YY答疑 与大师一起,分享android技术 时间:7月21日下午2:00 报名联系QQ:22243 ...
- 在C++工程中main函数之前跑代码的廉价方法(使用全局变量和全局函数)
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- JSP:JAVA Bean在JSP中的运用
目录结构,如图: index.jsp <%@ page language="java" import="java.util.*" pageEncoding ...