<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Algorithms on PwzXxm</title><link>https://pwzxxm.com/topics/algorithms/</link><description>Recent content in Algorithms on PwzXxm</description><generator>Hugo</generator><language>en</language><copyright>© Patrick Weizhi Xu</copyright><lastBuildDate>Tue, 29 May 2018 00:00:00 +0000</lastBuildDate><atom:link href="https://pwzxxm.com/topics/algorithms/index.xml" rel="self" type="application/rss+xml"/><item><title>Static Linked List - Another Way To Represent Graphs</title><link>https://pwzxxm.com/static-linked-list/</link><pubDate>Tue, 29 May 2018 00:00:00 +0000</pubDate><guid>https://pwzxxm.com/static-linked-list/</guid><description>&lt;p&gt;Static Linked List is a data structure that stores linked list in static arrays. It is usually used to represent graphs. It is very interesting that its Chinese name literally translated as &amp;ldquo;Linked Forward Star&amp;rdquo;. You have two choices of paths to understand this.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Start from &lt;a href="https://pwzxxm.com/static-linked-list/#forward-star"&gt;Forward Star&lt;/a&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Start from &lt;a href="https://pwzxxm.com/static-linked-list/#adjacency-list"&gt;Adjacency List&lt;/a&gt;.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;However, I would recommend to explore both ideas to have a better understanding. If you know some of it or you just don&amp;rsquo;t care, you can jump to &lt;a href="https://pwzxxm.com/static-linked-list/#static-linked-list"&gt;here&lt;/a&gt; straight away.&lt;/p&gt;</description></item><item><title>POJ 3268 Silver Cow Party</title><link>https://pwzxxm.com/poj-3268/</link><pubDate>Sat, 14 May 2016 00:00:00 +0000</pubDate><guid>https://pwzxxm.com/poj-3268/</guid><description>&lt;p&gt;There are one cow from each &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;N&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; (&lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mn&gt;1000&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1 \le N \le 1000&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;) farms want to go to the number X farm to have a party, hurrah! One cow from each farm need to go to the party and go back. There are &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;M&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; (&lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mn&gt;100,000&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1 \le M \le 100{,}000&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;) weighted (represents time) one-direction roads connects pairs of roads. Cows are smart though, they want to go via the shortest path. The question is, what is the longest time the cow will take.&lt;/p&gt;</description></item><item><title>POJ 2376 Cleaning Shifts</title><link>https://pwzxxm.com/poj-2376/</link><pubDate>Wed, 16 Mar 2016 00:00:00 +0000</pubDate><guid>https://pwzxxm.com/poj-2376/</guid><description>&lt;p&gt;&lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;N&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; (&lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mn&gt;25,000&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1 \le N \le 25{,}000&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;) cows, each cow only available for a interval of time.&#10;&lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;T&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; (&lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mn&gt;1,000,000&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1 \le T \le 1{,}000{,}000&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;) shifts.&#10;Find minimum number of cows to complete &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;T&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; shifts.&lt;/p&gt;&#10;&lt;p&gt;Link: &lt;a href="http://poj.org/problem?id=2376"&gt;http://poj.org/problem?id=2376&lt;/a&gt;&lt;/p&gt;&#10;&lt;h3 id="notes"&gt;Notes&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Each shift must has at least one cow assigned to it.&lt;/li&gt;&#10;&lt;li&gt;A cow finishes after the end time. That is, if there are two cows, start and end at &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;(1, 3)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; and &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;(4, 10)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt; respectively. It is considered as an continuous internal and accepted case for &lt;span class="katex"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;T = 10&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;.&lt;/li&gt;&#10;&lt;li&gt;There can not have any gap between these cows&amp;rsquo;s working intervals.&lt;/li&gt;&#10;&lt;li&gt;Use &lt;code&gt;scanf()&lt;/code&gt; for input otherwise it will run out of the time limit.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;h3 id="solution"&gt;Solution&lt;/h3&gt;&#10;&lt;p&gt;Sort cows by ending time. Using greedy that for each interval, find the cow which has latest end time and before the current end time. As the vector is sorted, the first cow we find is the answer.&lt;/p&gt;</description></item><item><title>Binary Tree</title><link>https://pwzxxm.com/binary-tree/</link><pubDate>Fri, 15 Jan 2016 00:00:00 +0000</pubDate><guid>https://pwzxxm.com/binary-tree/</guid><description>&lt;p&gt;Binary tree is called so because of its shape. It&amp;rsquo;s like a tree, it have leaves and a root. In computer science, the &amp;ldquo;tree&amp;rdquo; is usually upside down, the root at the top and leaves grow below it. It is binary so it every node only can have 0, 1 or 2 leaves.&lt;/p&gt;&#10;&lt;br&gt;&#10;&lt;h2 id="terminologies"&gt;Terminologies&lt;/h2&gt;&#10;&lt;h3 id="leaf-node"&gt;Leaf Node&lt;/h3&gt;&#10;&lt;p&gt;The node do NOT have any child nodes.&lt;/p&gt;&#10;&lt;h3 id="inner-node"&gt;Inner Node&lt;/h3&gt;&#10;&lt;p&gt;The Node between the leaf node and the root.&lt;/p&gt;</description></item><item><title>Stack, Queue and Linked Lists</title><link>https://pwzxxm.com/stack-queue-linked-list/</link><pubDate>Fri, 25 Dec 2015 00:00:00 +0000</pubDate><guid>https://pwzxxm.com/stack-queue-linked-list/</guid><description>&lt;p&gt;Stack, queue and Linked lists are basic data structures. They appear in our daily life. For example, stacks of intermodal containers at the port, waiting in line(queue) to ride a roller coaster and the film The Human Centipede，if you know it.&lt;/p&gt;&#10;&lt;h2 id="stack"&gt;Stack&lt;/h2&gt;&#10;&lt;h3 id="intro"&gt;Intro&lt;/h3&gt;&#10;&lt;p&gt;Stack is like a stack of intermodal containers, or a stack of book. Every time you want to put a new item, you put above the original stack, not between or below. Every time you want to remove a item, you need to start from the top, remove the item which is latest added. Hence, there is a principle called &amp;ldquo;&lt;strong&gt;LIFO&lt;/strong&gt;&amp;rdquo;, Last In, First Out.&lt;/p&gt;</description></item></channel></rss>