十一模拟十7 1595. 螺旋矩阵 PAT甲级真题1105

1595. 螺旋矩阵

给定一个包含 N 个正整数的序列,请你将序列中的元素以非递增顺序填充到螺旋矩阵中。

从左上角的第一个元素开始填充,并按照顺时针方向旋转。

要求矩阵有 m 行 n ,并且 m,n 满足:

  • m×n=N
  • m≥n
  • m−n 尽可能小

输入格式

第一行包含整数 N。

第二行包含 N 个整数。

输出格式

输出填充好的 m×n 的矩阵。

数字之间用一个空格隔开,结尾不得有多余空格。

数据范围

1≤N≤10000,
1≤1≤ 序列中元素 ≤10000

输入样例:

12
37 76 20 98 76 42 53 95 60 81 58 93

输出样例:

98 95 93
42 37 81
53 20 76
58 60 76

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

12
37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:

98 95 93
42 37 81
53 20 76
58 60 76

题目大意:将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”~所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充~要求矩阵的规模为m行n列,满足条件:m*n等于N;m>=n;且m-n取所有可能值中的最小值~

分析:

1.一个关键在于如何枚举出这个行大于列,并且行-列尽可能小的矩阵,

2.另一个关键在于数组方向的控制

//1595. 螺旋矩阵
//给定一个包含 N个正整数的序列,请你将序列中的元素以非递增顺序填充到螺旋矩阵中。
//
//从左上角的第一个元素开始填充,并按照顺时针方向旋转。
//
//要求矩阵有 m行 n列,
//并且 m, n满足:
//
//m×n = N
//m≥n
//m−n尽可能小
//
//输入格式
//第一行包含整数 N。
//
//第二行包含 N个整数。
//
//输出格式
//输出填充好的 m×n的矩阵。
//
//数字之间用一个空格隔开,结尾不得有多余空格。
//
//数据范围
//1≤N≤10000,
//1≤序列中元素 ≤10000
//输入样例:
//12
//37 76 20 98 76 42 53 95 60 81 58 93
//
//输出样例:
//98 95 93
//42 37 81
//53 20 76
//58 60 76#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;
const int N = 10010;//yxc
int n;
int w[N];
bool cmp(int a,int b) {return a > b;//这也是大到小排序
}
class Solution {
public:void func() {cin >> n;for (int i = 0; i < n; i++) cin >> w[i];sort(w, w + n, greater<int>());//加了greater<int>()就是从大到小排序int r, c;//行,列//枚举行列//注意i <= n / i,n取12时候,i可以取到1,2,3,不能取到4//然后r=12/3=4,c=i=3,即行是4,列是3for (int i = 1; i <= n / i; i++) {if (n % i == 0) {r = n / i;c = i;}}//创建二维vector,有r行,每行是一个vector(有c个数据)//相当于r行c列,每行是一个vector<int>vector<vector<int>> res(r, vector<int>(c));//二维数组坐标移动,比如从a[0][0]到a[0][1],右移一位,因为这里定义的是//res[x][y],所以是y++的时候,右移一位,y--左移一位//而x++,即纵坐标++,是向下移一位,x--则是向上移动一维//定义4个方向int dx[] = { -1, 0, 1, 0 };//横坐标int dy[4] = { 0, 1, 0, -1 };//纵坐标for (int i = 0, x = 0, y = 0, d = 1; i < n; i++){res[x][y] = w[i];int a = x + dx[d];int b = y + dy[d];//a < 0 || a >= r || b < 0 || b >= c判断越界//res[a][b]判断是否被占用//这里是越界了,或者该点已经被占用了,就进入该ifif (a < 0 || a >= r || b < 0 || b >= c || res[a][b]){d = (d + 1) % 4;//d控制方向,→↓←↑循环a = x + dx[d];//重新给a,b赋值,x,y就是当前点的坐标b = y + dy[d];}x = a, y = b;}//最后输出,考虑pat末尾没空格for (int i = 0; i < r; i++) {cout << res[i][0];for (int j = 1; j < c; j++)cout << ' ' << res[i][j];cout << endl;}}
};class Solution2 {
public:void func() {}
};int main() {Solution s;s.func();return 0;
}

十一模拟十8 1599. 合影 PAT甲级真题1109

1599. 合

合影时队形非常重要,给出 N 个人排成 K行的规则,如下所示:

  • 每行的人数必须为 N/K(向下取整),所有多余的人(如果有)都放到最后一行;
  • 位于后排的所有人都不得矮于位于前排的任何人;
  • 在每一行中,最高的人站在该行的中心位置(定义为位置 (m/2+1)(/2+1),位置从1开始编号,其中 m是该行的总人数,除法结果向下取整);
  • 在每一行中,其他人必须按照其身高非递增顺序依次排入该行,交替地将他们的位置先安排到最高人的右侧,然后再安排到最高人的左侧(例如,假设五个人的身高为 190、188,186、175、170190、188,186、175、170,最终阵型将是 175、188、190、186、170175、188、190、186、170。在这里,我们假设你面对着合影人员,因此你的左手边位置其实是最高人的右手边位置。);
  • 当许多人的身高相同时,必须按姓名的字典序升序进行排序,保证所有人的姓名不重复。

现在,给定一群人的相关信息,请你将他们的合照队形排好。

输入格式

第一行包含两个整数,N 表示总人数,K 表示排成的行数。

接下来 N行,每行包含一个人的姓名和身高。

姓名是长度不超过 88 的由大小写字母构成的字符串,身高是范围在 [30,300][30,300] 的整数。

输出格式

输出排好的合影队形,即在 K行输出人员姓名。

名字之间用空格隔开,行尾不得由多余空格。

注意,由于你面对着合影人员,因此,后排的人在上方输出,前排的人在下方输出。

数据范围

1≤N≤10^4,
1≤K≤10

输入样例:

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159

输出样例:

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:

The number of people in each row must be N/K (round down to the nearest integer), with all the extra people (if any) standing in the last row;
All the people in the rear row must be no shorter than anyone standing in the front rows;
In each row, the tallest one stands at the central position (which is defined to be the position (m/2+1), where m is the total number of people in that row, and the division result must be rounded down to the nearest integer);
In each row, other people must enter the row in non-increasing order of their heights, alternately taking their positions first to the right and then to the left of the tallest one (For example, given five people with their heights 190, 188, 186, 175, and 170, the final formation would be 175, 188, 190, 186, and 170. Here we assume that you are facing the group so your left-hand side is the right-hand side of the one at the central position.);
When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.
Now given the information of a group of people, you are supposed to write a program to output their formation.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers N (<=10000), the total number of people, and K (<=10), the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in [30, 300]).

