*[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整除的数字,那么就计算一下就能得到. ...
随机推荐
- SQL Server Profiler监控执行语句
SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...
- 利用VSCode进行.Net Core初尝试
1.下载.Net Core,https://www.microsoft.com/net/core#windows,按照页面提示进行SDK的安装. 2.下载VsCode,https://www.visu ...
- String与StringBuffer对象问题
下面的代码创建了三个String对象,其中pool中一个,heap中两个 String s1 = new String("abc"); String s2 = new String ...
- WPF中ListBox的项ListBoxItem被选中的时候Background变化
使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:
- 使用另一种方式实现js中Function的调用(call/apply/bind)
在JavaScript中函数的调用可以有多种方式,但更经典的莫过于call和apply.call跟apply都绑定在函数上,他们两个的第一个参数意义相同,传入一个对象,他作为函数的执行环境(实质上是为 ...
- SQL Server Management Studio Keyboard shortcuts
一些平时在SQL Server Management Studio 使用到的快捷键 F5 (Ctrl+x)执行选中部分的语句,没有选中则全文执行 Ctrl+L 现实执行计划(估计) Ctrl+M 在运 ...
- SOA Demo
使用SOA来实现两个数字的相加,不包含验证,仅供练习使用. PDF文档下载地址:http://files.cnblogs.com/chenyongblog/SOA_Demo.pdf 源码下载:http ...
- Java中的main()方法详解
在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...
- python之字串
python字串声明: 单引('), 双引("), 三引(''' 或 """"). python字串前缀: r表示原生字串, 字串内容: (1)不能包 ...
- 利用iOS API编写简单微博客户端全过程
要编写社交网络客户端程序,可以大体上分为4个主要的步骤 下面我们按照这个流程,介绍一下: 1.引入Accounts和Social框架 工 程中需要引入Accounts和Social框架,Account ...