11/28 PAT(甲级) 训练五

单词组 :
  1. odd degree 奇数点
  2. circuit 回路
  3. even degree 偶数点

1124 Raffle for Weibo Followers (20 分)

John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo – that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.

Input Specification:

Each input file contains one test case. For each case, the first line gives three positive integers M (≤ 1000), N and S, being the total number of forwards, the skip number of winners, and the index of the first winner (the indices start from 1). Then M lines follow, each gives the nickname (a nonempty string of no more than 20 characters, with no white space or return) of a follower who has forwarded John’s post.
Note: it is possible that someone would forward more than once, but no one can win more than once. Hence if the current candidate of a winner has won before, we must skip him/her and consider the next one.

Output Specification:

For each case, print the list of winners in the same order as in the input, each nickname occupies a line. If there is no winner yet, print Keep going… instead.

Sample Input 1:

9 3 2
Imgonnawin!
PickMe
PickMeMeMeee
LookHere
Imgonnawin!
TryAgainAgain
TryAgainAgain
Imgonnawin!
TryAgainAgain

Sample Output 1:

PickMe
Imgonnawin!
TryAgainAgain

Sample Input 2:

2 3 5
Imgonnawin!
PickMe

Sample Output 2:

Keep going…

时间半小时应该还能做快点,题目看太慢。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=100050;
const int INF=0x3f3f3f3f;
int n,skip,start;
string s[MAX];
vector<string>st;
map<string,int>book;
int main()
{
    int flag=0,t;
    scanf("%d%d%d",&n,&skip,&start);
    t=skip;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(i==start)
        {
            st.push_back(s[i]);
            flag=1;
            book[s[i]]=1;
            continue;
        }
        if(flag==1&&(i-start)%t==0)
        {
            if(book[s[i]]==0)
            {
               st.push_back(s[i]);
               book[s[i]]=1;
               start+=t;
               t=skip;
            }
            else if(book[s[i]]==1)
            {
                t++;
            }

        }
    }
    if(flag==0)
    printf("Keep going...");
    else
    {
       for(int i=0;i<st.size();i++)
       {
           cout<<st[i]<<endl;
       }
    }
    return 0;
}

1125 Chain the Ropes (25 分)

Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figure. The resulting chain will be treated as another segment of rope and can be folded again. After each chaining, the lengths of the original two segments will be halved.
Your job is to make the longest possible rope out of N given segments.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (2≤N≤10^​4​​ ). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 10^​4
​​ .

Output Specification:

For each case, print in a line the length of the longest possible rope that can be made by the given segments. The result must be rounded to the nearest integer that is no greater than the maximum length.

Sample Input:

8
10 15 12 3 4 13 1 15

Sample Output:

14

一道简单数学题,假设三段长度分别为2 * x1 , 2 * x2 , 2 * x3 (x1<x2<x3),第一次对折合并,俩极端分别为(x1+x2)/2+x3和(x2+x3)/2+x1,是个递减的,所以用最小堆维护。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=100050;
const int INF=0x3f3f3f3f;
int num[MAX];
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
        q.push(num[i]);
    }
    while(q.size()>1)
    {
        int t1=q.top();
        q.pop();
        int t2=q.top();
        q.pop();
        int t3=(t1+t2)/2;
        q.push(t3);
    }
    printf("%d",q.top());
    return 0;
}

1126 Eulerian Path (25 分)

In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the famous Seven Bridges of Konigsberg problem in 1736. It has been proven that connected graphs with all vertices of even degree have an Eulerian circuit, and such graphs are called Eulerian. If there are exactly two vertices of odd degree, all Eulerian paths start at one of them and end at the other. A graph that has an Eulerian path but not an Eulerian circuit is called semi-Eulerian.
Given an undirected graph, you are supposed to tell if it is Eulerian, semi-Eulerian, or non-Eulerian.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N (≤ 500), and M, which are the total number of vertices, and the number of edges, respectively. Then M lines follow, each describes an edge by giving the two ends of the edge (the vertices are numbered from 1 to N).

Output Specification:

For each test case, first print in a line the degrees of the vertices in ascending order of their indices. Then in the next line print your conclusion about the graph – either Eulerian, Semi-Eulerian, or Non-Eulerian. Note that all the numbers in the first line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