Output Specification:

For each case, print the formation — that is, print the names of people in K lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.

Sample Input:

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159

Sample Output:

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

题目大意:拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下:
每排人数为N/K(向下取整),多出来的人全部站在最后一排;后排所有人的个子都不比前排任何人矮;每排中最高者站中间(中间位置为m/2+1,其中m为该排人数,除法向下取整);每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者,所以你的左边是中间人的右边);若多人身高相同,则按名字的字典序升序排列。这里保证无重名。现给定一组拍照人,请编写程序输出他们的队形。输出拍照的队形。即K排人名,其间以空格分隔,行末不得有多余空格。注意:假设你面对拍照者,后排的人输出在上方,前排输出在下方~

分析:

1.注意是从中间位置,一个左边,一个右边交替排序,用到双指针。

//1599. 合影
//
//合影时队形非常重要,给出 N个人排成 K行的规则,如下所示:
//
//每行的人数必须为 N / K(向下取整),所有多余的人(如果有)都放到最后一行;
//位于后排的所有人都不得矮于位于前排的任何人;
//在每一行中,最高的人站在该行的中心位置(定义为位置(m / 2 + 1),位置从1开始编号,
//其中 m是该行的总人数,除法结果向下取整);
//在每一行中,其他人必须按照其身高非递增顺序依次排入该行,交替地将他们的位置先安排到最高人的右侧,
//然后再安排到最高人的左侧(例如,假设五个人的身高为 190、188,186、175、170,
//最终阵型将是 175、188、190、186、170。在这里,我们假设你面对着合影人员,
//因此你的左手边位置其实是最高人的右手边位置。);
//当许多人的身高相同时,必须按姓名的字典序升序进行排序,保证所有人的姓名不重复。
//现在,给定一群人的相关信息,请你将他们的合照队形排好。
//
//输入格式
//第一行包含两个整数,N表示总人数,K表示排成的行数。
//
//接下来 N行,每行包含一个人的姓名和身高。
//
//姓名是长度不超过 8的由大小写字母构成的字符串,身高是范围在[30, 300]的整数。
//
//输出格式
//输出排好的合影队形,即在 K行输出人员姓名。
//
//名字之间用空格隔开,行尾不得由多余空格。
//
//注意,由于你面对着合影人员,因此,后排的人在上方输出,前排的人在下方输出。
//
//数据范围
//1≤N≤104,
//1≤K≤10
//
//输入样例:
//10 3
//Tom 188
//Mike 170
//Eva 168
//Tim 160
//Joe 190
//Ann 168
//Bob 175
//Nick 186
//Amy 160
//John 159
//输出样例:
//Bob Tom Joe Nick
//Ann Mike Eva
//Tim Amy John#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;
const int N = 10010;int n, m;
struct Person{string name;int h;//身高bool operator< (const Person& t) const{if (h != t.h) return h > t.h;return name < t.name;}
}p[N];bool cmp(Person a, Person b) {if (a.h != b.h) {return a.h > b.h;}else {return a.name < b.name;}
}string line[N];//保存一行的人
class Solution {
public:void func() {cin >> n >> m;//输入学生姓名,身高for (int i = 0; i < n; i++) cin >> p[i].name >> p[i].h;//身高从大到小排序sort(p, p + n);//i循环每行,在每行之中,j循环每个人for (int i = 0, j = 0; i < m; i++){int len = n / m;if (i==0) len += n % m; //计算机输出的第一排,就是拍照队伍 最后一排//排序是中间的人(r=len/2+1)最高,然后左边一个,右边一个,交替地排序,双指针//for (int r = len / 2 + 1, l = r - 1; l > 0 || r <= len; l--, r++){if (r <= len) {line[r] = p[j++].name;//j循环每个人}if (l > 0) {line[l] = p[j++].name;}}//输出该行人物,注意pat末尾无空格cout << line[1];for (int k = 2; k <= len; k++) cout << ' ' << line[k];cout << endl;}}
};class Solution2 {
public:void func() {}
};int main() {Solution s;s.func();return 0;
}//1.交替排序,用到双指针

十一模拟十9 1614. 单身狗 PAT甲级真题1121

1614. 单身狗

“单身狗”是中文对于单身人士的一种爱称。

本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。

输入格式

输入第一行给出一个正整数 N,是已知夫妻/伴侣的对数;

随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 55 位数字(从 0000000000 到 9999999999),ID 间以空格分隔;

之后给出一个正整数 M,为参加派对的总人数;

随后一行给出这 M 位客人的 ID,以空格分隔。

题目保证无人重婚或脚踩两条船。

输出格式

首先第一行输出落单客人的总人数;

随后第二行按 ID 递增顺序列出落单的客人。

ID 间用 11 个空格分隔,行的首尾不得有多余空格。

数据范围

1≤N≤50000,
1≤M≤10000

输入样例:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

输出样例:

5
10000 23333 44444 55555 88888

"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID's which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (<=10000) followed by M ID's of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output Specification:

First print in a line the total number of lonely guests. Then in the next line, print their ID's in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Sample Input:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

Sample Output:

5
10000 23333 44444 55555 88888

思路:n条以知couple输入进结构体数组,用 unordered_set<int> S 存储m个客人
然后遍历结构体数组,取出couple对儿,在S中查找这俩,如果同时存在,则删除
剩下的就是单独的,输出即可,简单题

//1614. 单身狗
//
//“单身狗”是中文对于单身人士的一种爱称。
//
//本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。
//
//输入格式
//输入第一行给出一个正整数 N,是已知夫妻 / 伴侣的对数;
//
//随后 N行,每行给出一对夫妻 / 伴侣——为方便起见,
//每人对应一个 ID号,为 5位数字(从 00000到 99999),ID 间以空格分隔;
//
//之后给出一个正整数 M,为参加派对的总人数;
//
//随后一行给出这 M位客人的 ID,以空格分隔。
//
//题目保证无人重婚或脚踩两条船。
//
//输出格式
//首先第一行输出落单客人的总人数;
//
//随后第二行按 ID
//递增顺序列出落单的客人。
//
//ID
//间用 1个空格分隔,行的首尾不得有多余空格。
//
//数据范围
//1≤N≤50000,
//1≤M≤10000
//
//输入样例:
//3
//11111 22222
//33333 44444
//55555 66666
//7
//55555 44444 10000 88888 22222 11111 23333
//输出样例:
//5
//10000 23333 44444 55555 88888#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;
const int N = 50010, M = 10010;//yxc:思路,n条以知couple输入进结构体数组,用 unordered_set<int> S 存储m个客人
//然后遍历结构体数组,取出couple对儿,在S中查找这俩,如果同时存在,则删除
//剩下的就是单独的,输出即可,简单题
struct Couple
{int a, b;
}c[N];
int ans[M];
class Solution {
public:void func() {int n, m;scanf("%d", &n);//结构体的初始化,数据输入for (int i = 0; i < n; i++) scanf("%d%d", &c[i].a, &c[i].b);scanf("%d", &m);//去重,无序,这也是哈希表,好处是操作都是o(1)级别unordered_set<int> S;//m个查询放入S中for (int i = 0; i < m; i++){int id;scanf("%d", &id);S.insert(id);}//遍历所有情侣for (int i = 0; i < n; i++){//取出是一对儿的情侣int a = c[i].a, b = c[i].b;//如果S中有这一对儿情侣,就删除,操作时间复杂度o(1)if (S.count(a) && S.count(b)){S.erase(a);S.erase(b);}}int k = 0;for (auto id : S) {ans[k++] = id;}sort(ans, ans + k);//要按id递增输出printf("%d\n", k);//输出又几个落单的//如果k!=0,输出,注意pat末尾无空格if (k){printf("%05d", ans[0]);for (int i = 1; i < k; i++) printf(" %05d", ans[i]);}}
};class Solution2 {
public:void func() {}
};int main() {Solution s;s.func();return 0;
}//1.用到了unordered_set<int> S 的 cout()方法来查找是否包含该整数
//2.哈希表的操作都是o(1)

十一模拟十十  1621. N 皇后问题 PAT甲级真题1128

1621. N 皇后问题

N 皇后问题是指将 N�个皇后放置在 N×N 棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行、同一列或同一斜线上。

在本题中,你无需解决这一难题。

你需要做的是判断我们给出的棋子摆放是否是一种合理的摆放方案,即是否能够满足皇后之间不能相互攻击到。

为了简化棋盘的表示,让我们假设在同一列中不会放置两个皇后。

这样我们就可以用一个整数序列 Q1,Q2,…,QN来表示一种棋盘摆放,其中 Qi 表示第 i 列的皇后所在的行号。

例如,下方左图的棋盘摆放可以用 (4, 6, 8, 2, 7, 1, 3, 5) 来表示,它是解决八皇后问题的一种合理摆放方案。

下方右图的棋盘摆放可以用 (4, 6, 7, 2, 8, 1, 9, 5, 3) 来表示,它并不是解决九皇后问题的一种合理摆放方案。

输入格式

第一行包含整数 K,表示共有 K 组测试数据。

每组测试数据占一行,首先包含整数 N,然后包含 N 个整数 Q1,Q2,…,QN

输出格式

对于每组数据,如果是合理摆放方案,则输出 YES,否则输出 NO

每个答案占一行。

数据范围

1<K≤200,
4≤N≤1000
1≤Qi≤N1

输入样例:

4
8 4 6 8 2 7 1 3 5
9 4 6 7 2 8 1 9 5 3
6 1 5 2 6 4 3
5 1 3 5 2 4

输出样例:

YES
NO
NO
YES

The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general N queens problem of placing N non-attacking queens on an N×N chessboard. (From Wikipedia - "Eight queens puzzle".)

Here you are NOT asked to solve the puzzles. Instead, you are supposed to judge whether or not a given configuration of the chessboard is a solution. To simplify the representation of a chessboard, let us assume that no two queens will be placed in the same column. Then a configuration can be represented by a simple integer sequence (Q1, Q2, ..., QN), where Qi is the row number of the queen in the i-th column. For example, Figure 1 can be represented by (4, 6, 8, 2, 7, 1, 3, 5) and it is indeed a solution to the 8 queens puzzle; while Figure 2 can be represented by (4, 6, 7, 2, 8, 1, 9, 5, 3) and is NOT a 9 queens' solution.

Input Specification:

Each input file contains several test cases. The first line gives an integer K (1 < K <= 200). Then K lines follow, each gives a configuration in the format "N Q1 Q2 ... QN", where 4 <= N <= 1000 and it is guaranteed that 1 <= Qi <= N for all i=1, ..., N. The numbers are separated by spaces.

Output Specification:

For each configuration, if it is a solution to the N queens problem, print "YES" in a line; or "NO" if not.

Sample Input:
4
8 4 6 8 2 7 1 3 5
9 4 6 7 2 8 1 9 5 3
6 1 5 2 6 4 3
5 1 3 5 2 4
Sample Output:
YES
NO
NO
YES

题目大意:给出一个皇后图,以这样的方式给出:一个数组包含n个数字,每个数字表示该列的皇后所在的行数~判断给出的皇后图是否满足不会互相攻击(任意两个皇后都要不在同一行或者同一列,且不在斜对角线上~)
分析:用v[n]存储一张图给出的数字~对于第j个数字,判断前0~j-1个数字中是否有在同一行的(v[j] == v[t])和在斜对角线上的(abs(v[j]-v[t]) == abs(j-t))【因为已经告诉肯定不在同一列了,所以不需要判断是否在同一列啦~】如果发现了不满足的情况,就将result由true标记为false~最后根据result是true还是false输出对应的YES还是NO即可~

分析:假设n=5,最左下角坐标是x=1,y=1,dg[x+y]=dg[2],udg[y-x+n]=udg[5]

//1621. N 皇后问题
//
//N皇后问题是指将 N个皇后放置在 N×N棋盘上,使得皇后不能相互攻击到,
//即任意两个皇后都不能处于同一行、同一列或同一斜线上。
//
//在本题中,你无需解决这一难题。
//
//你需要做的是判断我们给出的棋子摆放是否是一种合理的摆放方案,即是否能够满足皇后之间不能相互攻击到。
//
//为了简化棋盘的表示,让我们假设在同一列中不会放置两个皇后。
//
//这样我们就可以用一个整数序列 Q1, Q2, …, QN来表示一种棋盘摆放,
//其中 Qi表示第 i列的皇后所在的行号。
//
//例如,下方左图的棋盘摆放可以用(4, 6, 8, 2, 7, 1, 3, 5) 来表示,它是解决八皇后问题的一种合理摆放方案。
//
//下方右图的棋盘摆放可以用(4, 6, 7, 2, 8, 1, 9, 5, 3) 来表示,它并不是解决九皇后问题的一种合理摆放方案。
//
//1.jpg 2.jpg
//
//输入格式
//第一行包含整数 K,表示共有 K组测试数据。
//
//每组测试数据占一行,首先包含整数 N,然后包含 N个整数 Q1, Q2, …, QN。
//
//输出格式
//对于每组数据,如果是合理摆放方案,则输出 YES,否则输出 NO。
//
//每个答案占一行。
//
//数据范围
//1 < K≤200,
//4≤N≤1000
//
//1≤Qi≤N
//输入样例:
//4
//8 4 6 8 2 7 1 3 5
//9 4 6 7 2 8 1 9 5 3
//6 1 5 2 6 4 3
//5 1 3 5 2 4
//
//输出样例:
//YES
//NO
//NO
//YES#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;
const int N = 1010;//yxc
//题目给了条件每个列都只有一个皇后,故只需判断每行,和对角线是否有皇后
//判断:开个bool数组,插入时候判断下该行是否有皇后,同理,每个对角线也能开个数组
//对角线([词典]diagonal line)如何开数组?主对角线(左上到右下)用y-x+n表示,副对角线用y+x表示
int n;
bool row[N];//行
bool dg[N * 2];//正对角线,N行N列 应该有2N-1个正对角线
bool udg[N * 2];//负对角线
class Solution {
public:void func() {int T;//几次查询scanf("%d", &T);while (T--){scanf("%d", &n);//棋盘n行n列/*memset(row, 0, sizeof row);memset(dg, 0, sizeof dg);memset(udg, 0, sizeof udg);*///每次查询之前,3个数组清空下,再重新输入新的查询fill(row, row + N, 0);fill(dg, dg + N * 2, 0);fill(udg, udg + N * 2, 0);bool success = true;//表示是否有冲突,ture是无冲突,可莉方案for (int y = 1; y <= n; y++){//枚举列,y是列int x;//x代表行scanf("%d", &x);//row[x]判断第x行有没有,有就冲突if (row[x] || dg[x + y] || udg[y - x + n]) {success = false;//有冲突}//标记下row[x],dg[x + y],udg[y - x + n] 都有元素了row[x] = dg[x + y] = udg[y - x + n] = true;}if (success) puts("YES");//yes代表无冲突,是合理方案else puts("NO");}}
};class Solution2 {
public:void func() {}
};int main() {Solution s;s.func();return 0;
}

十一模拟十十2 1622. 推荐系统 PAT甲级真题1129

1622. 推荐系统

推荐系统可以预测用户对某项商品的偏好。

现在,请你编写一个非常简单的推荐系统,该系统通过分析用户对各种商品的访问次数来预测用户的偏好。

补充

用户会进行 N 次商品访问,每次访问某件商品之前,系统需要给用户推荐最多不超过 K 件商品。

而推荐的这 K件商品,应该是在本次访问之前的所有访问中,被访问次数最多的 K 件商品。

当然,用户在进行第一次访问时,无法给出任何推荐,因为此时所有商品的被访问次数都为 00。

输入格式

第一行包含两个正整数: N,表示用户访问商品次数,K 表示系统提供给用户的最大推荐数。

第二行给出用户正在访问的商品编号。

所有商品编号 1∼N。

输出格式

对于每次访问,以如下格式,输出一行建议:

query: rec[1] rec[2] ... rec[K]

其中,query 表示用户正在访问的商品,rec[i] 是推荐系统推荐给用户的第 i个商品。

应当以频率从高到低的顺序推荐访问频率最高的前 K 个商品。

对于访问频率相同的商品,优先推荐商品编号小的。

注意:对于第一次访问没有任何输出,因为无法给出建议。

数据范围

1≤N≤50000
1≤K≤10

输入样例:

12 3
3 5 7 5 5 3 2 1 8 3 8 12

输出样例:

5: 3
7: 3 5
5: 3 5 7
5: 5 3 7
3: 5 3 7
2: 5 3 7
1: 5 3 2
8: 5 3 1
3: 5 3 1
8: 3 5 1
12: 3 5 8

Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user’s preference by the number of times that an item has been accessed by this user.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers: N (<= 50000), the total number of queries, and K (<= 10), the maximum number of recommendations the system must show to the user. Then given in the second line are the indices of items that the user is accessing -- for the sake of simplicity, all the items are indexed from 1 to N. All the numbers in a line are separated by a space.

Output Specification:

For each case, process the queries one by one. Output the recommendations for each query in a line in the format:

query: rec[1] rec[2] ... rec[K]

where query is the item that the user is accessing, and rec[i] (i = 1, ... K) is the i-th item that the system recommends to the user. The first K items that have been accessed most frequently are supposed to be recommended in non-increasing order of their frequencies. If there is a tie, the items will be ordered by their indices in increasing order.

Note: there is no output for the first item since it is impossible to give any recommendation at the time. It is guaranteed to have the output for at least one query.

Sample Input:
12 3
3 5 7 5 5 3 2 1 8 3 8 12
Sample Output:
5: 3
7: 3 5
5: 3 5 7
5: 5 3 7
3: 5 3 7
2: 5 3 7
1: 5 3 2
8: 5 3 1
3: 5 3 1
8: 3 5 1
12: 3 5 8

题目大意:根据用户每次点击的东西的编号,输出他在点当前编号之前应该给这个用户推荐的商品的编号~只推荐k个~也就是输出用户曾经点击过的商品编号的最多的前k个~如果恰好两个商品有相同的点击次数,就输出编号较小的那个~
分析:【并不复杂~只是写的比较详细~不怕不怕~(摸头...】因为每个商品有两个属性:编号value和出现的次数cnt,编号具有唯一性,然后set又会根据大小自动排序,所以我们可以考虑将value和cnt组成一个node属性,把所有商品编号和它对应的次数变成node放入set里面,重载小于号,使<根据set中node的cnt排序,如果cnt相等就按照node的value排序~
这样set里面就是按照出现次数排序好的商品node,每次输出set的前k个node的value值就可以~(要记住,因为是点击前推荐,所以我们在接收完num的值后,应该先输出再插入让set帮忙排序当前num,当前的这个点击编号暂时先不算在输出结果里面的~)
首先struct node 里面要定义一个构造函数node(int a, int b),将a赋值value,b赋值cnt~这个是为了以后查找node和插入node的时候方便~
然后重载<号,如果当前cnt不等于a.cnt就将cnt大的排在前,否则将value小的排在前面~
每次输入的时候,先不插入,先输出,当i != 0时候开始输出,因为i==0时候用户才第一次点击,没有可以推荐的~(沮丧脸...) 输出的同时记录输出过的个数tempCnt,当tempCnt < k的时候输出,因为只要前k个~所以就从头到尾依次输出set中的值就可以啦~
book[num]标记num出现的次数,每次寻找set中当前值为num和次数为book[num]的那个值,如果找到了就把他移除,(找到说明这个数已经出现过啦,cnt已经不对啦,先移除掉吧~)然后将book[num]+1,在将node(num, book[num])插入到set中,set会帮忙根据我们自定义的<的规则自动排序哒~

我分析:yxc:

输出:

int n, m;//n是浏览的商品总数,m是每次浏览新商品就推荐m个商品
int cnt[N];//存放当前每个商品的出现次数
int top_k[11];//存放前K个商品,因为题目条件给了k最大为10,所以这里开个大小为11的数组

过程:

维护一个int top_k[11]数组,用于存放需要推荐的0~k-1个商品。每次输入新商品,0~k-1个商品里如果不存该商品,则加入top_k的第下标k位,对其排序,sort(top_k, top_k + k, cmp);这样0~k-1个商品就是访问次数最多的那k个商品

//1622. 推荐系统
//
//推荐系统可以预测用户对某项商品的偏好。
//
//现在,请你编写一个非常简单的推荐系统,该系统通过分析用户对各种商品的访问次数来预测用户的偏好。
//
//补充:
//用户会进行 N次商品访问,每次访问某件商品之前!!!,
//系统需要给用户推荐最多不超过 K件商品。
//
//而推荐的这 K件商品,应该是在本次访问之前的所有访问中,
//被访问次数最多的 K件商品。
//
//当然,用户在进行第一次访问时,无法给出任何推荐,因为此时所有商品的被访问次数都为 0。
//
//输入格式
//第一行包含两个正整数: N,表示用户访问商品次数,
//K表示系统提供给用户的最大推荐数。
//
//第二行给出用户正在访问的商品编号。所有商品编号 1∼N。
//
//输出格式
//对于每次访问,以如下格式,输出一行建议:
//
//query : rec[1] rec[2] ... rec[K]
//其中,query 表示用户正在访问的商品,rec[i] 是推荐系统推荐给用户的第 i个商品。
//应当以频率从高到低的顺序推荐访问频率最高的前 K个商品。
//对于访问频率相同的商品,优先推荐商品编号小的。
//注意:对于第一次访问没有任何输出,因为无法给出建议。
//
//数据范围
//1≤N≤50000
//
//1≤K≤10
//输入样例:
//12 3
//3 5 7 5 5 3 2 1 8 3 8 12
//输出样例:
//5: 3
//7 : 3 5
//5 : 3 5 7
//5 : 5 3 7
//3 : 5 3 7
//2 : 5 3 7
//1 : 5 3 2
//8 : 5 3 1
//3 : 5 3 1
//8 : 3 5 1
//12 : 3 5 8#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;
const int N = 50010;//yxc
int n, m;//n是浏览的商品总数,m是每次浏览新商品就推荐m个商品
int cnt[N];//存放当前每个商品的出现次数
int top_k[11];//存放前K个商品,因为题目条件给了k最大为10,所以这里开个大小为11的数组
bool cmp(int x, int y) {if (cnt[x] != cnt[y]) return cnt[x] > cnt[y];//访问次数更多的排在前面else return x < y;//编号小的排前面
}
class Solution {
public:void func() {scanf("%d%d", &n, &m);//数量大有5w,所以用scanfint k = 0;for (int i = 0; i < n; i++) {//读入n个商品,也就是浏览n个商品,同时输入推荐商品int id;//商品编号scanf("%d", &id);//浏览第一个商品时 是不用输出推荐商品的,所以i=0,什么也不做if (i){//先输出idprintf("%d:", id);//再输出推荐的k 个商品for (int j = 0; j < k; j++) {printf(" %d", top_k[j]);}//puts("");//换行符cout << endl;}//输出完推荐的k个商品之后,再将当前商品加入系统//当前商品的访问次数+1cnt[id] ++;//判断下输入的id是否已经存在 在前k个 商品里(遍历一遍),是的话就exists=truebool exists = false;for (int j = 0; j < k; j++) {if (top_k[j] == id) {exists = true;break;}}//如果前k个商品里不存在,则将其加在top_k的末尾的后面一个(还不在前k个里,是第k+1个)if (!exists) {top_k[k++] = id;}//对这k+1个商品进行排序,手写排序/*sort(top_k, top_k + k, [](int x, int y) {if (cnt[x] != cnt[y]) return cnt[x] > cnt[y];return x < y;});*/sort(top_k, top_k + k, cmp);k = min(k, m);//k如果超过m=3,则k还是=3,就是只推荐3个商品(下标0,1,2)就行了}}
};//柳婼
int book[50001];
struct node {int value, cnt;node(int a, int b) :value(a), cnt(b) {}bool operator < (const node& a) const {return (cnt != a.cnt) ? cnt > a.cnt : value < a.value;}
};
class Solution2 {
public:void func() {int n, k, num;scanf("%d%d", &n, &k);set<node> s;for (int i = 0; i < n; i++) {scanf("%d", &num);if (i != 0) {printf("%d:", num);int tempCnt = 0;for (auto it = s.begin(); tempCnt < k && it != s.end(); it++) {printf(" %d", it->value);tempCnt++;}printf("\n");}auto it = s.find(node(num, book[num]));if (it != s.end()) s.erase(it);book[num]++;s.insert(node(num, book[num]));}}
};int main() {Solution s;s.func();return 0;
}

十一模拟十十2 1625. 切整数 PAT甲级真题1132

1625. 切整数

切整数就是将一个有 K位数字的整数 Z 从中间切成两个 K/2位的整数 A和 B。

例如,将 Z=167334 切掉,我们可以得到 A=167 和 B=334。

如果 Z 能被 A 和 B 的乘积整除,那么将非常的有趣,就像 167334/(167×334)=3167334/(167×334)=3 这样。

现在给定整数 Z,请你判断它是否能满足被 A 和 B的乘积整除。

输入格式

第一行包含整数 N,表示共有 N 组测试数据。

接下来 N 行,每行包含一个 Z。

保证 Z 有偶数位。

输出格式

对于每个 Z,输出一行结论,如果能被 A 和 B 的乘积整除,则输出 Yes,否则输出 No

数据范围

1≤N≤20
10≤Z<2^31

输入样例:

3
167334
2333
12345678

输出样例:

Yes
No
No

Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided by the product of A and B, as 167334 / (167 x 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 20). Then N lines follow, each gives an integer Z (10<=Z<=231). It is guaranteed that the number of digits of Z is an even number.

Output Specification:

For each case, print a single line "Yes" if it is such a number, or "No" if not.

Sample Input:

3
167334
2333
12345678

Sample Output:

Yes
No
No

题目大意:给一个偶数个位的正整数num,把它从中间分成左右两个整数a、b,问num能不能被a和b的乘积整除,能的话输出yes,不能的话输出no

分析:要注意a*b如果为0的时候不能取余,否则会浮点错误~

直接用int保存num的值,计算出num的长度len,则令d = pow(10, len / 2)时,num取余d能得到后半部分的整数,num除以d能得到前半部分的整数,计算num % (a*b)是否等于0就可以得知是否可以被整除~

我:

1.soti是string to int,从string到int的转行,若要从int到string转换,用to_string(int)

2.string.substr(0, len / 2));用于切分string,substr(from,to)是前闭后开的区间

//1625. 切整数
//
//切整数就是将一个有 K位数字的整数 Z从中间切成两个 K / 2位的整数 A和 B。
//
//例如,将 Z = 167334切掉,我们可以得到 A = 167和 B = 334。
//
//如果 Z能被 A和 B的乘积整除,那么将非常的有趣,就像 167334 / (167×334) = 3这样。
//
//现在给定整数 Z,请你判断它是否能满足被 A和 B的乘积整除。
//
//输入格式
//第一行包含整数 N,表示共有 N组测试数据。
//
//接下来 N行,每行包含一个 Z。
//
//保证 Z有偶数位。
//
//输出格式对于每个 Z,输出一行结论,如果能被 A和 B的乘积整除,则输出 Yes,否则输出 No。
//
//数据范围
//1≤N≤20
//
//10≤Z < 2^31
//输入样例:
//3
//167334
//2333
//12345678
//输出样例:
//Yes
//No
//No#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;//yxc
class Solution {
public:void func() {int T;cin >> T;while (T--){string number;cin >> number;int len = number.size() / 2;int left = stoi(number.substr(0, len));int right = stoi(number.substr(len));int n = stoi(number);if (left * right && n % (left * right) == 0) puts("Yes");else puts("No");}}
};
//柳婼
class Solution2 {
public:void func() {int n, num;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &num);string s = to_string(num);int len = s.length();int a = stoi(s.substr(0, len / 2));int b = stoi(s.substr(len / 2));if (a * b != 0 && num % (a * b) == 0)printf("Yes\n");elseprintf("No\n");}}
};int main() {Solution s;s.func();return 0;
}

十一模拟十十2 1633. 外观数列 PAT甲级真题1140

1633. 外观数列

外观数列是指具有以下特点的整数序列:

D, D1, D111, D113, D11231, D112213111, ...

其中 D 是一个 [0,9][0,9] 范围内的不等于 11 的整数。

序列的第 n+1 项是对第 n 项的描述。

比如第 2 项表示第 1 项有 1 个 D,所以就是 D1

第 2 项是 1个 D(对应 D1)和 1 个 1(对应 11),所以第 3 项就是 D1

又比如第 4项是 D113,其描述就是 1 个 D,2 个 1,1 个 3,所以下一项就是 D11231

当然这个定义对 D=1 也成立。

本题要求你推算任意给定数字 D 的外观数列的第 N 项。

输入格式

共一行包含两个整数 D和 N。

输出格式

在一行中给出数字 D 的外观数列的第 N 项。

数据范围

0≤D≤9,
1≤N≤40

输入样例:

1 8

输出样例:

1123123111

Look-and-say sequence is a sequence of integers as the following:
D, D1, D111, D113, D11231, D112213111, ...
where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1’s, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.

Input Specification:

Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (<=40), separated by a space.

Output Specification:

Print in a line the Nth number in a look-and-say sequence of D.

Sample Input:

1 8

Sample Output:

1123123111

题目大意:给两个数字D和n,第一个序列是D,后一个序列描述前一个序列的所有数字以及这个数字出现的次数,比如D出现了1次,那么第二个序列就是D1,对于第二个序列D1,第三个序列这样描述:D出现1次,1出现1次,所以是D111……以此类推,输出第n个序列~

分析:用string s接收所需变幻的数字,每次遍历s,从当前位置i开始,看后面有多少个与s[i]相同,设j处开始不相同,那么临时字符串t =t + s[i] + to_string(j - i); 然后再将t赋值给s,cnt只要没达到n次就继续加油循环下一次,最后输出s的值~

我:好好debug一下,搞清每一步

//1633. 外观数列
//
//外观数列是指具有以下特点的整数序列:
//D, D1, D111, D113, D11231, D112213111, ...
//其中 D 是一个[0, 9]范围内的不等于 1的整数。
//
//序列的第 n + 1项是对第 n项的描述。
//
//比如第 2项表示第 1项有 1个 D,所以就是 D1;
//
//第 2项是 1个 D(对应 D1)和 1个 1(对应 11),所以第 3项就是 D111。
//
//又比如第 4项是 D113,其描述就是 1个 D,2个 1,1个 3,所以下一项就是 D11231。
//
//当然这个定义对 D = 1也成立。
//
//本题要求你推算任意给定数字 D 的外观数列的第 N项。
//
//输入格式
//共一行包含两个整数 D和 N。
//
//输出格式
//在一行中给出数字 D的外观数列的第 N项。
//
//数据范围
//0≤D≤9,
//1≤N≤40
//
//输入样例:
//1 8
//
//输出样例:
//1123123111#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;//yxc
class Solution {
public:void func() {int d, n;//输入1,8cin >> d >> n;string cur = to_string(d);for (int k = 0; k < n - 1; k++) {//k<7,从0遍历到6,循环了7次string next;for (int i = 0; i < cur.size();){int j = i + 1;while (j < cur.size() && cur[i] == cur[j]) j++;next += cur[i] + to_string(j - i);i = j;}cur = next;}}
};//柳婼,和yxc思路一样的。
class Solution2 {
public:void func() {string s;int n, j;cin >> s >> n;for (int cnt = 1; cnt < n; cnt++) {string t;for (int i = 0; i < s.length(); i = j) {for (j = i; j < s.length() && s[j] == s[i]; j++);t += s[i] + to_string(j - i);}s = t;}cout << s;}
};int main() {Solution s;s.func();return 0;
}

十一模拟十十4 1640. 堆 PAT甲级真题1147

1640. 堆

在计算机科学中,堆是一种的基于树的专用数据结构,它具有堆属性:

如果 P是 C的父结点,则在大顶堆中 P结点的权值大于或等于 C结点的权值,
在小顶堆中 P结点的权值小于或等于 C结点的权值。

一种堆的常见实现是二叉堆,它是由完全二叉树来实现的。

你的任务是判断给定的完全二叉树是否是堆。

输入格式
第一行包含两个整数 M和 N,分别表示给定完全二叉树的数量以及每个完全二叉树包含的结点数量。

接下来 M行,每行包含 N个不同的整数(都在 int 范围内),表示一个完全二叉树的层序遍历序列。

输出格式
对于每个给定的二叉树,首先输出一行对它是否是堆的判断结论。

如果是大顶堆,则输出 Max Heap,如果是小顶堆,则输出 Min Heap,如果不是堆,则输出 Not Heap。

然后,再输出一行它的后序遍历序列。

同行数字用空格隔开,行首行尾不得有多余空格。

数据范围
1≤M≤100,
1 < N≤1000

输入样例:

3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56

输出样例:

Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))
Your job is to tell if a given complete binary tree is a heap.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (<= 100), the number of trees to be tested; and N (1 < N <= 1000), the number of keys in each tree, respectively. Then M lines follow, each contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.
Output Specification:
For each given tree, print in a line "Max Heap" if it is a max heap, or "Min Heap" for a min heap, or "Not Heap" if it is not a heap at all. Then in the next line print the trees postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.

Sample Input:

3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56

Sample Output:

Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10

题目大意:给一个树的层序遍历,判断它是不是堆,是大顶堆还是小顶堆。输出这个树的后序遍历~

分析:30分大题,25行代码,一行代码一分……((⊙o⊙)嗯) // 我为什么这么机智可爱又伶俐?

从后往前检查所有节点(除了根节点)和它的父节点的关系,判断是否破坏最大堆或者最小堆的性质,如果有不满足的情况将maxn或minn置为0,以此排除最大堆或者最小堆~

然后后序遍历,对于index结点分别遍历孩子index*2和右孩子index*2+1,遍历完左右子树后输出根结点~

我:

//1640. 堆
//
//在计算机科学中,堆是一种的基于树的专用数据结构,它具有堆属性:
//
//如果 P是 C的父结点,则在大顶堆中 P结点的权值大于或等于 C结点的权值,
//在小顶堆中 P结点的权值小于或等于 C结点的权值。
//
//一种堆的常见实现是二叉堆,它是由完全二叉树来实现的。
//
//你的任务是判断给定的完全二叉树是否是堆。
//
//输入格式
//第一行包含两个整数 M和 N,分别表示给定完全二叉树的数量以及每个完全二叉树包含的结点数量。
//
//接下来 M行,每行包含 N个不同的整数(都在 int 范围内),表示一个完全二叉树的层序遍历序列。
//
//输出格式
//对于每个给定的二叉树,首先输出一行对它是否是堆的判断结论。
//
//如果是大顶堆,则输出 Max Heap,如果是小顶堆,则输出 Min Heap,如果不是堆,则输出 Not Heap。
//
//然后,再输出一行它的后序遍历序列。
//
//同行数字用空格隔开,行首行尾不得有多余空格。
//
//数据范围
//1≤M≤100,
//1 < N≤1000
//输入样例:
//3 8
//98 72 86 60 65 12 23 50
//8 38 25 58 52 82 70 60
//10 28 15 12 34 9 8 56
//输出样例:
//Max Heap
//50 60 65 72 12 23 86 98
//Min Heap
//60 58 52 38 82 70 25 8
//Not Heap
//56 12 34 28 9 8 15 10#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include<algorithm>
#include<deque>
#include <math.h>
#include<cstdio>
using namespace std;
const int N = 1010;//yxc,父节点大于子节点,就是大根堆,小于就是小根堆,有大有小就不是堆
int n;//每棵树n个结点
int h[N];//用数组存放堆(完全二叉树)
class Solution {
public://后根遍历void dfs(int u){if (u * 2 <= n) dfs(u * 2);//遍历左子树if (u * 2 + 1 <= n) dfs(u * 2 + 1);//遍历右子树printf("%d", h[u]);if (u != 1) printf(" ");//遍历到堆顶u=1时,后面不输出空格(pat输出末尾无空格)}void func() {int T;//输入T行数据,T个树scanf("%d%d", &T, &n);//每棵树n个结点while (T--){//输入堆的n个结点,用数组保存,注意堆要下标从1开始for (int i = 1; i <= n; i++) scanf("%d", &h[i]);//lt是less than,ture说明出现过小于关系//gt是great than,true说明出现过大于关系bool lt = false, gt = false;//遍历所有结点for (int i = 1; i <= n; i++) {for (int j = 0; j < 2; j++) {//合法结点,父节点有这个孩子时if (i * 2 + j <= n){int a = h[i];//父节点int b = h[i * 2 + j];//j=0时是左孩子,j=1时是右孩子if (a < b) lt = true;//出现过a<b,反正不是大根堆else gt = true;//出现过a>=b,反正不是小根堆}}  }if (lt==true && gt==true) puts("Not Heap");else if (lt==true && gt==false) puts("Min Heap");else if (lt==false && gt==true)puts("Max Heap");dfs(1);//后续遍历输出puts("");//输出换行符}}
};//柳婼
int a[1005], m, n;
class Solution2 {
public:void postOrder(int index) {if (index > n) return;postOrder(index * 2);postOrder(index * 2 + 1);printf("%d%s", a[index], index == 1 ? "\n" : " ");}void func() {scanf("%d %d", &m, &n);while (m--) {int minn = 1, maxn = 1;for (int i = 1; i <= n; i++) scanf("%d", &a[i]);for (int i = 2; i <= n; i++) {if (a[i] > a[i / 2]) maxn = 0;if (a[i] < a[i / 2]) minn = 0;}if (maxn == 1) printf("Max Heap\n");else if (minn == 1) printf("Min Heap\n");else printf("Not Heap\n");postOrder(1);}}
};int main() {Solution s;s.func();return 0;
}

PAT 十一章 模拟 17-24 自用相关推荐

