自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(169)
  • 收藏
  • 关注

原创 【PAT】1072 Gas Station (30 分)

#include <bits/stdc++.h>using namespace std;int n,m,k,Ds;struct node{ int v,dist;};map<string,int> mp;vector<node> G[1010];//n:house num//m:候选 气站 num//k:路数量//Ds:气站最大服务范围int dist[1010],visited[1010];priority_queue<pair&l.

2022-02-22 23:34:22 275 1

原创 【PAT】1087 All Roads Lead to Rome (30 分)

三个标尺1.cost最小【边权】2. happiness最大【点权】3.average_happiness最大【经过结点数最少】#include <bits/stdc++.h>using namespace std;#define maxn 210int n,k;string s1;struct citys{ string name; int happiness;};int cost[maxn][maxn];//cost[i][j]=i->j_c.

2022-02-15 18:56:36 452

原创 【PAT】1091 Acute Stroke (30 分)

#include <bits/stdc++.h>using namespace std;int m,n,l,t;int arr[1300][130][80];//1286*128*60struct node{ int x,y,z;};//(1,0,0) (0,1,0) (0,0,1)//(-1,0,0) (0,-1,0) (0,0,-1)int X[6]={1,0,0,-1,0,0};int Y[6]={0,1,0,0,-1,0};int Z[6]={0,0,1.

2022-02-14 23:36:23 393

原创 【PAT】1103 Integer Factorization (30 分)

大的index开头,当前方案还从index开始,下一方案从index-1开始。当前方案下一个还可以是index#include <bits/stdc++.h>using namespace std;int n,k,p;#define maxn 410vector<int> t;void initTablet(){ for(int i=0;pow(i,p)<=n;i++){ t.push_back(pow(i,p)); .

2022-02-14 16:51:43 292

原创 【PAT】1111 Online Map (30 分)

#include <bits/stdc++.h>using namespace std;int n,m;int s1,s2;#define maxn 520int s[maxn][maxn];int t[maxn][maxn];int s_dist[maxn],s_path[maxn],s_tdist[maxn],collected[maxn];struct cmp{ bool operator () (pair<int, int> a, pair<.

2022-02-14 00:39:16 204

原创 【PAT】1122 Hamiltonian Cycle (25 分)

#include <bits/stdc++.h>using namespace std;int n,m,k;int G[220][220];vector<int> path;int num[220];bool isHam(){ fill(num,num+220,0); //1:是不是环 if(path[0]!=path[path.size()-1]) return false; //2:是不是简单路径:每个结点都访问了且仅访问一次,起点除.

2022-02-13 21:37:38 77

原创 【PAT】1126 Eulerian Path (25 分)

区别连通和相邻#include <bits/stdc++.h>using namespace std;//欧拉路径:访问且仅访问每个结点一次//欧拉环:起点=终点的欧拉路径//Eulerian:连通图,所有结点偶数度 ->有一个欧拉环//semi-Eulerian:两个结点奇数度,其中一个是起点,一个是终点#define maxn 520vector<int> G[maxn];int visited[maxn];int cnt=0;void df..

2022-02-13 20:51:43 306

原创 【PAT】1130 Infix Expression (25 分)

#include <bits/stdc++.h>using namespace std;struct node{ string data; int lchild,rchild;};vector<node> T;int Troot=-1;void in_order(int root){ if(root==-1) return; if(T[root].lchild!=-1 && T[root].rchild!=-1 &.

2022-02-13 19:31:08 137

原创 【PAT】1131 Subway Map (30 分)

先建图!!!利用前后驱关系建立无向图;几号线其实属于额外信息,利用hash打表;第一标尺:找经过站点最少的第二标尺:找换乘次数最少的#include <bits/stdc++.h>using namespace std;unordered_map<int,int> line;//v[i][j].line=line[i*10000+j]vector<vector<int> > v(10000);vector<int> t.