题目看懂!!!题就好写!!
1.不是个连通图,non-Eulerian
2.是个连通图,所有顶点的出度加入度都为偶数时 Eulerian
3.是个连通图,恰好有两个奇数阶顶点,为 semi-Eulerian。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=550;
const int INF=0x3f3f3f3f;
int G[MAX][MAX];
int n,m,ji=0,ou=0,num=0;
int book[MAX];
vector<int>edge[MAX];
void dfs(int start)
{
    num++;
    for(int i=0;i<edge[start].size();i++)
    {
        if(book[edge[start][i]]==0)
        {
        book[edge[start][i]]=1;
        dfs(edge[start][i]);
        }
    }
}
int main()
{
   memset(book,0,sizeof(book));
   memset(G,0,sizeof(G));
   scanf("%d%d",&n,&m);
   for(int i=0;i<m;i++)
   {
       int x,y;
       scanf("%d%d",&x,&y);
       G[x][y]=1;
       G[y][x]=1;
       edge[x].push_back(y);
       edge[y].push_back(x);
   }
   for(int i=1;i<=n;i++)
   {
       printf("%d",edge[i].size());
       if(i!=n)
        printf(" ");
       if(edge[i].size()%2==0)
         ou++;
       else
         ji++;
   }
   book[1]=1;
   dfs(1);
   printf("\n");
   if(num==n&&ou==n)
   {
       printf("Eulerian\n");
   }
   else if(num==n&&ou==n-2)
   {
       printf("Semi-Eulerian\n");
   }
   else
       printf("Non-Eulerian\n");
    return 0;
}

1123 Is It a Complete AVL Tree (30 分)

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.

Sample Input 1:

5
88 70 61 63 65

Sample Output 1:

70 63 88 61 65
YES

Sample Input 2:

8
88 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68
NO

旋转树很长时间没考过,复习记一下6个函数!,getbalance,update,getheight,L,R,,insert
层序遍历输出,问是否是完全AVL,dfs最大index是否等于n即可

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=100050;
const int INF=0x3f3f3f3f;
int n,maxnum=0;
vector<int>st;
struct node
{
    int data,height;
    node* lchild;
    node* rchild;
};
node* newNode(int v)
{
    node* Node=new node;
    Node->data=v;
    Node->height=1;
    Node->lchild=Node->rchild=NULL;
    return Node;
}
int getheight(node* root)
{
    if(root==NULL)
        return 0;
    return root->height;
}
int getbalancefactor(node* root)
{
    return getheight(root->lchild)-getheight(root->rchild);
}
void updateheight(node* root)
{
    root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}
void L(node* &root)
{
    node* temp=root->rchild;
    root->rchild=temp->lchild;
    temp->lchild=root;
    updateheight(root);
    updateheight(temp);
    root=temp;
}
void R(node* &root)
{
    node* temp=root->lchild;
    root->lchild=temp->rchild;
    temp->rchild=root;
    updateheight(root);
    updateheight(temp);
    root=temp;
}
void Insert(node* &root,int x)
{
    if(root==NULL)
    {
        root=newNode(x);
        return ;
    }
    if(x<root->data)
    {
        Insert(root->lchild,x);
        updateheight(root);
        if(getbalancefactor(root)==2)
        {
            if(getbalancefactor(root->lchild)==1)//LL
            {
                R(root);
            }
            else if(getbalancefactor(root->lchild)==-1)//LR
            {
                L(root->lchild);
                R(root);
            }
        }
    }
    else
    {
        Insert(root->rchild,x);
        updateheight(root);
        if(getbalancefactor(root)==-2)
        {
            if(getbalancefactor(root->rchild)==-1)//RR
            {
                L(root);
            }
            else if(getbalancefactor(root->rchild)==1)//RL
            {
                R(root->rchild);
                L(root);
            }
        }
    }
}
void bfs(node *root)
{
    queue<node*>q;
    q.push(root);
    while(!q.empty())
    {
        node* top=q.front();
        q.pop();
        st.push_back(top->data);
        if(top->lchild!=NULL)
            q.push(top->lchild);
        if(top->rchild!=NULL)
            q.push(top->rchild);
    }
}
void dfs(node*root,int index)
{
    if(root==NULL)
        return;
    if(index>maxnum)
    {
        maxnum=index;
    }
    dfs(root->lchild,index*2);
    dfs(root->rchild,index*2+1);

}
int main()
{
    int v;
    scanf("%d",&n);
    node* root=NULL;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&v);
        Insert(root,v);
    }
    bfs(root);
    for(int i=0;i<st.size();i++)
    {
        printf("%d",st[i]);
        if(i!=st.size()-1)
        {
            printf(" ");
        }
    }
    printf("\n");
    dfs(root,1);
    if(maxnum>n)
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值