zoukankan      html  css  js  c++  java
  • 天梯赛初赛 进阶题 题解

    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
    ",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
    ");
            else if(father[x]==father[y]&&d[x][y]==-1)
                printf("OK but...
    ");
            else if(father[x]!=father[y]&&d[x][y]==-1)
                printf("No way
    ");
            else if(father[x]!=father[y]&&d[x][y]!=-1)
                printf("OK
    ");
        }
        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
    ",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;
    
    }



  • 相关阅读:
    【论文阅读】A practical algorithm for distributed clustering and outlier detection
    第11组 团队Git现场编程实战
    第11组 团队项目-需求分析报告
    团队项目-选题报告
    第二次结对编程作业
    第11组 团队展示
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    (转)script标签到底该放在哪里
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228595.html
Copyright © 2011-2022 走看看