1124 Raffle for Weibo Followers(20 分)

题意:微博抽奖,有M个人,标号为1~M。从第S个人开始,每N个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给下一个人。如果没有得奖的输出“Keep going...”。

分析:按题意模拟即可。cnt表示当前的人距离上一个获奖的人间隔的人数。若cnt==N表示当前人可以获奖,若该人已获奖,可将cnt--,继续判断下一个人。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
map<string, int> mp;
int main(){
int M, N, S;
scanf("%d%d%d", &M, &N, &S);
bool ok = false;
int cnt = 0;
string s;
for(int i = 1; i <= M; ++i){
++cnt;
cin >> s;
if(i == S){
mp[s] = 1;
cout << s <<endl;
ok = true;
cnt = 0;
}
if(ok && cnt == N){
if(!mp.count(s)){
mp[s] = 1;
cnt = 0;
cout << s << endl;
}
else{
--cnt;
}
}
}
if(!ok) printf("Keep going...\n");
return 0;
}
1125 Chain the Ropes(25 分)

题意:将n段绳子连接成一段,每次连接只能两段相连组成新的一段,且新的一段的长度是原来两段长度和的一半,问n段绳子能连接成的最长长度,要求答案下取整。

分析:每次取最短的两段绳子相连,优先队列实现。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<queue>
using namespace std;
priority_queue<double, vector<double>, greater<double> > q;
int main(){
int N, x;
scanf("%d", &N);
while(N--){
scanf("%d", &x);
q.push((double)x);
}
while(q.size() > 1){
double top1 = q.top();
q.pop();
double top2 = q.top();
q.pop();
q.push((top1 + top2) / 2);
}
printf("%d\n", (int)q.top());
return 0;
}
1126 Eulerian Path(25 分)

题意:给定一个无向图,判断是Eulerian,Semi-Eulerian还是Non-Eulerian,并输出每个点的度数。

分析:

1、在一个连通图中若每个结点度数都是偶数,则一定含有欧拉回路,称为Eulerian;

2、在一个连通图中若只有两个结点度数是奇数,则一定含有欧拉通路,且该通路分别从其中一个度数为奇数的点出发,到达另一个度数为奇数的点,称为Semi-Eulerian。

3、因为Eulerian和Semi-Eulerian的前提是连通图,所以首先用并查集判连通。若不连通,则为Non-Eulerian,注意此时要输出每个点的度数。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<queue>
using namespace std;
const int MAXN = 500 + 10;
vector<int> G[MAXN];
vector<int> ans;
set<int> st;
int fa[MAXN];
int Find(int x){
return fa[x] = (x == fa[x]) ? x : Find(fa[x]);
}
int main(){
int N, M;
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; ++i) fa[i] = i;
int x, y;
for(int i = 0; i < M; ++i){
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
int tmpx = Find(x);
int tmpy = Find(y);
if(tmpx < tmpy) fa[tmpy] = tmpx;
else fa[tmpx] = tmpy;
}
for(int i = 1; i <= N; ++i) st.insert(Find(i));
int cnt = 0;
for(int i = 1; i <= N; ++i){
int len = G[i].size();
ans.push_back(len);
if(len % 2 == 1) ++cnt;
}
for(int i = 0; i < N; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
if(st.size() != 1){
printf("Non-Eulerian\n");
}
else{
if(cnt == 0){
printf("Eulerian\n");
}
else if(cnt == 2){
printf("Semi-Eulerian\n");
}
else{
printf("Non-Eulerian\n");
}
}
return 0;
}
1127 ZigZagging on a Tree(30 分)

题意:已知二叉树的中序遍历和后序遍历,求Z字形的层序遍历。即,若根结点为第1层,则偶数层从左到右遍历,奇数层从右到左遍历。