2022-02-13 17:14:07 325

原创 【PAT】1142 Maximal Clique (25 分)

#include <bits/stdc++.h>using namespace std;#define maxn 210int nv,ne;int G[maxn][maxn];int tmp[maxn];//通过tmp[]=1,标记当前clique,不然找不在当前clique中的很麻烦int isClique(int k){ //任意两个均连通 ->是clique for(int i=1;i<=nv;i++){ if(tmp[i]==.

2022-02-13 15:55:28 177

原创 【C++STL】set自定义排序,仿函数

struct nnum{ //重载() bool operator () (int v1,int v2){ return v1>v2; }};set<int,nnum> num;//仿函数对set自定义排序,默认递增排序template<class T,class T2>void printSet(set<T,T2> &a){ for (auto it = a.begin(); it != a..

2022-02-13 11:53:34 246 1

原创 【天梯赛】L1-011 A-B (20 分)

#include <bits/stdc++.h>using namespace std;int main(){ string t1,t2; getline(cin,t1); getline(cin,t2); for(int i=0;i<t2.size();i++){ char ch=t2[i]; while(t1.find(ch)!=string::npos){ //str.find(ch)返回第一次出现.

2022-02-11 03:26:04 210

原创 【天梯赛】L1-010 比较大小 (10 分)

//swap()#include <bits/stdc++.h>using namespace std;int main(){ int a,b,c; cin>>a>>b>>c; int ta,tb,tc; if(a>b) swap(a,b); if(a>c) swap(a,c); if(b>c) swap(b,c); printf("%d->%d-&gt.

2022-02-11 02:59:53 357

原创 【天梯赛】L1-009 N个数求和 (20 分)

#include <bits/stdc++.h>using namespace std;long long gcd(long long a,long long b){ return !b? a:gcd(b,a%b);}//最大公约数int main(){ int n; scanf("%d",&n); long long t_fz,t_fm,t_zs=0; long long res_fz=0,res_fm=1,res_zs=0; for.

2022-02-11 02:44:51 331

原创 【天梯赛】L1-002 打印沙漏 (20 分)

#include <bits/stdc++.h>using namespace std;int main(){ int n; char ch; scanf("%d %c",&n,&ch); //特判:n==0,n==1此时line_up=0,没法打印和计算 if(n==0){ cout<<0; return 0; }else if(n==1){ cout<&.

2022-02-10 21:31:41 220

原创 【PAT】1150 Travelling Salesman Problem (25 分)

#include <bits/stdc++.h>using namespace std;#define maxn 210int n,m,k,tk;struct node{ int v,dist;};vector<node> G[maxn];//[1,n]int kPath[maxn];//[1,tk]int sid=-1,ss=INT_MAX;int vis[maxn];void isTS(int times){ fill(vis,vis+.

2022-02-08 20:25:06 318

原创 【洛谷】P1522 [USACO2.4]牛的旅行 Cow Tours

#include <bits/stdc++.h>using namespace std;#define maxn 160#define inf 1e20struct node{ int x,y;};double calS(const node &n1,const node &n2){ return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y));}int nv;int G[maxn.

2022-02-08 16:34:36 265

原创 【洛谷】P1462 通往奥格瑞玛的道路

#include <bits/stdc++.h>using namespace std;struct node{ long long v; long long c;//c:血量->边权 friend bool operator < (const node &n1,const node &n2){//小根堆 return n1.c>n2.c; }};#define maxn 10010long lon.

2022-02-07 20:04:14 178

原创 【洛谷】P1163 银行贷款

#include <bits/stdc++.h>using namespace std;int y,per,m;double f(double w){ double ans=0,v=1; for(int i=1;i<=m;i++){ v*=(1+w); ans+=(per/v); } return ans;}int main(){ scanf("%d %d %d",&y,&per,&am.

2022-02-07 17:30:53 870

原创 【洛谷】P1102 A-B 数对

#include <bits/stdc++.h>using namespace std;#define maxn 200010int a[maxn];int main(){ int n,c; long long ans=0;//最坏情况ans=n^n=4*10^10 -> long long scanf("%d %d",&n,&c); for(int i=0;i<n;i++){ scanf("%d",&a.

2022-02-07 16:38:55 291

原创 【洛谷】P1144 最短路计数

因为无向无权,实际上最短路径长度=该点在bfs搜索树的深度dfs似乎没法解决自环,会一直不断循环当然用Dijkstra做也可以#include <bits/stdc++.h>using namespace std;struct d_node{ int v,w; friend bool operator < (const d_node &d1,const d_node &d2){ return d1.w>d2.w;.

2022-02-06 18:55:19 736

原创 【洛谷】P1629 邮递员送信

单起点最短路->一对多:Dijkstra算法单终点最短路->多对一:反着建图+Dijstra算法#include <bits/stdc++.h>using namespace std;struct node{ int v,w; friend bool operator < (const node &n1,const node &n2){ return n1.w>n2.w; }};int nv.

2022-02-05 21:11:33 198

原创 【洛谷】P3916 图的遍历

//反向建边+nv次dfs【从大到小】#include <bits/stdc++.h>using namespace std;int nv,ne;vector<int> v[100010];int ans[100010];void dfs(int root,int index){ if(ans[root]) return;//被访问过 ans[root]=index; for(int i=0;i<v[root].size();i++).

2022-02-04 20:22:18 309

原创 【数据结构】图的遍历——邻接表实现dfs\bfs

#include <bits/stdc++.h>using namespace std;int nv,ne;vector<int> v[100010];int flag_dfs=0;int visited_dfs[100010];void dfs(int root){ visited_dfs[root]=1; printf("%s%d",flag_dfs==0?"":" ",root); flag_dfs=1; for(int i=.

2022-02-04 19:38:12 575

原创 【PAT】1155 Heap Paths (30 分)

#include <bits/stdc++.h>using namespace std;vector<int> v;int flag_heap=0;vector<int> ans;void dfs(int root){ ans.push_back(root); if((root*2+1)>=v.size() && (root*2+2)>=v.size()){//叶子 for(int i=0;i&l.

2022-02-02 23:55:44 270

原创 【PAT】1004 Counting Leaves (30 分)

#include <bits/stdc++.h>using namespace std;const int MAXN = 110;vector<int> T[MAXN];int num[MAXN];int max_depth=1;void countLeaves(int index,int depth){ if(depth>max_depth) max_depth=depth; if(T[index].size()==0){//叶结点 .

2022-02-02 19:16:55 370

原创 【PAT】1020 Tree Traversals (25 分)

#include <iostream>#include <vector>#include <queue>using namespace std;struct node{ int data; int lchild,rchild;};vector<int> post,in;vector<node> T;int t_index=0;int lca(int inl,int inr,int postRoot){ .

2022-01-30 21:58:16 660

原创 【PAT】1043 Is It a Binary Search Tree (25 分)

pre不建树还原二叉树#include <iostream>#include <vector>using namespace std;vector<int> pre,post;int n;int isMirror=0;void getPost(int root,int tail){ if(root>tail) return; int i=root+1,j=tail; if(!isMirror){ w..

2022-01-29 22:32:26 278

原创 【PAT】1053 Path of Equal Weight (30 分)

dfs遍历树,求路径中结点的权重和,最后按照路径权重排序输出#include <iostream>#include <vector>#include <algorithm>using namespace std;int s;struct node{ int w; vector<int> children;}T[100];vector<vector<int> > path;void dfs.

2022-01-29 20:43:57 761

原创 【PAT】1057 Stack (30 分)

#include <iostream>#include <stack>using namespace std;const int maxn=100010;int cnt[maxn];//前缀和的树状数组序列(单点修改\区间查询)stack<int> s;int lowbit(int x){ return x&(-x);}void update(int x,int k){//单点修改: for(;x<maxn;x+=lowbit(.

2022-01-28 22:51:09 296

原创 【数据结构】树状数组(维护一个线性序列的前缀区间)

区间查询->前缀和->树结构维护(log_2(n))(x-lowbit(x)+1) ->若无子结点是自身,若有子结点是最底层第一个子结点单点修改、区间查询:t[x]初始化为add(x,a[x])返回的和,只修改t[x]不修改a[x]+lowbit(x) -> 父结点,上一层的(连续)前缀和-lowbit(x) ->上一层的(不连续)前缀和-lowbit(x)+1 ...

2022-01-27 21:09:59 290

原创 【PAT】1064 Complete Binary Search Tree (30 分)

排序后,递归插入左-根-右子树#include <iostream>#include <vector>#include <queue>#include <algorithm>using namespace std;struct node{ int data; int lchild,rchild;};int n;int v[1010];vector<node> T;int v_index=0;in..

2022-01-26 21:19:21 324

原创 【PAT】1066 Root of AVL Tree (25 分)

#include <iostream>#include <math.h>using namespace std;struct node{ int data; node* lchild,* rchild;};int getHeight(node* root){ if(!root) return 0; int l=getHeight(root->lchild); int r=getHeight(root->rchild).

2022-01-25 22:44:02 182

原创 【PAT】1079 Total Sales of Supply Chain (25 分)

#include <iostream>#include <vector>#include <math.h>using namespace std;vector<int> T[100010];double p,r;double total_sales=0;void dfs(int root,int depth){ if(T[root].size()==1 && T[root][0]<0){ tot.

2022-01-25 22:15:43 121

原创 【PAT】1086 Tree Traversals Again (25 分)

Push序列:相当于先序遍历序列prePop序列:相当于中序遍历序列in根据两序列还原二叉树#include <iostream>#include <stack>#include <vector>#include <math.h>using namespace std;stack<int> s;vector<int> pre,in;int flag_postOrder=0;void buildT(..

2022-01-25 19:50:09 236

原创 【PAT】1090 Highest Price in Supply Chain (25 分)

dfs从根开始比从叶子结点开始快得多,为了不超时->倒过来存#include <iostream>#include <vector>#include <math.h>using namespace std;vector<int> T[100010];//T[supplier_index]=indexint highest_num=0,highest_depth=-1;void dfs(int root,int depth)..

2022-01-25 19:21:44 47

原创 【PAT】1094 The Largest Generation (25 分)

求静态普通树每层的元素个数#include <iostream>#include <vector>#include <queue>#include <algorithm>using namespace std;vector<int> T[110];int num[110];int max_depth=1;void dfs(int root,int depth){ num[depth]++; if(T[r.

2022-01-25 18:43:50 128

原创 【PAT】1099 Build A Binary Search Tree (30 分)

二叉搜索树的固定顺序:左子树<根<=右子树,按照中序dfs顺序插入即可#include <iostream>#include <vector>#include <queue>#include <algorithm>using namespace std;struct node{ int data; int lchild,rchild;};vector<node> T;int num[110.

2022-01-25 18:17:54 69

原创 【PAT】1102 Invert a Binary Tree (25 分)

①输入的时候就左右子树互换,不用之后再递归互换了②递归左右互换#include <iostream>#include <vector>#include <queue>#include <string.h>using namespace std;struct node{ int lchild,rchild;};vector<node> T;int times[15];int flag_levelOrde..

2022-01-25 17:43:54 117

原创 【PAT】1106 Lowest Price in Supply Chain (25 分)

#include <iostream>#include <math.h>#include <vector>#include <queue>using namespace std;struct node{ vector<int> children;};int n;double p,r;vector<node> C;int cnt=1,min_depth=99999999;void dfs(int...

2022-01-22 23:02:28 286

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除