一周赛事小结
2017-09-10 13:57
211 查看
一周赛事小结
一周赛事小结所学新知识
NO1邻接表
代码
NO2堆
代码
优先队列
单源最短路径
方法总结
不足之处
所学新知识:
NO.1:邻接表:
优点:节省了邻接矩阵的耗费空间的优点代码:
#include <bits/stdc++.h> using namespace std; int u[1000],v[1000],w[1000]; int main() { int n,m; cin>>n>>m; //n为 线段数 m为输入个数 int first[n+1]; int next[m+1]; //======初始化数组====== for(int i=1; i<=n; i++) first[i]=-1; for(int i=1; i<=m; i++) next[i]=-1; for(int i=1; i<=m; i++) { cin>>u[i]>>v[i]>>w[i]; //u[i]---头线段 v[i]---尾节点 next[i]=first[u[i]]; first[u[i]]=i; } for(int i=1; i<=n; i++) { int k=first[i]; while(k!=-1) { cout<<u[k]<<" "<<v[k]<<" "<<w[k]<<endl;; k=next[k]; } } return 0; } /* 6 9 1 2 1 1 3 12 2 3 9 2 4 3 3 5 5 4 3 4 4 5 13 4 6 15 5 6 4 */
NO.2:堆:
优点:询问时方便与查找最大的数或者是最小的数代码:
//本人手打 #include <bits/stdc++.h> using namespace std; const int N=300010; int size=0; struct Heap { int heap ; //=====数组中插入x这个数==== void getin(int x) { size++; int t=size; heap[t]=x; while(heap[t]>heap[t/2]&&t/2>=1) { swap(heap[t],heap[t/2]); t/=2; } } //====数组中输出这个数===== int getout() { int res=heap[1]; heap[1]=heap[size]; size--; int t=1,j=2*t; if(heap[t*2]<heap[t*2+1]) j+=1; while(heap[t]<heap[j]&&j<=size) { swap(heap[t],heap[j]); t=j; j=2*t; if(heap[t*2]<heap[t*2+1]) j+=1; } return res; } //判断的函数 bool judge() { if(size<=0) { cout<<endl<<"this heap is empty!"; return false; } else { return true; } } }; int main() { //srand(time(NULL)); Heap my_heap; int n; for(int i=1; i<=10; i++) { cin>>n; my_heap.getin(n); } for(int i=1; i<=10; i++) { if(my_heap.judge()) cout<<my_heap.getout(); cout<<" "; } return 0; }
优先队列:
优先队列在头文件#include <queue>中; 其声明格式为:priority_queue <int> ans;//声明一个名为ans的整形的优先队列 基本操作有: empty( ) //判断一个队列是否为空 pop( ) //删除队顶元素 push( ) //加入一个元素 size( ) //返回优先队列中拥有的元素个数 top( ) //返回优先队列的队顶元素 优先队列的时间复杂度为O(logn),n为队列中元素的个数,其存取都需要时间。 在默认的优先队列中,优先级最高的先出队。默认的int类型的优先队列中先出队的为队列中较大的数。 然而更多的情况下,我们是希望可以自定义其优先级的,下面介绍几种常用的定义优先级的操作: [cpp] view plain copy #include <iostream> #include <vector> #include <queue> using namespace std; int tmp[100]; struct cmp1 { bool operator ()(int x, int y) { return x > y;//小的优先级高 } }; struct cmp2 { bool operator ()(const int x, const int y) { return tmp[x] > tmp[y]; //tmp[]小的优先级高,由于可以在队外改变队内的值, //所以使用此方法达不到真正的优先,建议用结构体类型。 } }; struct node { int x, y; friend bool operator < (node a, node b) { return a.x > b.x;//结构体中,x小的优先级高 } }; priority_queue<int>q1; priority_queue<int, vector<int>, cmp1>q2; priority_queue<int, vector<int>, cmp2>q3; priority_queue<node>q4; int main() { int i,j,k,m,n; int x,y; node a; while(cin>>n) { for(int i=0;i<n;i++) { cin>>a.y>>a.x; q4.push(a); } cout << endl; while(!q4.empty()) { cout<<q4.top().y <<" "<<q4.top().x<<endl; q4.pop(); } cout << endl; } return 0; }
单源最短路径:
详情请看我的博客!方法总结:
1.能够找到了一种看穿题目的方法,找到突破信息2.学会大致了解英语的题目梗概
3.学会看清楚数据范围,不要超时或者是数据范围之类的!
4.找到了前缀和与后缀和
5.可以试着去发现一些题目的递推公式
6.利用数论来解决问题!想清楚!
7.找规律,对于题目要尽量想到简便的方法来解决它,减少次数
8.注意数据类型的转换
9.搜索树的问题要把握好!
10.要好好考虑数据情况
11.灵活的去剪枝,不然会TLE!哦
12.尽量将循环的次数降低,比如三次降成二次的循环!
13 找出重点来!
14 学会去转换,such as k=a1+a2+a3+a4,可以变成将a4=k-a1-a2-a3,然后去找到a4的知就好,中间用2分查找就好了,将o(n^4)的算法变成了o(n^3*logn)的算法!再想办法变成o(n^2*logn)!!!
不足之处
1.有畏惧难题的情况,看重名次的念头,之后要尽量去改正2.有时候会去偷懒…哎
3.改掉想家的臭毛病!
4.抱着对一道题要错100遍的心态去做它!
5.认真,诚实的去做一件事!