天梯赛初赛 进阶题 题解
来源:互联网 发布:telnet 3306端口不通 编辑:程序博客网 时间:2024/05/16 01:18
L2-009 抢红包
题目链接:
https://www.patest.cn/contests/gplt/L2-009
简单题,结构体排序
#include <iostream>#include <string.h>#include <string>#include <stdio.h>#include <math.h>#include <algorithm>#include <stdlib.h>#include <map>#include <stack>#include <queue>using namespace std;typedef long long int LL;const int maxn=1e9;int n,m;struct Node{ int pos; int mon; int num;}res[10004];int cmp(Node a,Node b){ if(a.mon==b.mon&&a.num==b.num) return a.pos<b.pos; else if(a.mon==b.mon) return a.num>b.num; return a.mon>b.mon;}int main(){ int x,y; scanf("%d",&n); for(int i=1;i<=n;i++) { res[i].pos=i; res[i].mon=0; res[i].num=0; } for(int i=1;i<=n;i++) { scanf("%d",&m); int num=0; for(int j=1;j<=m;j++) { scanf("%d%d",&x,&y); res[x].mon+=y; res[x].num++; num+=y; } res[i].mon-=num; } sort(res+1,res+n+1,cmp); for(int i=1;i<=n;i++) { printf("%d %.2f\n",res[i].pos,1.0*res[i].mon/100); } return 0;}
L2-010 排座位
题目链接:
https://www.patest.cn/contests/gplt/L2-010
并查集,把是朋友的并在一起,敌对的连一条边
#include <iostream>#include <string.h>#include <algorithm>#include <stdlib.h>#include <stdio.h>#include <math.h>using namespace std;int father[105];int d[105][105];int n,m,k;int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x];}int main(){ int x,y,z; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) father[i]=i; for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); if(z==-1) { d[x][y]=-1; d[y][x]=-1; continue; } int fx=find(x); int fy=find(y); if(fx!=fy) father[fx]=fy; } for(int i=1;i<=k;i++) { scanf("%d%d",&x,&y); find(x),find(y); if(father[x]==father[y]&&d[x][y]!=-1) printf("No problem\n"); else if(father[x]==father[y]&&d[x][y]==-1) printf("OK but...\n"); else if(father[x]!=father[y]&&d[x][y]==-1) printf("No way\n"); else if(father[x]!=father[y]&&d[x][y]!=-1) printf("OK\n"); } return 0;}L2-011 玩转二叉树
题目链接:
https://www.patest.cn/contests/gplt/L2-011
还原二叉树,反转就是在广搜的时候先右子树,再左子树。
#include <iostream>#include <string.h>#include <string>#include <stdio.h>#include <math.h>#include <algorithm>#include <stdlib.h>#include <map>#include <stack>#include <queue>using namespace std;typedef long long int LL;const int maxn=1e9;typedef struct Tree{ int data; Tree *lchild; Tree *rchild;}a[40];int post[40];int in[40];int n;int ans[40];void dfs(int l1,int r1,int l2,int r2,Tree* &root){ root=new Tree(); int i; for( i=l1;i<=r1;i++) if(in[i]==post[l2]) break; root->data=post[l2]; if(i==l1) root->lchild=NULL; else dfs(l1,i-1,l2+1,l2+i-l1,root->lchild); if(i==r1) root->rchild=NULL; else dfs(i+1,r1,r2-(r1-i)+1,r2,root->rchild); }int cnt;void bfs(Tree *tree){ queue<Tree*> q; q.push(tree); while(!q.empty()) { Tree *root=q.front(); q.pop(); ans[cnt++]=root->data; if(root->rchild!=NULL) q.push(root->rchild); if(root->lchild!=NULL) q.push(root->lchild); }}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&in[i]); for(int i=1;i<=n;i++) scanf("%d",&post[i]); Tree *tree; cnt=0; dfs(1,n,1,n,tree); bfs(tree); for(int i=0;i<n;i++) { if(i==n-1) printf("%d\n",ans[i]); else printf("%d ",ans[i]); } return 0;}L2-012 关于堆的判读
题目链接:
https://www.patest.cn/contests/gplt/L2-012
最小堆是一颗完全二叉树,所以我们可以用数组模拟,然后插入的时候比较自己和父节点的大小即可,
#include <iostream>#include <string.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <string>#include <map>using namespace std;int n,m;int a[1005];string b,c;map<int,int>mm;int cnt;void insert(int x){ a[++cnt]=x; int j=cnt; while(j>1) { if(a[j/2]>a[j]) { mm[a[j/2]]=j; swap(a[j/2],a[j]); j=j/2; } else break; } mm[x]=j;}int main(){ int x,y; scanf("%d%d",&n,&m); cnt=0; for(int i=1;i<=n;i++) { scanf("%d",&x); insert(x); } for(int i=1;i<=m;i++) { cin>>x>>b; if(b=="is") { cin>>b; if(b=="the") { cin>>b; if(b=="root") { if(a[1]==x) cout<<"T"<<endl; else cout<<"F"<<endl; continue; } else { cin>>b>>y; if(mm[y]/2==mm[x]) cout<<"T"<<endl; else cout<<"F"<<endl; continue; } } else { cin>>b>>c>>y; if(mm[x]/2==mm[y]) cout<<"T"<<endl; else cout<<"F"<<endl; continue; } } else { cin>>y>>b>>c; if(min(mm[x],mm[y])%2==0&&abs(mm[y]-mm[x])==1) cout<<"T"<<endl; else cout<<"F"<<endl; continue; } } return 0;}
0 0
- 天梯赛初赛 进阶题 题解
- 2016天梯模拟赛 进阶题解
- PAT 天梯赛初赛
- 20160604团队天梯赛初赛
- 【天梯赛-初赛题】L2-004 关于堆的判断
- 天梯赛 登顶题解
- 2016年 团体程序设计天梯赛 - 初赛
- 排座位(天梯赛初赛)
- 天梯赛初赛 5-4 帅到没朋友
- 【PAT】【GPLT】【2016 天梯赛 初赛】 L1-020 帅到没朋友
- 2016年团体程序设计天梯赛-初赛 - 到底有多二 - Java
- 2016年团体程序设计天梯赛-初赛 - 大笨钟 - Java
- 2016年团体程序设计天梯赛-初赛 - 谁先倒 - Java
- 2016年团体程序设计天梯赛-初赛 - 奇偶分家 - Java
- 2016年团体程序设计天梯赛-初赛 - 后天 - Java
- 2016年团体程序设计天梯赛-初赛 - 抢红包
- 2016 天梯赛初赛 排座位(并查集)
- GPLT-天梯赛2016初赛 喊山(bfs)
- markdown新手指南(语法简介)
- CTF 【每日一题20160607】
- 使用Ant工具编译Tomcat源代码(window环境下)
- IO复用:select函数
- Android性能优化方案
- 天梯赛初赛 进阶题 题解
- linux基本命令(59)——rcp命令
- 利用Lambda表达式简化命令者模式
- 资质android声音检测仪---分贝仪 (附有源码)
- checkbox的三种显示状态
- Oracle优化器
- 【leetcode】287. Find the Duplicate Number
- 招聘行业的经验
- HADOOP -hdfs of wordcount