Time of Update: 2018-12-05
中國剩餘定理用來解決以下問題:對於N個兩兩互質的整數,求最小公解X,X滿足X mod N1 = A1, X mod N2 = A2……X mod Nn = An 我國有一本數學古書「孫子算經」有這樣一道問題:「今有物,不知其數,三三數之,剩二;五五數之,剩三;七七數之,剩二。問物幾何?」意思是一個整數除以三餘二,除以五餘三,除以七餘二,求這個整數(滿足條件且最小) 做法是:
Time of Update: 2018-12-05
//離散化處理#include<iostream>#include<vector>#include<cstring>#include<algorithm>using namespace std;struct coordinate//座標結構體{int x,y;int id;//為每個點標記一個ID,方便關聯,id和id+1的便屬於同一個矩形int color;bool used;};vector<coordinate> v;bool
Time of Update: 2018-12-05
DescriptionAlice and Bob need to send secret messages to each other and are discussing ways to encode their messages: Alice: "Let's just use a very simple code: We'll assign 'A' the code word 1, 'B' will be 2, and so on down to 'Z' being assigned 26.
Time of Update: 2018-12-05
//傳統非滾動數組實現形式#include<iostream>#include<cmath>using namespace std;int s[1000][1000];int main(){int T,M,t,v,_max = -1;//T為時間上限(背包容量),M為藥草份數(物品總數)scanf("%d%d",&T,&M);for(int i = 1;i <= M;++i){scanf("%d%d",&t,&v);for(int
Time of Update: 2018-12-05
//簡單快速排序,原來在Sicily上用qsort要添加algorithm的,難怪校賽的時候用next_permutation出錯,原來是沒添加<algorithm>真是可惡!經驗不夠啊!!//POJ跟Sicily 差別這麼大……#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int arr[1000001];int cmp_int(const
Time of Update: 2018-12-05
//並查集的簡單應用——帶路徑壓縮的並查集//平攤分析後,每個點只需遍曆一次即可建立相應並查集,搜尋的複雜度是O(1);//計算不相交集合的個數#include<iostream>using namespace std;int fa[50010],n,m,ans,vis[50010];void initSet()//初始化{for(int i = 1;i <= n;++i)fa[i] = i;}int Find(int x){int temp_x = x;if(x == fa[
Time of Update: 2018-12-05
//並查集,統計不相交集合的元素個數#include<iostream>using namespace std;int n,m,k,fa[30010],num[30010],x,y;void initSet(){for(int i = 0;i < n;++i)fa[i] = i;}int Find(int x){if(x == fa[x])return x;else return fa[x] = Find(fa[x]);//路徑壓縮}void Union(int x,int
Time of Update: 2018-12-05
//最簡便的做法,根據樹的定義來做//樹有N個頂點,N-1條邊//個人覺得這道題比較撲街,資料範圍也沒給,細節那麼多,連空樹、多重邊這種東西都要考慮#include<iostream>using namespace std;int node[1000],edge[1000][1000];//記錄出現過的點和邊bool ok;int x,y,N,E,n;void init(){N = E = n = 0;ok =
Time of Update: 2018-12-05
http://imlazy.ycool.com/post.2059102.html看了這篇文章才做出來的,收穫很大,膜拜神牛!網路流的痛點在於圖的構建以及圖的簡化//網路流——最大流//此題的關鍵是構圖和對圖的簡化以及流量的合并//可參見http://imlazy.ycool.com/post.2059102.html//有非常詳細的講解#include<iostream>#include<queue>#include<cstring>#define INF
Time of Update: 2018-12-05
//網路流——通過構圖用最大流來作做大匹配//將用電器(device)與源點相連//插座(plug)和匯點相連//適配器與插座相連,且權值為INF//將最大匹配問題構圖轉化為最大流問題,再用匯點總流減去最大流得到答案#include<iostream>#include<queue>#include<map>#include<cstring>#include<string>#define INF 2000000000using
Time of Update: 2018-12-05
//二進位高精度運算#include<iostream>#include<cstring>#include<algorithm>struct BinaryNum//高精度結構體{char num[100];int data[100];int len;void reverse()//逆序轉換{for(int i = 0;i < len;++i){data[i] = num[len - i - 1] - '0';}}void print()
Time of Update: 2018-12-05
//類比題,遞迴處理即可,利用string 類強大的可+性,可簡單實現遞迴函式//一開始調用了STL的string類的substr函數,結果restric function了//真是無語。。。#include<iostream>#include<string>using namespace std;string Substr(string word,int position,int len){string substr;for(int i = position;i -
Time of Update: 2018-12-05
//求最小產生樹中的最大邊//第一次寫Kruskal演算法,感謝JSH同學的指點//並查集的優越性,平攤後只需要O(1)的時間即可擷取結果//求最小產生樹時時刻注意邊數和結點數是不同的,不要混淆,因為弄混了WA了3次#include<iostream>#include<cstdlib>#include<algorithm>using namespace std;int G[505][505];int n,t,_max;struct
Time of Update: 2018-12-05
//動態規劃//將板按高度排序//記錄到達每塊板的左右端點所需的最少時間,和每塊板從左右兩邊跳下去的下一塊板指標//從高到低不斷迭代更新最少時間所需值//注意邊緣處理和直接落地的情況以及滿足最大跳樓高度這些條件#include<iostream>#include<algorithm>#define INF 1000000000;using namespace std;struct Plat{int x,y,h;int R_next;//從右端點掉下去的下一塊板int L_
Time of Update: 2018-12-05
//Kruskal + 並查集#include<iostream>#include<algorithm>using namespace std;int fa[101];//父親指標數組int G[105][105];//圖int n,e;//點數和邊數struct edge//存放邊的結構體{int u;int v;int w;}E[5000];bool cmp(edge a,edge b)//對邊權進行排序的排序比較函數{return a.w <
Time of Update: 2018-12-05
//公式遞推//列表找規律,比如讀取各位為8,那麼知道有1個被跳過,讀取十位為3, 那麼算從1到30共有多少位被跳過了,讀取百位為5,計算從1到500共有多少位被跳過了//然後分類討論,讀取的這個位元>=5時是1種情況,<5是另一種情況#include<iostream>#include<cmath>using namespace std;int calculate(int n){int ans = 0;int carry = 1;int bit =
Time of Update: 2018-12-05
//二分圖最大匹配(匈牙利演算法)//行列匹配,按照題目意思是求最小點覆蓋//根據圖論知識,最大匹配數等於最小點覆蓋,因此將問題直接轉化為二分圖最大匹配來處理#include<iostream>#include<cstring>using namespace std;const int MAXN = 505;bool g[MAXN][MAXN];bool check[MAXN];//檢查數組int uMatch[MAXN],vMatch[MAXN],U,V;bool
Time of Update: 2018-12-05
//最大二分匹配(匈牙利演算法)//這題實際上是求最小路徑覆蓋,但根據最小路徑覆蓋的定義可以將它轉化為最大二分匹配//最小路徑覆蓋 = |V| - 最大獨立集 = |V| - 最大二分匹配 (二分圖中最大獨立集 = 最大二分匹配)//但是在構圖過程中,匹配是雙向的,即<u,v> 和<v,u>都算匹配,因此匹配數多了1倍,所以要除以2//最後得出公式: ans = V + maxMatch/2#include<iostream>using
Time of Update: 2018-12-05
Stanford Report, June 14, 2005‘You’ve got to find what you love,’ Jobs saysThis is the text of the Commencement address by Steve Jobs, CEO of Apple Computer and of Pixar Animation Studios, delivered on June 12, 2005.I am honored to be with you today
Time of Update: 2018-12-05
//圖的搜尋遍曆//“We assume that there is only one job processing in one