原创 PAT 甲級 1062 Talent and Virtue 排序

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and

原创 PAT 1089 Insert or Merge

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted outp

原创 PAT 1048 Find Coins 散列/雙指針

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a un

原创 PAT 1010 Radix 二分

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 i

原创 PAT 1016 Phone Bills 排序

A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a cer

原创 PAT 甲級 1025 PAT Ranking 排序

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. E

原创 POJ 2182 Lost Cows 線段樹

Lost Cows Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor j

原创 樹狀數組

樹狀數組(Binary Indexed Tree(B.I.T), Fenwick Tree)是一個查詢和修改複雜度都爲log(n)的數據結構,空間複雜度則爲O(n),通過將線性結構轉化成樹狀結構,從而進行跳躍式掃描。主要用於查詢任意兩位之

原创 線段樹

線段樹用於對數據的更新和查詢,主要優勢體現在對段的處理上。如將數組a[]從a[i]-a[j]的元素均加上b,要做j-i+1次,而有一個段表示a[i]-a[j]的話,就直接將這個段的和sum加上b*(j-i+1)即可。同理,查詢段的和也只需

原创 POJ 2528 Mayor's posters 線段樹+離散化

Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election c

原创 POJ 2352 stars 樹狀數組

StarsDescriptionAstronomers often examine star maps where stars are represented by points on a plane and each star has

原创 POJ 2479 Maximum sum 動態規劃 最大子段和 最大子段積

  Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calcu

原创 POJ 1159 Palindrome 迴文子序列

1. http://poj.org/problem?id=1159 題意:給定一個字符串,求最少插入多少字符使該字符串變成迴文字符串。 分析: 方法1:先求該字符串與其逆序列的最長公共子序列LCS(該部分已經迴文),則原字符串長度減去LC

原创 POJ1182食物鏈 並查集

Description 動物王國中有三類動物A,B,C,這三類動物的食物鏈構成了有趣的環形。A吃B, B吃C,C吃A。  現有N個動物,以1-N編號。每個動物都是A,B,C中的一種,但是我們並不知道它到底是哪一種。  有人用兩種說法對這N

原创 POJ1458 Common Subsequence 最長公共子序列 公共子串

  Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Give