From c743f823dbbbcd977727d9addb515601ba17b04c Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Thu, 28 Apr 2016 09:12:05 +0800 Subject: [PATCH 01/11] Create 2386.cpp --- POJ/2386.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 POJ/2386.cpp diff --git a/POJ/2386.cpp b/POJ/2386.cpp new file mode 100644 index 0000000..0034821 --- /dev/null +++ b/POJ/2386.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; +char map[105][105]; + +void dfs(int line,int col) +{ + if(map[line][col]=='.'||map[line][col]==0) return; + map[line][col]='.'; + dfs(line-1,col-1); + dfs(line-1,col); + dfs(line-1,col+1); + dfs(line,col-1); + dfs(line,col+1); + dfs(line+1,col-1); + dfs(line+1,col); + dfs(line+1,col+1); +} + +int main() +{ + int n,m; + scanf("%d %d",&n,&m); + for(int i=1;i Date: Thu, 28 Apr 2016 09:14:46 +0800 Subject: [PATCH 02/11] Create 3278.cpp --- POJ/3278.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 POJ/3278.cpp diff --git a/POJ/3278.cpp b/POJ/3278.cpp new file mode 100644 index 0000000..cd3341e --- /dev/null +++ b/POJ/3278.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +using namespace std; + +struct treenode +{ + int value; + treenode *plus,*reduce,*add; +}; + +typedef struct treenode tree; + +tree* root=NULL; + +stack bus; +stack tmpbus; +stack answerbus; + +bool vis[100001]; + +int main() +{ + int n,k; + scanf("%d %d",&n,&k); + root=new (nothrow) tree; + root->value=n; + bus.push(root); + int currentstep=-1; + while(answerbus.empty()) + { + ++currentstep; + while(!bus.empty()) + { + tree* father=bus.top(); + vis[father->value]=true; + if(father->value==k) + { + answerbus.push(father); + break; + } + bus.pop(); + int vplus=father->value*2,vreduce=father->value-1,vadd=father->value+1; + if(vplus<100001&&!vis[vplus]) + { + tree* node=new (nothrow) tree; + node->value=vplus; + father->plus=node; + tmpbus.push(node); + } + if(vadd<100001&&!vis[vadd]) + { + tree* node=new (nothrow) tree; + node->value=vadd; + father->add=node; + tmpbus.push(node); + } + if(vreduce>-1&&!vis[vreduce]) + { + tree* node=new (nothrow) tree; + node->value=vreduce; + father->reduce=node; + tmpbus.push(node); + } + } + while(!tmpbus.empty()) + { + bus.push(tmpbus.top()); + tmpbus.pop(); + } + } + printf("%d\n",currentstep); + return 0; +} From bf5a12d00eaa214f2d3e7bda55264480d457c0b5 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 29 Apr 2016 11:07:23 +0800 Subject: [PATCH 03/11] Create 208_csdn.cpp --- NYOJ/208_csdn.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 NYOJ/208_csdn.cpp diff --git a/NYOJ/208_csdn.cpp b/NYOJ/208_csdn.cpp new file mode 100644 index 0000000..72bca8e --- /dev/null +++ b/NYOJ/208_csdn.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +const int N = 10005; +int arr[N][2]; +bool used[N];//表示第几天被占用了 +int compare(const void *a, const void *b) +{ + int *p1 = (int *)a; + int *p2 = (int *)b; + return p2[0] - p1[0];//价格降序 +} +int main() +{ + int n,i,j,sum,d; + while(scanf("%d", &n) != EOF) + { + memset(used, 0, sizeof(used)); + for(i = 0; i < n; ++i) + scanf("%d %d", &arr[i][0], &arr[i][1]); + qsort(arr, n, 2*sizeof(int), compare); + sum = 0; + for(i = 0; i < n; ++i) + { + for(j = arr[i][1]; j > 0; --j) + { + if(!used[j])//在这之前,有日期可用 + { + used[j] = true; + sum += arr[i][0]; + break; + } + } + } + printf("%d\n", sum); + } + return 0; +} From 2f77163d5c248d1f099b835a14ca6a25b64493f5 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 29 Apr 2016 11:26:22 +0800 Subject: [PATCH 04/11] Create 208.cpp --- NYOJ/208.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 NYOJ/208.cpp diff --git a/NYOJ/208.cpp b/NYOJ/208.cpp new file mode 100644 index 0000000..185aece --- /dev/null +++ b/NYOJ/208.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +using namespace std; + +#define MAXN 10005 +struct pack +{ + int profit,deadline; +}; +pack p[MAXN]; +bool cmp(const pack& a,const pack& b) +{ + return a.profit>b.profit; +} +bool isdayused[MAXN]; + +int main() +{ + int t; + while(scanf("%d",&t)==1) + { + memset(p,0,sizeof(pack)*MAXN); + memset(isdayused,false,sizeof(bool)*MAXN); + for(int i=0;i0;day--) + { + if(!isdayused[day]) + { + isdayused[day]=true; + sum+=p[i].profit; + break; + } + } + } + printf("%d\n",sum); + } + return 0; +} From 48d215f576a68cfb5a79a14874a9e8fc152968b4 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 29 Apr 2016 11:54:18 +0800 Subject: [PATCH 05/11] Another way of NYOJ 208 --- NYOJ/208_2.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 NYOJ/208_2.cpp diff --git a/NYOJ/208_2.cpp b/NYOJ/208_2.cpp new file mode 100644 index 0000000..1240f8e --- /dev/null +++ b/NYOJ/208_2.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +using namespace std; +#define MAXN 10005 + +struct pack +{ + int profit,deadline; +}; + +pack p[MAXN]; + +int father[MAXN]; + +bool cmp(const pack& a,const pack& b) +{ + return a.profit>b.profit; +} + +int GetFather(int pos) +{ + if(father[pos]==-1) + { + /// This pos is itself's father + return pos; + } + /// Else , this pos has a father. So it should know who is his Eldest Father. + father[pos]=GetFather(father[pos]); + /// And then , report back to it's caller. + return father[pos]; +} + +int main() +{ + int t; + while(scanf("%d",&t)==1) + { + /// Reset father + /** This way caused TLE. + for(int i=0;i0) + { + /// This pos's father is not 0 : which means it's possible to sell this thing. + sum+=p[i].profit; + /// And This pos become a son of the pos on its left. + father[ans]=GetFather(ans-1); + } + } + printf("%d\n",sum); + } + return 0; +} From 8a364ad41124dcfe309656cdcecf856193b43c20 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 29 Apr 2016 12:25:20 +0800 Subject: [PATCH 06/11] Create 207_csdn.cpp --- NYOJ/207_csdn.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 NYOJ/207_csdn.cpp diff --git a/NYOJ/207_csdn.cpp b/NYOJ/207_csdn.cpp new file mode 100644 index 0000000..76ee802 --- /dev/null +++ b/NYOJ/207_csdn.cpp @@ -0,0 +1,75 @@ +#include +const int N = 50005; +int parent[N]; +int relation[N];//根节点到节点的关系 +void Init(int n) +{ + for(int i = 0; i <= n; ++i) + { + parent[i]= i; + relation[i] = 0; + } +} +//更新的步骤,先将当前节点与其根节点相连,然后更新其与根节点的关系 +//当前节点x与根节点r的关系更新的方式:(x与其父节点的关系+其父节点的关系与根节点的关系)%3 +//所以在更新节点x的数据之前需要更新其父节点的数据,这是Find为什么搞成递归函数的原因 +//其更新的顺序是从根节点一直往下,一直到当前节点x的父节点。 +int Find(int x) +{ + if(x != parent[x])//不是根节点 + { + int temp = parent[x]; + //将当前节点的父节点设置为根节点 + parent[x] = Find(temp); + //更新当前节点与根节点的关系,由x->x父和x父->父根的关系得到x->父根的关系 + //所以在这之前必须更新其父节点与根节点的关系 + relation[x] = (relation[x] + relation[temp]) % 3; + } + return parent[x]; +} + +int main() +{ + int n,m,i; + int x,y,d; + int rx,ry; + int cnt; + while(scanf("%d %d", &n, &m) != EOF)//POJ上只需要一次输入,所以不需要while循环 + { + cnt = 0; + Init(n); + for(i = 0; i < m; ++i) + { + scanf("%d %d %d", &d, &x, &y); + if(x > n || y > n) + { + ++cnt; + continue; + } + if(d == 2 && x == y) + { + ++cnt; + continue; + } + rx = Find(x); + ry = Find(y); + if(rx == ry)//属于同一个子集 + { + //如果x、y是同类,那么他们相对于根节点的关系应该是一样的 + //如果不是同类,加入y之后,x相对于根节点的关系(x根->y,y->x(即3-(d-1)=2).即x根->x)应该是不变的 + if((d == 1 && relation[x] != relation[y]) || + (d == 2 && relation[x] != (relation[y] + 2)%3)) + ++cnt; + } + else//合并两个连通区域 + { + parent[ry] = rx;//y根的父节点更新成x根 + //(d - 1)为x与y的关系,3-relation[y]是y与y的根节点的关系,注意方向,relation[x]是其根节点与x的关系 + //x根->x,x->y,y->y根:即x根->y根 + relation[ry] = (relation[x] + d - 1 + 3 - relation[y]) % 3; + } + } + printf("%d\n", cnt); + } + return 0; +} From 91f6ebbcb0382c7d0a48c7945aea5fad9e310513 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 29 Apr 2016 12:26:19 +0800 Subject: [PATCH 07/11] Create 207_cnblogs.cpp --- NYOJ/207_cnblogs.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 NYOJ/207_cnblogs.cpp diff --git a/NYOJ/207_cnblogs.cpp b/NYOJ/207_cnblogs.cpp new file mode 100644 index 0000000..43980ec --- /dev/null +++ b/NYOJ/207_cnblogs.cpp @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +const int N = 50005; +int father[N]; +int relation[N];//根点节到点节的关系 + +void init(int n) +{ + for(int i = 0; i <= n; ++i) + { + father[i]= i; + relation[i] = 0; + } +} +//更新的步调,先将当前点节与其根点节相连,然后更新其与根点节的关系 +//当前节点x与根节点r的关系更新的方法: +// (x与其父点节的关系+其父点节的关系与根点节的关系)%3 +//所以在更新节点x的数据之前需要更新其父节点的数据,这是find为什么搞成递归函数的原因 +//其更新的次序是从根节点开始往下,始终到当前点节x的父点节。 +int find(int x) +{ + if(x != father[x])//不是根点节 + { + int temp = father[x]; + //将当前点节的父点节设置为根点节 + father[x] = find(temp); + //更新当前点节与根点节的关系,由x->x父和x父->父根的关系失掉x->父根的关系 + //所以在这之前必须更新其父点节与根点节的关系 + relation[x] = (relation[x] + relation[temp]) % 3; + } + return father[x]; +} + +int main() +{ + int n, m, x, y, d, fx, fy, cnt; + + while(~scanf("%d %d", &n, &m))//POJ上只要需一次入输,所以不要需while循环 + { + cnt = 0; + init(n); + for(int i = 0; i < m; ++i) + { + scanf("%d %d %d", &d, &x, &y); + if(x > n || y > n) + { + ++cnt; + continue; + } + if(d == 2 && x == y) + { + ++cnt; + continue; + } + fx = find(x); + fy = find(y); + if(fx == fy)//属于同一个子集 + { + //如果x、y是同类,那么他们相对根点节的关系应该是一样的 + if(d == 1 && relation[x] != relation[y]) + ++cnt; + //如果不是同类,加入x与y的关系之后,x相对根点节的关系(x根->y,y->x(即3-(d-1)=2).即x根->x)应该是不变的 + //这里d=2表示x - y = 2-1=1;而y->x=3-(x->y)=3-1=2; + if(d == 2 && relation[x] != (relation[y] + 2)%3) + ++cnt; + } + else//合并两个连通区域 + { + father[fy] = fx;//y根的父点节更新成x根 + //(d-1)为x与y的关系,3-relation[y]是y与y的根点节的关系,注意方向,relation[x]是其根点节与x的关系 + //x根->x,x->y,y->y根:即x根->y根 + relation[fy] = (relation[x] + (d-1) + (3-relation[y])) % 3;//注意这里只更新的是fy相对于根的关系 + } + } + printf("%d\n", cnt); + } + return 0; +} From 74c78348796de6d02dc9a5ee5104e3ddfa61ed11 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Fri, 29 Apr 2016 12:54:33 +0800 Subject: [PATCH 08/11] From http://acm.nyist.net/JudgeOnline/bestcode.php?pid=207 --- NYOJ/207_iphxer.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 NYOJ/207_iphxer.cpp diff --git a/NYOJ/207_iphxer.cpp b/NYOJ/207_iphxer.cpp new file mode 100644 index 0000000..58d3ca6 --- /dev/null +++ b/NYOJ/207_iphxer.cpp @@ -0,0 +1,61 @@ + +#include +#include +//#include"iphxer_h.h" +#include +#include +#include +#include + +using namespace std; +#define maxsize 50100 +int rank1[maxsize];//相对于根元素的权值0:同类,1:x吃father[x],2表示被father[x] 吃 +int father[maxsize]; +void initU(int n){ + for(int i=0;in||y>n||(d==2&&x==y)) + ++sum; + else{ + xf=Find(x); + yf=Find(y); + if(xf==yf){ + if((rank1[x]-rank1[y]+3)%3!=d-1) + sum++; + }else{ + Union(x,y,d-1); + } + } + } + cout< Date: Sun, 1 May 2016 14:21:49 +0800 Subject: [PATCH 09/11] Create 207.cpp --- NYOJ/207.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 NYOJ/207.cpp diff --git a/NYOJ/207.cpp b/NYOJ/207.cpp new file mode 100644 index 0000000..67514e0 --- /dev/null +++ b/NYOJ/207.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +#define MAXN 50005 + +int father[MAXN]; +///Relationship: 0 Same 1 Father--eat-->son 2 son--eat-->father +int relation[MAXN]; + + +int find(int pos) +{ + if(father[pos]!=pos) + { + int x=father[pos]; + father[pos]=find(x); + relation[pos]=(relation[pos]+relation[x])%3; + } + return father[pos]; +} + +int main() +{ + int N,K; + while(scanf("%d %d",&N,&K)==2) + { + /// Init + for(int i=0; i<=N; i++) + { + relation[i]=0; + father[i]=i; + } + int a,b,c; + int cnt=0; + for(int i=0;iN||c>N) + { + ++cnt;continue; + } + int rootfather_b=find(b); + int rootfather_c=find(c); + if(rootfather_b==rootfather_c) + { + if(a==1&&relation[b]!=relation[c]) + { + ++cnt;continue; + } + if(a==2&&relation[b]!=(relation[c]+2)%3) + { + ++cnt;continue; + } + } + else + { + father[rootfather_c]=rootfather_b; + relation[rootfather_c]=(relation[b] + (a-1) + (3-relation[c])) % 3; + + } + } + printf("%d\n",cnt); + } + return 0; + +} From ff8c1e167d1332ce40b9726a8d5ed0bfeebee297 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sun, 1 May 2016 14:25:04 +0800 Subject: [PATCH 10/11] Create 1182.cpp --- POJ/1182.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 POJ/1182.cpp diff --git a/POJ/1182.cpp b/POJ/1182.cpp new file mode 100644 index 0000000..6227ece --- /dev/null +++ b/POJ/1182.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +#define MAXN 50005 + +int father[MAXN]; +///Relationship: 0 Same 1 Father--eat-->son 2 son--eat-->father +int relation[MAXN]; + + +int find(int pos) +{ + if(father[pos]!=pos) + { + int x=father[pos]; + father[pos]=find(x); + relation[pos]=(relation[pos]+relation[x])%3; + } + return father[pos]; +} + +int main() +{ + int N,K; + scanf("%d %d",&N,&K); + { + /// Init + for(int i=0; i<=N; i++) + { + relation[i]=0; + father[i]=i; + } + int a,b,c; + int cnt=0; + for(int i=0;iN||c>N) + { + ++cnt;continue; + } + int rootfather_b=find(b); + int rootfather_c=find(c); + if(rootfather_b==rootfather_c) + { + if(a==1&&relation[b]!=relation[c]) + { + ++cnt;continue; + } + if(a==2&&relation[b]!=(relation[c]+2)%3) + { + ++cnt;continue; + } + } + else + { + father[rootfather_c]=rootfather_b; + relation[rootfather_c]=(relation[b] + (a-1) + (3-relation[c])) % 3; + + } + } + printf("%d\n",cnt); + } + return 0; + +} From 949a142b957c551fd6681a8cb31f2045374cca36 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sun, 1 May 2016 14:26:34 +0800 Subject: [PATCH 11/11] Create 3251.cpp --- SDUTOJ/3251.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 SDUTOJ/3251.cpp diff --git a/SDUTOJ/3251.cpp b/SDUTOJ/3251.cpp new file mode 100644 index 0000000..09209b0 --- /dev/null +++ b/SDUTOJ/3251.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct STX +{ + double height,weight; +}; + +bool comp(const STX& a,const STX& b) +{ + return a.height vec; + for(int i=0;ired) + { + printf("blue\n"); + } + else if(blue