*[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014
图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况;每个节点记录以该节点结束的最长路径,这样加入新的路径时去更新。注意路径是双向的~
#include <vector>
#include <algorithm>
using namespace std; struct Road {
int start;
int end;
int val;
}; bool cmp(const Road &a, const Road &b) {
return a.val < b.val;
} int solution(int N, vector<int> &A, vector<int> &B, vector<int> &C) {
int M = A.size();
vector<Road> roads(M);
for (int i = 0; i < M; i++) {
roads[i].start = A[i];
roads[i].end = B[i];
roads[i].val = C[i];
}
sort(roads.begin(), roads.end(), cmp);
vector<pair<int, int>> dp(N); // first: the longest length ends with this node; second: the last path val to this node;
int result = 0;
for (int i = 0; i < M; i++) {
int x2y_len = dp[roads[i].end].first;
int x2y_val = dp[roads[i].end].second;
if (roads[i].val > dp[roads[i].start].second &&
dp[roads[i].start].first + 1 > dp[roads[i].end].first) {
x2y_len = dp[roads[i].start].first + 1;
x2y_val = roads[i].val;
result = max(x2y_len, result);
}
// the other side
int y2x_len = dp[roads[i].start].first;
int y2x_val = dp[roads[i].start].second;
if (roads[i].val > dp[roads[i].end].second &&
dp[roads[i].end].first + 1 > dp[roads[i].start].first) {
y2x_len = dp[roads[i].end].first + 1;
y2x_val = roads[i].val;
result = max(y2x_len, result);
}
dp[roads[i].end].first = x2y_len;
dp[roads[i].end].second = x2y_val;
dp[roads[i].start].first = y2x_len;
dp[roads[i].start].second = y2x_val;
}
return result;
}
*[codility]AscendingPaths的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
- [codility]CountDiv
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...
随机推荐
- [react native] Error loading page
如上图显示的错误,解决方法如下: 在react native ios项目的info.plist文件中,新增一个属性. 在Info.plist中添加NSAppTransportSecurity类型Dic ...
- php对数组排序代码
php对数组排序,介绍了和php,有关的知识.技巧.经验,和一些php源码等. 对数组排序 usort() 函数使用用户自定义的函数对数组排序. */ function cmp($a, $b) //用 ...
- sql语句中like匹配的用法详解
在SQL结构化查询语言中,LIKE语句有着至关重要的作用. LIKE语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的作用 ...
- php 文件上传一例简单代码
1.程序文件 <?php //判断临时文件存放路径是否包含用户上传的文件 if(is_uploaded_file($_FILES["uploadfile"]["tm ...
- SQL Server 2012 BI 学习 第一天
了解数据源,数据源视图,多维数据集,维度 数据源:一个数据库或者其它数据链接,SSAS不支持使用模拟功能来处理 OLAP 对象.模拟信息选择“使用服务帐户” 数据源视图:DSV是元数据的单个统一视图, ...
- Python标准库之urllib,urllib2自定义Opener
urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能.要支持这些功能,必须使用build_opener()函数创建自定义Opener对象. 1. build_open ...
- 使用Sass优雅并高效的实现CSS中的垂直水平居中(附带Flex布局,CSS3+SASS完美版)
实现css水平垂直居中的方法有很多,在这里我简单的说下四种比较常用的方法: 1.使用CSS3中的Flex布局 对于flex,我们要了解的是它是一个display的属性,而且必须要给他的父元素设置fle ...
- MySQL主从修复
MySQL主从故障修复 测试库:192.168.1.2 主192.168.1.3 从 192.168.1.4 主 4又是2的从库192.168.1.5 从 有人修改了192.168.1.2和192.1 ...
- C#的winform拼数字游戏
C#的winform拼数字游戏 声明:阅读了别人的代码学习修改而来,增加了美观度和游戏乐趣.(作者出处忘了不好意思) 程序截图 关键代码 using System; using System.Coll ...
- sublime package
Sublime text 2/3 中 Package Control 的安装与使用方法 2014/05/23前端工具, 工具, 教程, 软件4条评论 Package Control 插件是一个方便 S ...