原创 PAT A 1029 Median 雙指針/二分

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median

原创 歐幾里得算法及擴展

輾轉相除法 求整數a,b的最大公約數gcd(a,b): 定理:gcd(a,b)=gcd(b,a%b) 證明:設a除以b得到的商和餘數分別爲k和r,則a=kb+r,r=a%b,設d爲a,b公約數,則d一定整除r=a-kb,所以d是(b,a%

原创 藍橋杯 壘骰子 矩陣快速冪

賭聖atm晚年迷戀上了壘骰子,就是把骰子一個壘在另一個上邊,不能歪歪扭扭,要壘成方柱體。 經過長期觀察,atm 發現了穩定骰子的奧祕:有些數字的面貼着會互相排斥! 我們先來規範一下骰子:1 的對面是 4,2 的對面是 5,3 的對面是 6

原创 PAT 1033 To Fill or Not to Fill 貪心

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is

原创 POJ 1463 Strategic game 樹形DP

Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fa

原创 POJ1258 Agri-Net 最小生成樹Prim

Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connecti

原创 PAT 1084 Broken Keyboard 散列

On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to t

原创 POJ1125 Stockbroker Grapevine 多源最短路徑Floyd

http://poj.org/problem?id=1125 題意:股票經紀人要在一羣人中散佈傳言,傳言只能在認識的人中傳遞。給出經紀人的人數,每個經紀人和其他人關係的信息:編號和傳遞時間,求出以哪個人爲起點可以在耗時最短的情況下讓所有收

原创 POJ2488 Knight's Journey 深搜DFS

A Knight's Journey   Description Background  The knight is getting bored of seeing the same black and white squares aga

原创 藍橋杯 生命之樹 樹形DP

生命之樹 在X森林裏,上帝創建了生命之樹。 他給每棵樹的每個節點(葉子也稱爲一個節點)上,都標了一個整數,代表這個點的和諧值。 上帝要在這棵樹內選出一個非空節點集S,使得對於S中的任意兩個點a,b, 都存在一個點列 {a, v1, v2

原创 PAT 1038 Recover the Smallest Number 貪心

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {

原创 PAT A1085 Perfect Sequence / B1030 完美數列 二分/雙指針

給定一個正整數數列,和正整數 p,設這個數列中的最大值是 M,最小值是 m,如果 M≤mp,則稱這個數列是完美數列。 現在給定參數 p 和一些正整數,請你從中選擇儘可能多的數構成一個完美數列。 輸入格式: 輸入第一行給出兩個正整數 N 和

原创 PAT 甲級1012 The Best Rank 排序

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C -

原创 PAT 1068 Find More Coins 01揹包

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

原创 PAT 1007最大子序列和 1045最長不下降子序列 1040最長迴文子串

1007 Maximum Subsequence Sum dp[i]記錄以A[i]結尾的最大連續子序列和,s[i]記錄該序列對應的起始位置。 狀態轉移方程:dp[i]=max(A[i],dp[i-1]+A[i]) import java