分析:离散化树的结点。递归建树,bfs层序遍历的同时,记录点的层数,然后输出Z字形遍历的结果。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
const int MAXN = 30 + 10;
map<int, int> mp;
int a[MAXN], inorder[MAXN], postorder[MAXN];
int lchild[MAXN], rchild[MAXN];
int cnt;
vector<pair<int, int> > tmp;
vector<int> ans;
int getId(int x){
if(mp.count(x)) return mp[x];
a[++cnt] = x;
return mp[x] = cnt;
}
int build(int iL, int iR, int pL, int pR, int root){
if(iL > iR) return 0;
int id = iL;
while(inorder[id] != root) ++id;
int num = id - iL;
root = getId(root);
lchild[root] = build(iL, id - 1, pL, pL + num - 1, postorder[pL + num - 1]);
rchild[root] = build(id + 1, iR, pL + num, pR - 1, postorder[pR - 1]);
return root;
}
void bfs(int st){
queue<int> x, deep;
x.push(st);
deep.push(1);
tmp.push_back(pair<int, int>(st, 1));
while(!x.empty()){
int tmpx = x.front();
int tmpdeep = deep.front();
x.pop();
deep.pop();
if(lchild[tmpx]){
x.push(lchild[tmpx]);
deep.push(tmpdeep + 1);
tmp.push_back(pair<int, int>(lchild[tmpx], tmpdeep + 1));
}
if(rchild[tmpx]){
x.push(rchild[tmpx]);
deep.push(tmpdeep + 1);
tmp.push_back(pair<int, int>(rchild[tmpx], tmpdeep + 1));
}
}
}
int main(){
int N;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &inorder[i]);
}
for(int i = 0; i < N; ++i){
scanf("%d", &postorder[i]);
}
build(0, N - 1, 0, N - 1, postorder[N - 1]);
bfs(getId(postorder[N - 1]));
int l = tmp.size();
for(int i = 0; i < l; ++i){
if(tmp[i].second != 1 && tmp[i].second % 2 == 1){
stack<int> st;
while(tmp[i].second % 2 == 1){
st.push(tmp[i].first);
++i;
}
--i;
while(!st.empty()){
ans.push_back(st.top());
st.pop();
}
}
else{
ans.push_back(tmp[i].first);
}
}
int len = ans.size();
for(int i = 0; i < len; ++i){
if(i) printf(" ");
printf("%d", a[ans[i]]);
}
printf("\n");
return 0;
}

PAT (Advanced Level) 1124~1127:1124模拟 1125优先队列 1126欧拉通路 1127中序后序求Z字形层序遍历的更多相关文章

  1. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历

    相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...

  2. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  3. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  4. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  5. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  6. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  7. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  8. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  9. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

随机推荐

  1. 简单bat脚本

    hwf.bat: set GAP_HOME=%~dp0\.. ::copy "%JAVA_HOME%\bin\javaw.exe" "%JAVA_HOME%\bin\HW ...

  2. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  3. Flask - flask-mail

    flasky中git reset --hard 8e 问题 使用flask-mail通过163邮箱的smtp服务发送token认证邮件,要关闭TLS才能发送 原理 还在研究中 结果

  4. Plastic Sprayer Supplier - Sprayer: How Can The Product Be Sprayed?

    In many end products, especially in cosmetics, the first thing that appeals to consumers is the form ...

  5. 下载安装charles并导入证书、使用

    抓包原理 1.截获真实客户端的 HTTPS请求,伪装客户端向真实服务端发送 HTTPS 请求. 2.接受真实服务器响应,用 Charles 自己证书伪装服务端向真实客户端发送内容 3.证书导入 看博客 ...

  6. 简单优化MySQL(后续在补充)

    如何优化: ---从设计表结构的角度出发: 用多个小表代替一个大表,注意不要过度设计 批量插入代替循环插入 合理控制缓存空间大小,一般来说其大小设置为几十兆比较合适 可以通过 SQL_CACHE 和 ...

  7. 【原】Linux中常见服务介绍

    1.SSH介绍 简单说,SSH(Secure Shell Protocol)是一种网络协议,用于计算机之间的加密登录.在默认状态下SSH服务提供俩个服务功能,一个是提供类似telnet远程联机服务器的 ...

  8. ssm 框架 使用ajax异步,实现登陆

    只是简单写一下 js.jsp.和controller jsp <%@ page contentType="text/html;charset=UTF-8" language= ...

  9. 《iOS设计模式解析》书籍目录

    1.你好,设计模式 2.案例分析:设计一个应用程序 3.原型 4.工厂方法 5.抽象工厂 6.生成器 7.单例 8.适配器 9.桥接 10.外观 11.中介者 12.观察者 13.组合 14.迭代器 ...

  10. Python学习笔记之面向对象

    下面是初步总结的Python的面向对象的参数定义,后面会逐渐完善面向对象的相关知识.