前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >天梯赛初赛 进阶题 题解

天梯赛初赛 进阶题 题解

作者头像
ShenduCC
发布2018-04-27 10:48:01
6470
发布2018-04-27 10:48:01
举报
文章被收录于专栏:算法修养算法修养

L2-009 抢红包

题目链接:

https://www.patest.cn/contests/gplt/L2-009

简单题,结构体排序

代码语言:javascript
复制
#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

并查集,把是朋友的并在一起,敌对的连一条边

代码语言:javascript
复制
#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

还原二叉树,反转就是在广搜的时候先右子树,再左子树。

代码语言:javascript
复制
#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

最小堆是一颗完全二叉树,所以我们可以用数组模拟,然后插入的时候比较自己和父节点的大小即可,

代码语言:javascript
复制
#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;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-06-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档