Time of Update: 2018-12-05
痛點在於建圖;要將牛拆分成 兩個點,一個與drink相連,一個與food相連,這兩個點也連,他們的權值為一。。#include"stdio.h"#include"string.h"#include"queue"#define inf 9999999int r[900][900];int pre[900];int visit[900];int N,F,D,k;using namespace std;int bfs(int s,int t){int
Time of Update: 2018-12-05
用到了二分冪演算法和公式;羅列一到五可得到規律; 1 2 3 4 51 12 2 13 3 2 14 12 5 2 15 28 12 5 2 1所以可以得到公式A1=1 (n=1
Time of Update: 2018-12-05
#include"stdio.h"#include"string.h"int pre[10000005];int link[10000005];int a[100005],b[100005];int find(int k){return k==pre[k]?k:pre[k]=find(pre[k]);}int main(){int
Time of Update: 2018-12-05
這題效率低的解法還是很容易做出的,只要注意判斷輸入的一種特殊情況即可,詳見代碼,我的解題代碼如下:#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <string>#include <algorithm>using namespace std;struct
Time of Update: 2018-12-05
#include"stdio.h"#include"math.h"int main(){ int n,i,j; double a[10000],b[10000],k,h,f1,f2; while(scanf("%d",&n),n) { h=0.0; for(i=0;i<n;i++) scanf("%lf%lf",&a[i],&b[i]); for(i=0;i<n;i++)
Time of Update: 2018-12-05
這道題要我們判斷所給圖是否可以用兩種顏色進行染色,即"二染色“。已知所給圖一定是強連通圖。分析之:若圖中無迴路,則該圖是一棵樹,一定可以二染色。若圖中有迴路,但迴路有偶數個節點,仍然可以二染色。僅當圖中存在迴路且迴路有奇數個節點時,不能二染色。具體實現細節我在代碼中給出了詳細的注釋,我的解題代碼如下:/*關鍵在於:若且唯若存在奇迴路時,無法二染色*/#include <iostream>#include <cstdio>#include
Time of Update: 2018-12-05
這道題要求按字典序產生字串的全排列,不可重複(但字元可以重複,且區分大小寫)。基本思路是先對輸入的字串按字典序排序,然後從第一位開始遞迴,從所有輸入的字元中選出一個填充,然後再選第二位......具體實現看代碼。要注意的是最後的輸出方式,不小心的話會莫名其妙的WA,詳情見代碼。我的解題代碼如下:#include <iostream>#include <cstdio>#include <cstring>#include
Time of Update: 2018-12-05
解題思路:只考慮第一次,獲得的金幣的平均值為sum/n.sum為所有色子的面的金幣值相加。對於運氣好,搖中了可以再來一次,該輪就能獲得m/n*(sum/n)運氣好,又再來一次,該輪能獲得(m/n)^2*(sum/n)無窮無盡的搖下去,一共能獲得sum/n*(1+p + p^2+`````+p^k + ````),其中p = m/n將式子化簡,就能得到E = sum/(n-m)。所以當sum = 0時為0,n=m時為inf。其餘就為sum/(n-m)。 #include"stdio.h"int
Time of Update: 2018-12-05
最小費用最大流最小費用最大流一般用鄰接表來實現,因為鄰接矩陣不能處理平行邊等等;而一條有向邊是要儲存兩條資訊,無向圖的話要拆成兩條有向邊處理,相當於變為4條邊,這也是鄰接矩陣不能做到的然後最小費用最大流的原理就不講了,講一下實現的要注意的問題和一些技巧1.用結構體數組來儲存邊,最好從下標0開始儲存不要從下標1開始儲存,因為增廣的時候需要用到位元運算,從下標1儲存不利於位元運算2.為了滿足上面的位元運算的要求,除了從下標0開始外,儲存邊的順序也有講究,以一條無向邊做例子(u,v),cap,cost
Time of Update: 2018-12-05
這道題首先要對輸入進行處理,解題的一般思路是將所給的c數組與r數組按照各個曆史事件的rank重排,即最早的事件的編號放在數組的第一位......然後這題轉化為求兩個串的最長公用子序列長度的問題。但我使用了另外一種解法(雖然仍然要用動態規劃 =-= ):只對輸入的c數組重排(即c數組中c[i]存放rank為i的事件的編號),r數組不變。建立ans數組,ans[i]存放以rank為i為結尾的最長序列長度,初始化均為1。程式從第0個開始填充ans數組。當執行到求ans[i]時,分別判斷rank從0 —
Time of Update: 2018-12-05
注意:找規律;1、0*0+x*x=n, sum+=4;2、x*x+x*x=n, sum+=4;3. x*x+y*y=n; sum+=8; 例如25 中的3,4 有(3,4),(3,-4),(-3,4),(-3,-4),(4,3),(4,-3),(-4,3),(-4,-3);八種。 中的5,0, 有(5,0),(-5,0),(0,-5),(0,5) 四種。#include"stdio.h"#include"math.h"int main(){
Time of Update: 2018-12-05
ek演算法效率太低,在這會逾時,用dinic演算法,還不錯。。#include"stdio.h"#include"string.h"#define inf 0x7fffffff#define M 20000int n,m,s,t,adj[M],dis[M];int q[M],p[M],sum;struct point { int v,u; int flow,next;}map[M];int min(int a,int b){ a=a>b?b:a; return a;}void
Time of Update: 2018-12-05
這道題用暴力解法+動態規劃。分析如下:對於某個1*m的矩陣,即一個數列,求其maximal sub-rectangle,可以通過求最大長連續字串和來求得(這個用到了動態規劃)。那麼對於n*m的矩陣,將每列的各個數字求和,將得到一個1*m的矩陣,用上文所說的方法求得的最大和即為該n*m矩陣的所有行數為n的子矩陣中的最大子矩陣和。那麼這道題,通過枚舉所有行數為1、2、3.....N
Time of Update: 2018-12-05
//做這個題的方法就是;把每一個點都一次消為零,//到最後一個點的時候看他是否為零。。#include"stdio.h"#include"string.h"int main(){ int n,i; int a[1000]; while(scanf("%d",&n),n!=-1) { a[1]=n; for(i=2;i<=8;i++) scanf("%d",&a[i]); for(i=2;i<=4;i++) { a[i]=a[i]-a[i-1];
Time of Update: 2018-12-05
#include"stdio.h"#include"string.h"int map[70000];int main(){ int m,i,j; while(scanf("%d",&m)!=EOF) { j=1; for(i=2;i<=m;) { while(m%i) i++; m=m/i; map[j++]=i;
Time of Update: 2018-12-05
這真是一道有趣的題目,不知道別人怎麼想,總之我覺得這題真的很有意思,值得一做。先附上題目:There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer.
Time of Update: 2018-12-05
先將距離從大到小排序;只要兩點之間達到了聯通,便不需要再添加新的距離。。#include <stdio.h> #include <algorithm> using namespace std;#include<stdlib.h> int pre[2000]; struct point { int x,y,z; }a[40000],d; int cmp(point a,point b){ return a.z<b.z;}int
Time of Update: 2018-12-05
#include<stdio.h>#include<algorithm>#include<stdio.h>int pre[2000];using namespace std;struct point { int x,y,z;} a[2000];bool cmp(point a,point b){ return a.z<b.z;} int fun(int x){ if(x!=pre[x])pre[x]=fun(pre[x]);return pre[x];
Time of Update: 2018-12-05
用最大流和並查集應該都可以,但不知為何,最大流老是逾時,囧。。。題意: 給出一幅無向圖,有些節點有財富,有些節點有倉庫。要求把所有財寶運至倉庫,使經過的最大邊最小。解法:
Time of Update: 2018-12-05
和大數階乘一樣,都要用數組儲存,類比手算過程。。#include<stdio.h>#include"string.h"int main(){ int m,n,i,j,k,h,a[40000]; scanf("%d",&n); while(n--) { scanf("%d",&m); memset(a,0,sizeof(a)); a[0]=1; h=1; for(i=1;i<=m;i++) { for(j=0;j<h;j++)