浅浅地谈一下随机算法【poj2454】【poj3318】
隨機算法我也只是稍微接觸了一下,就是想寫篇博客自己稍微總結一下
其實隨機算法也算是一個玄學吧,運氣不好還是會wa。但是我們知道,計算機可以在短時間內計算大量的數據,所以碰到正確答案的概率還是挺大的。
當然隨機不是隨機答案啦,對于不同的題有可以不同的隨機對象。通常我們會在一下情況下用隨機:
1、逐個枚舉太多無用的情況下選擇隨機 以減少枚舉次數
2、判斷正確性的時候降低暴力判斷的復雜度
下面還是放題吧
poj2454 Jersey Politics
Description
In the newest census of Jersey Cows and Holstein Cows, Wisconsin cows have earned three stalls in the Barn of Representatives. The Jersey Cows currently control the state’s redistricting committee. They want to partition the state into three equally sized voting districts such that the Jersey Cows are guaranteed to win elections in at least two of the districts.
Wisconsin has 3*K (1 <= K <= 60) cities of 1,000 cows, numbered 1..3*K, each with a known number (range: 0..1,000) of Jersey Cows. Find a way to partition the state into three districts, each with K cities, such that the Jersey Cows have the majority percentage in at least two of districts.
All supplied input datasets are solvable.
Input
* Line 1: A single integer, K
* Lines 2..3*K+1: One integer per line, the number of cows in each city that are Jersey Cows. Line i+1 contains city i’s cow census.
Output
* Lines 1..K: K lines that are the city numbers in district one, one per line
* Lines K+1..2K: K lines that are the city numbers in district two, one per line
* Lines 2K+1..3K: K lines that are the city numbers in district three, one per line
Sample Input
2
510
500
500
670
400
310
Sample Output
1
2
3
6
5
4
Hint
Other solutions might be possible. Note that “2 3” would NOT be a district won by the Jerseys, as they would be exactly half of the cows.
translate
在澤西奶牛和荷斯坦奶牛的最新普查中,威斯康星奶牛在谷倉中獲得了三個檔位。 澤西奶牛目前控制著國家重新分配委員會。 他們想將國家分為三個相當大的投票地區,以便保證澤西奶牛在至少兩個地區獲得選舉權。
威斯康星州有3 * K(1 <= K = = 60)個1000頭牛的城市,編號為1..3 * K,每頭都有澤西奶牛的已知數量(范圍:1.001)。 找到一種將州劃分為三個區域的方式,每個區域都有K個城市,使得澤西奶牛在至少兩個地區擁有多數比例。
所有提供的輸入數據集都可以解決。
首先有一個貪心:把序列從大到小排序,取前2*k個作為需要滿足要求的兩個子序列的元素。之后就是對于這2*k個元素如何分配的問題了
如果dfs的話會出事(這是肯定的),那么如何減少枚舉次數呢?就是用隨機算法了
但是如何隨機也是有講究的。如果我們直接隨機這個序列,很有可能兩個子序列的元素并沒有改變(只是交換了一下順序)。我們希望每一次的枚舉都有效,即交換元素。那么很顯然,隨機這個交換的元素就好了
poj3318 Matrix Multiplication
Description
You are given three n × n matrices A, B and C. Does the equation A × B = C hold true?
Input
The first line of input contains a positive integer n (n ≤ 500) followed by the the three matrices A, B and C respectively. Each matrix’s description is a block of n × n integers.
It guarantees that the elements of A and B are less than 100 in absolute value and elements of C are less than 10,000,000 in absolute value.
Output
Output “YES” if the equation holds true, otherwise “NO”.
Sample Input
2
1 0
2 3
5 1
0 8
5 1
10 26
Sample Output
YES
Hint
Multiple inputs will be tested. So O(n3) algorithm will get TLE.
translate
給定三個n×n矩陣A,B和C.方程A×B = C是否成立?
題目的hint說了,暴力的把矩陣乘起來要炸。
那么如果可以找到一個快速的判斷方法,不一定要對錯兩面都具有絕對性,只要其中一面具有絕對性就好。
設v為隨機生成的n維列向量,每個元素取1或0??碅*(B*v)和C*v是否相同。但是這樣不穩定,因為即使A*B不等于C,這樣算出來可能是相等的。經計算,這種情況還相等的概率小于1/2。那么多判斷幾次,出錯的概率就會大大降低。所以試60次就差不多了
轉載于:https://www.cnblogs.com/LinnBlanc/p/7763125.html
總結
以上是生活随笔為你收集整理的浅浅地谈一下随机算法【poj2454】【poj3318】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#的自定义滚动条
- 下一篇: 作业19——夜间模式的开启与关闭,父模板