标签:搜索 两种 ons mes str 复杂 情况 one puts
堆
1 #include <iostream> 2 3 using namespace std; 4 5 int const MAX_N=233333; 6 int heap[MAX_N]; 7 int sz,n; 8 9 void push(int); 10 int pop(void); 11 12 int main() 13 { 14 cin >> n; 15 for (int i=0; i<n; i++) 16 { 17 int x; 18 cin >> x; 19 push(x); 20 } 21 for (int i=0; i<sz; i++) cout << heap[i] << " "; 22 cout << endl; 23 n=sz; 24 for (int i=0; i<n/2; i++) 25 cout << "pop: " << pop() << endl; 26 for (int i=0; i<sz; i++) cout << heap[i] << " "; 27 cout << endl; 28 } 29 30 void push(int x) 31 { 32 int i=sz++; 33 while (i>0) 34 { 35 int p=(i-1)/2; 36 if (heap[p]<=x) break; 37 heap[i]=heap[p]; 38 i=p; 39 } 40 heap[i]=x; 41 } 42 43 int pop() 44 { 45 int ret=heap[0]; 46 int x=heap[--sz]; 47 int i=0; 48 while (i*2+1<sz) 49 { 50 int a=i*2+1, b=i*2+2; 51 if (b<sz && heap[b]<heap[a]) a=b; 52 if (heap[a]>=x) break; 53 heap[i]=heap[a]; 54 i=a; 55 } 56 heap[i]=x; 57 return ret; 58 }
1 #include <iostream> 2 3 using namespace std; 4 5 int const MAX_N=100000; 6 int n; 7 int heap[MAX_N]; 8 9 void adjust(); 10 void down(int); 11 void up(int); 12 13 int main() 14 { 15 cin >> n; 16 for (int i=0; i<n; i++) cin >> heap[i]; 17 adjust(); 18 int sz=n; 19 for (int i=1; i<sz; i++) 20 { 21 int t=heap[0]; 22 heap[0]=heap[n-1]; 23 heap[--n]=t; 24 down(0); 25 } 26 for (int i=sz-1; i>=0; i--) cout << heap[i] << " "; 27 } 28 29 void adjust() 30 { 31 int f,t; 32 for (int i=n-1; i>0; i--) 33 { 34 if ((i-1)/2==(i-2)/2 && heap[i-1]<=heap[i]) f=(--i-1)/2; 35 else f=(i-1)/2; 36 if (heap[f]>heap[i]) 37 { 38 t=heap[f]; 39 heap[f]=heap[i]; 40 heap[i]=t; 41 down(i); 42 } 43 } 44 } 45 46 void down(int p) 47 { 48 int s,t; 49 while (p*2+1<n) 50 { 51 if (p*2+2<n && heap[p*2+2]<heap[p*2+1]) s=p*2+2; 52 else s=p*2+1; 53 if (heap[s]<heap[p]) 54 { 55 t=heap[s]; 56 heap[s]=heap[p]; 57 heap[p]=t; 58 p=s; 59 } 60 else break; 61 } 62 } 63 64 void up(int p) 65 { 66 int f,t; 67 while ((f=(p-1)/2)>=0) 68 { 69 if (heap[f]>heap[p]) 70 { 71 t=heap[f]; 72 heap[f]=heap[p]; 73 heap[p]=t; 74 p=f; 75 } 76 else break; 77 } 78 }
1 #include <queue> 2 #include <cstdio> 3 using namespace std; 4 5 int main() 6 { 7 priority_queue<int> pque; 8 pque.push(3); 9 pque.push(5); 10 pque.push(1); 11 while (!pque.empty()) 12 { 13 printf("%d\n",pque.top()); 14 pque.pop(); 15 } 16 return 0; 17 }
Expedition(POJ 2431)
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 23011 | Accepted: 6512 |
Description
Input
Output
Sample Input
4 4 4 5 2 11 5 15 10 25 10
Sample Output
2
Hint
1 #include <queue> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 struct node 7 { 8 int a; 9 int b; 10 }; 11 12 const int MAX_N=10100; 13 int n; 14 int l,p; 15 node s[MAX_N+1]; 16 17 bool compare(const node &x, const node &y) 18 { 19 return x.a<y.a; 20 } 21 22 int main() 23 { 24 scanf("%d",&n); 25 for (int i=0; i<n; i++) 26 { 27 scanf("%d %d", &s[i].a, &s[i].b); 28 } 29 scanf("%d %d", &l, &p); 30 for (int i=0; i<n; i++) s[i].a=l-s[i].a; 31 s[n].a=l; 32 s[n].b=0; 33 sort(s,s+(++n),compare); 34 int ans=0,pos=0,tank=p; 35 priority_queue<int> pque; 36 for (int i=0; i<n; i++) 37 { 38 int d = s[i].a-pos; 39 while (tank-d<0) 40 { 41 if (pque.empty()) 42 { 43 puts("-1"); 44 return 0; 45 } 46 tank += pque.top(); 47 pque.pop(); 48 ans++; 49 } 50 tank -= d; 51 pos=s[i].a; 52 pque.push(s[i].b); 53 } 54 printf("%d\n",ans); 55 }
Fence Repair(POJ 3253)
Time Limit: 2000MS | Memory Limit: 65536K |
Total Submissions: 61237 | Accepted: 20199 |
Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn‘t own a saw with which to cut the wood, so he mosies over to Farmer Don‘s Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn‘t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Output
Sample Input
3 8 5 8
Sample Output
34
Hint
1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #include <queue> 5 6 using namespace std; 7 8 const int MAX_N=20200; 9 int n; 10 int l[MAX_N-1]; 11 12 int main() 13 { 14 cin >> n; 15 for (int i=0; i<n; i++) cin >> l[i]; 16 long long ans=0; 17 priority_queue<int, vector<int>, greater<int>> que; 18 for (int i=0; i<n; i++) 19 { 20 que.push(l[i]); 21 } 22 while (que.size()>1) 23 { 24 int l1,l2; 25 l1=que.top(); 26 que.pop(); 27 l2=que.top(); 28 que.pop(); 29 ans+=l1+l2; 30 que.push(l1+l2); 31 } 32 cout << ans << endl; 33 }
二叉搜索树
1 #include <iostream> 2 3 using namespace std; 4 5 struct node 6 { 7 int val; 8 node *lch, *rch; 9 }; 10 11 node* insert(node *, int); 12 bool find(node *, int); 13 node *remove(node *, int); 14 void inorder(node *); 15 16 int main() 17 { 18 node *root=NULL; 19 int n; 20 cin >> n; 21 int x; 22 for (int i=0; i<n; i++) 23 { 24 cin >> x; 25 root=insert(root,x); 26 } 27 inorder(root); 28 if (find(root,8)) root=remove(root,8); 29 if (find(root,4)) root=remove(root,4); 30 if (find(root,7)) root=remove(root,7); 31 inorder(root); 32 } 33 34 node *insert(node *p, int x) 35 { 36 if (p==NULL) 37 { 38 node *q = new node; 39 q->val=x; 40 q->lch=q->rch=NULL; 41 return q; 42 } 43 else 44 { 45 if (x<p->val) p->lch=insert(p->lch,x ); 46 else p->rch=insert(p->rch, x); 47 return p; 48 } 49 } 50 51 bool find(node *p, int x) 52 { 53 if (p==NULL) return false; 54 else if (x==p->val) return true; 55 else if (x<p->val) return find(p->lch, x); 56 else return find(p->rch, x); 57 } 58 59 node* remove(node *p, int x) 60 { 61 if (p==NULL) return NULL; 62 else if (x<p->val) p->lch=remove(p->lch, x); 63 else if (x>p->val) p->rch=remove(p->rch, x); 64 else if (p->lch==NULL) 65 { 66 node *q=p->rch; 67 delete p; 68 return q; 69 } 70 else if (p->lch->rch==NULL) 71 { 72 node *q = p->lch; 73 q->rch=p->rch; 74 delete p; 75 return q; 76 } 77 else 78 { 79 node *q; 80 for (q=p->lch; q->rch->rch!=NULL; q=q->rch); 81 node *r=q->rch; 82 q->rch=r->lch; 83 r->lch=p->lch; 84 r->rch=p->rch; 85 delete p; 86 return r; 87 } 88 return p; 89 } 90 91 void inorder(node *p) 92 { 93 if (p==NULL) return; 94 inorder(p->lch); 95 cout << p->val << " "; 96 inorder(p->rch); 97 }
1 #include <cstdio> 2 #include <set> 3 using namespace std; 4 int main() 5 { 6 //声明 7 set<int> s; 8 9 //插入元素 10 s.insert(1); 11 s.insert(3); 12 s.insert(5); 13 14 //查找元素 15 set<int> ::iterator ite; 16 ite=s.find(1); 17 if (ite==s.end()) puts("not found"); 18 else puts("found"); 19 ite=s.find(2); 20 if (ite==s.end()) puts("not found"); 21 else puts("found"); 22 23 //删除元素 24 s.erase(3); 25 26 //其它的查找元素的方法 27 if (s.count(3)!=0) puts("found"); 28 else puts("not found"); 29 30 //遍历所有元素 31 for (ite=s.begin(); ite!=s.end(); ++ite) 32 { 33 printf("%d\n",*ite); 34 } 35 36 //清空集合 37 s.clear(); 38 39 return 0; 40 }
map的使用:
1 #include <cstdio> 2 #include <map> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 //声明 8 map<int, const char*> m; 9 10 //插入元素 11 m.insert(make_pair(1, "ONE")); 12 m.insert(make_pair(10, "TEN")); 13 m[100]="HUNDRED";//其它的写法 14 15 //查找元素 16 map<int, const char*> ::iterator ite; 17 ite=m.find(1); 18 puts(ite->second);//(输出)ONE 19 20 ite=m.find(2); 21 if (ite==m.end()) puts("not found"); 22 else puts(ite->second); 23 24 puts(m[10]);//其它的写法 25 26 //删除元素 27 m.erase(10); 28 29 //遍历一边所有元素 30 for (ite=m.begin(); ite!=m.end(); ++ite) 31 { 32 printf("%d: %s\n", ite->first, ite->second); 33 } 34 35 //清空map 36 m.clear(); 37 38 return 0; 39 }
并查集
1 #include <iostream> 2 using namespace std; 3 4 const int MAX_N=10000; 5 int par[MAX_N]; 6 int r[MAX_N]; 7 int n; 8 9 void init(int n);//初始化n个元素 10 int find(int x);//查询树的根 11 void unite(int x, int y);//合并x和y所属的集合 12 bool same(int x, int y);//判断x和y是否属于同一个集合 13 14 int main() 15 { 16 cin >> n; 17 init(n); 18 unite(n/2,n/2+1); 19 cout << same(1,2) << endl; 20 cout << same(5,6) << endl; 21 } 22 23 void init(int n) 24 { 25 for (int i=0; i<n; i++) 26 { 27 r[i]=0; 28 par[i]=i; 29 } 30 } 31 32 int find(int x) 33 { 34 if (par[x]==x) return x; 35 else return par[x]=find(par[x]); 36 } 37 38 void unite(int x, int y) 39 { 40 x=find(x); 41 y=find(y); 42 if (x==y) return; 43 if (r[x]<r[y]) par[x]=y; 44 else 45 { 46 par[y]=x; 47 if (r[x]==r[y]) ++r[x]; 48 } 49 } 50 51 bool same(int x, int y) 52 { 53 return find(x)==find(y); 54 }
食物链(POJ 1182)
Time Limit: 1000MS | Memory Limit: 10000K |
Total Submissions: 90889 | Accepted: 27314 |
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
1 #include <cstdio> 2 3 using namespace std; 4 5 int n,k; 6 int *T,*X,*Y,*par,*r; 7 8 void init(int n) 9 { 10 for (int i=0; i<n; i++) 11 { 12 par[i]=i; 13 r[i]=0; 14 } 15 } 16 17 int find(int x) 18 { 19 if (par[x]==x) return x; 20 return par[x]=find(par[x]); 21 } 22 23 void unite(int x, int y) 24 { 25 x=find(x); 26 y=find(y); 27 if (x==y) return; 28 if (r[x]<r[y]) par[x]=y; 29 else 30 { 31 par[y]=x; 32 if(r[x]==r[y]) ++r[x]; 33 } 34 } 35 36 bool same(int x, int y) 37 { 38 return find(x)==find(y); 39 } 40 41 int main() 42 { 43 scanf("%d %d",&n,&k); 44 T=new int[k]; 45 X=new int[k]; 46 Y=new int[k]; 47 par=new int[n*3]; 48 r=new int[n*3]; 49 for (int i=0; i<k; i++) scanf("%d %d %d",&T[i],&X[i],&Y[i]); 50 //元素x,x+n,x+n*2分别表示x-A,X-B,x-C 51 init(n*3); 52 int ans=0; 53 for (int i=0; i<k; i++) 54 { 55 int t=T[i],x=X[i]-1,y=Y[i]-1; 56 if (x<0 || x>=n || y<0 || y>=n) 57 { 58 ++ans; 59 continue; 60 } 61 if (t==1) 62 { 63 if (same(x,y+n) || same(x,y+n*2)) ans++; 64 else for (int j=0; j<3; j++) unite(x+n*j,y+n*j); 65 } 66 else 67 { 68 if (same(x,y) || same(x,y+n*2)) ans++; 69 else for (int j=0; j<3; j++) unite(x+n*j,y+n*((j+1)%3)); 70 } 71 } 72 printf("%d\n",ans); 73 }
PS:顺便吐槽一下cin、cout的效率,这题一开始用cin、cout一直TLE,后来换了scanf和printf才AC。。。
标签:搜索 两种 ons mes str 复杂 情况 one puts
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9427649.html