  1. PAT 十一章 模拟 1-16 自用

    十一模拟1 1480. 电梯 PAT甲级真题1008 //1480. 电梯 //我们城市的最高建筑上只有一部电梯. // //给定一个由 N个整数组成的请求列表. // //我们要按照列表指定的顺序, ...

  2. 【正点原子FPGA连载】第十一章PL SYSMON测量输入模拟电压 摘自【正点原子】DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南

    1)实验平台:正点原子MPSoC开发板 2)平台购买地址:https://detail.tmall.com/item.htm?id=692450874670 3)全套实验源码+手册+视频下载地址: h ...

  3. 鸟哥的Linux私房菜(基础篇)- 第十一章、认识与学习 BASH

    第十一章.认识与学习 BASH 最近升级日期:2009/08/25 在 Linux 的环境下,如果你不懂 bash 是什么,那么其他的东西就不用学了!因为前面几章我们使用终端机下达命令的方式,就是透过 ...

  4. 【正点原子Linux连载】第七十一章 Linux 4G通信实验 -摘自【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.0

    1)实验平台:正点原子阿尔法Linux开发板 2)平台购买地址:https://item.taobao.com/item.htm?id=603672744434 2)全套实验源码+手册+视频下载地址: ...

  5. 鸟哥的Linux私房菜(服务器)- 第十一章、远程联机服务器SSH / XDMCP / VNC / RDP

    第十一章.远程联机服务器SSH / XDMCP / VNC / RDP 最近更新日期:2011/11/24 维护网络服务器最简单的方式不是跑去实体服务器前面登入,而是透过远程联机服务器联机功能来登入主 ...

  6. 第十一章、远程联机服务器SSH / XDMCP / VNC / RDP

    维护网络服务器最简单的方式不是跑去实体服务器前面登入,而是透过远程联机服务器联机功能来登入主机, 然后再来进行其他有的没的维护就是了.Linux 主机几乎都会提供 sshd 这个联机服务,而且这个服务 ...

  7. 频谱仪的更改ip_【正点原子FPGA连载】第五十一章 基于FFT IP核的音频频谱仪-摘自【正点原子】开拓者 FPGA 开发指南 (amobbs.com 阿莫电子论坛)...

    本帖最后由 正点原子 于 2020-10-24 15:19 编辑 203429z6c3os33t8albi33.png (66.36 KB) 2019-7-28 15:14 上传 第五十一章 基于FF ...

  8. 鸟哥的Linux私房菜(基础篇)- 第二十一章、系统配置工具(网络与打印机)与硬件侦测

    第二十一章.系统配置工具(网络与打印机)与硬件侦测 最近升级日期:2009/09/15 除了手动配置之外,其实系统提供了一个名为 setup 的命令给系统管理员使用喔!这个命令还能够配置网络呢.此外, ...

  9. 信息系统项目管理师---第十一章项目风险管理历年考题

    信息系统项目管理师-第十一章项目风险管理历年考题 1.2005 年 5 月第 47 题 :在项目风险管理的基本流程中,不包括下列中的(C ). A.风险分析 B.风险追踪 C.风险规避措施 D.风险管 ...

