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的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  7. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  8. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  9. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

随机推荐

  1. phpStudy for Linux (lnmp+lamp一键安装包)

    phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 phpStudy for Linux 支持Apache/Ng ...

  2. Azure IaaS for IT Pros Online Event 总结

    微软一个为期4天的一个有关于Azure的介绍,主要总结了些Azure现有的技术以及将会推出东西 主题链接 http://channel9.msdn.com/Events/Microsoft-Azure ...

  3. 【转载】Powershell在世纪互联Office365中批量将用户添加到组

    $NewUserPath = ".\Office365AddUserToGroup.csv" $NewUsers = import-csv $NewUserPath foreach ...

  4. Python支持中文注释

    三处设置,使Python的Eclipse开发环境(使用PyDev)支持中文 - (a)Eclipse的Window菜单Editors设置: Eclipse工具条 -> Window -> ...

  5. .NET基础:C#静态构造函数、静态方法、静态属性

    用一个题目带大家走进静态函数,先看题目 class Program    {        public static int Count = 0;        static Program()   ...

  6. ASP.NET Web – 输入的有效性验证

    下面这个示例显示了与文本框textFirstname相关的验证控件RequiredFieldValidator.所有的验证控件都有ErrorMessage和ControlToValidate属性.如果 ...

  7. 关于cookie的一点知识

    关于cookie的一点知识 1.cookie是存储在客户端计算机中. 2.cookie不能跨浏览器访问.cookie是浏览器保存的,所以不同浏览器对cookie的保存路径.存储数据的格式.文件大小都可 ...

  8. asp.net中XmlDocument解析出现出错,处理特殊字符

    xml结构会解析一些特殊字符,特别是& <  所以我们需要把结构放在CDATA中处理,CDATA里面的内容在XmlDocument 解析时会自动忽略掉,不会解析里面的内容:因此,我这里就 ...

  9. java集合类(六)About Queue

    接上篇“java集合类(五)About Map” 终于来到了java集合类的尾声,太兴奋了,不是因为可以休息一阵了,而是因为又到了开启新知识的时刻,大家一起加油打气!!Come on...Fighti ...

  10. ffmpeg 音频转码

    大多数厂家摄像机输出的音频流格式都是PCM,有一些场合(比如讲音视频流保存成Ts流)需要将PCM格式转成AAC格式.基本的思路是先解码得到音频帧,再将音频帧编码成AAC格式.编码和解码之间需要添加一个 ...