最新文章

  1. 8个试剂,其中一个有毒,最少多少只小白鼠能检测出有毒试剂——分而治之思想...
  2. ajax的数据库,AJAX 数据库
  3. linux cat代码,linux cat命令(示例代码)
  4. python基础入门(Peak带你学python)
  5. 使用Microsoft.AspNetCore.TestHost进行完整的功能测试
  6. Java——集合(输入5个学生的信息按总分高低排序)
  7. 你知道什么是 MySQL 的模糊查询?
  8. JSP 中 pageEncoding 和 charset 区别以及中文乱码解决方案
  9. 图书管理系统软件测试报告_软件测试新手入门小知识点,一定要牢记
  10. Excel VBA 宏编程入门
  11. 微信公众账号api开发
  12. Java 中的 String、StringBuffer、StringBuilder
  13. Android开发者的Ane简单入门
  14. 关于VCT(voxel cone trace——基于体素的锥形光线追踪)Renderer的代码解读
  15. 计算机类博士多少年,弗吉尼亚大学博士几年?
  16. 我看三十而立的80后
  17. [转]我的FLASH情结2010——浅谈FLASH WEB GAME与创业(下)
  18. 从word2vec开始,说下GPT庞大的家族系谱
  19. PyQtGraph库的部分踩坑记录
  20. Android “adb”不是内部或外部命令,也不是可运行的程序或批处理文件

热门文章

  1. 数控车椭圆编程实例带图_数控车床加工椭圆的方法
  2. Android P SELinux (四) CTS neverallow处理总结
  3. des加解密(JavaScriptJava)
  4. Adam优化器简单理解
  5. android 创建aar包
  6. docker安装_使用docker在带有SSL的Nginx反向代理后面部署Quarkus或任何基于Java的微服务...
  7. 后疫情时代2020年后游戏引擎技术会如何发展?
  8. 惠普服务器开机系统密码,惠普(hp)各型号打印机冷复位,清零,回复出厂设置方法 以及 服务菜单(service menu)密码...
  9. 深入分析netty(一)BootStrap与ServerBootStrap
  10. 计算机网络一些相互连接,计算机网络试题全集完整版