2018.3.18 PAT甲級比賽
寫個部落格,紀念一下今天的比賽吧。。。
A題
1140. Look-and-say Sequence (20)
時間限制 400 ms記憶體限制 65536 kB
程式碼長度限制 16000 B
判題程式 Standard 作者 CHEN, Yue
Look-and-say sequence
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 8Sample Output:
1123123111
思路
這題理解題意就好了
其中第n個序列是用來描述第n-1個序列的,如D113,下一個就是D11231,就是按順序來D一個,1兩個,3一個,組成D11231
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const double eps = 1e-8;
char seq[MAXN], seq2[MAXN];
int main() {
//freopen("/1.txt", "r", stdin);
int D, n, j, cnt, t;
int k1, k2; //兩個序列的長度
scanf("%d%d", &D, &n);
k1 = 0;
seq[k1++] = D;
seq[k1++] = 0;
for(int i = 1; i < n; ++i){
//轉換n-1次,找出第n個序列
k2 = j = 0;
cnt = 1;
while(j < k1 - 1) {
t = seq[j++];
cnt = 1; //數字t出現的次數
while(j < k1 - 1 && t == seq[j])
++j, ++cnt;
seq2[k2++] = t;
seq2[k2++] = cnt;
}
seq2[k2++] = 0;
strcpy(seq, seq2);
k1 = k2;
}
for(int i = 0; i < k1 - 1; ++i)
printf("%c", seq[i] + '0');
return 0;
}
B題
1141. PAT Ranking of Institutions (25)
時間限制 500 ms記憶體限制 65536 kB
程式碼長度限制 16000 B
判題程式 Standard 作者 CHEN, Yue
After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
ID Score School
where "ID" is a string of 6 characters with the first one representing the test level: "B" stands for the basic level, "A" the advanced level and "T" the top level; "Score" is an integer in [0, 100]; and "School" is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that "ID" is unique for each testee.
Output Specification:
For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
Rank School TWS Ns
where "Rank" is the rank (start from 1) of the institution; "School" is the institution code (all in lower case); "TWS" is the total weighted score which is defined to be the integer part of "ScoreB/1.5 + ScoreA + ScoreT*1.5", where "ScoreX" is the total score of the testees belong to this institution on level X; and "Ns" is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.
Sample Input:10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypuSample Output:
5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2
思路
這題emmmm...最後兩個測試用例超時了,最後才想到用Map存,以學校名為鍵值,第一次用Map,想不到會是在這樣的場景現學的....
設計好合適的結構體存就好了
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const double eps = 1e-8;
char seq[MAXN], seq2[MAXN];
struct School{
char name[10];
int cnt; //參與比賽的人數
double score; //總分數
int scoreInt;
School(char a[], int b, double c, int d) : cnt(b), score(c), scoreInt(d) {
strcpy(name, a);
}
bool operator < (School s) {
if(scoreInt == s.scoreInt) {
if(cnt == s.cnt)
return strcmp(name, s.name) < 0;
return cnt < s.cnt;
}
return scoreInt > s.scoreInt;
}
};
int main() {
//freopen("/1.txt", "r", stdin);
int n, k, t;
double score;
char ID[10], name[10];
vector<School> vec;
map<string, int> mm; //mm存學校名對應的下標
string str;
scanf("%d", &n);
while(n--) {
scanf("%s%lf%s", ID, &score, name);
if(ID[0] == 'T')
score *= 1.5;
else if(ID[0] == 'B')
score /= 1.5;
for(int i = 0; name[i]; ++i)
if(name[i] < 'a')
name[i] += 32;
str = name;
if(mm.find(str) == mm.end()) {
//新學校
mm[str] = vec.size();
//加上eps,防止出現數字1存成0.999999,int轉換一下變成0的情況
vec.push_back(School(name, 1, score, (int)(score + eps)));
} else {
t = mm[str];
++vec[t].cnt;
vec[t].score += score;
vec[t].scoreInt = (int)(vec[t].score + eps);
}
}
sort(vec.begin(), vec.end());
printf("%d\n", vec.size());
k = 0;
while(k < vec.size()) {
int Rank = k + 1;
t = vec[k].scoreInt;
while(k < vec.size() && vec[k].scoreInt == t) {
printf("%d %s %d %d\n", Rank, vec[k].name, vec[k].scoreInt, vec[k].cnt);
++k;
}
}
return 0;
}
C題
1142. Maximal Clique (25)
時間限制 400 ms記憶體限制 65536 kB
程式碼長度限制 16000 B
判題程式 Standard 作者 CHEN, Yue
A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. A maximal clique is a clique that cannot be extended by including one more adjacent vertex. (Quoted from https://en.wikipedia.org/wiki/Clique_(graph_theory))
Now it is your job to judge if a given subset of vertices can form a maximal clique.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers Nv (<= 200), the number of vertices in the graph, and Ne, the number of undirected edges. Then Ne lines follow, each gives a pair of vertices of an edge. The vertices are numbered from 1 to Nv.
After the graph, there is another positive integer M (<= 100). Then M lines of query follow, each first gives a positive number K (<= Nv), then followed by a sequence of K distinct vertices. All the numbers in a line are separated by a space.
Output Specification:
For each of the M queries, print in a line "Yes" if the given subset of vertices can form a maximal clique; or if it is a clique but not a maximal clique, print "Not Maximal"; or if it is not a clique at all, print "Not a Clique".
Sample Input:8 10 5 6 7 8 6 4 3 6 4 5 2 3 8 2 2 7 5 3 3 4 6 4 5 4 3 6 3 2 8 7 2 2 3 1 1 3 4 3 6 3 3 2 1Sample Output:
Yes Yes Yes Yes Not Maximal Not a Clique
思路
這個題Nc最大才200,所以可以暴力過
對新輸入的k個點,判斷其是否可以構成cilque,
先判斷這k個點是否相互鄰接,不鄰接輸出 "Not a Clique"
若相互鄰接,再尋找是否至少存在一個點(不在這k個點內),使得他與其他點都鄰接,若存在輸出"Not Maximal"
不存在輸出"Yes"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 205;
const double eps = 1e-8;
char seq[MAXN], seq2[MAXN];
bool G[MAXN][MAXN];
int Nv;
//尋找是否至少存在一個點使得其與k個連相鄰接
bool found(int a[], int k, bool tag[]) {
for(int i = 1; i <= Nv; ++i)
if(!tag[i]) {
bool flag = true;
for(int j = 0; j < k; ++j) {
if(!G[i][a[j]]) {
flag = false;
break;
}
}
if(flag)
return true;
}
return false;
}
int main() {
//freopen("/1.txt", "r", stdin);
int Ne, m, u, v, k;
int a[MAXN];
bool tag[MAXN];
scanf("%d%d", &Nv, &Ne);
while(Ne--) {
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = 1;
}
scanf("%d", &m);
while(m--) {
scanf("%d", &k);
bool flag = true;
memset(tag, 0, sizeof(tag));
for(int i = 0; i < k; ++i) {
scanf("%d", &a[i]);
tag[a[i]] = true; //標記這k個點
for(int j = 0; j < i; ++j)
if(!G[a[i]][a[j]]) { //存在兩個不連通的點,咔掉
flag = false;
break;
}
}
if(!flag) {
puts("Not a Clique");
continue;
}
//可以構成一個clique,再判斷能否找到更大的clique
if(found(a, k, tag))
puts("Not Maximal");
else
puts("Yes");
}
return 0;
}
D題
1143. Lowest Common Ancestor (30)
時間限制 200 ms記憶體限制 65536 kB
程式碼長度限制 16000 B
判題程式 Standard 作者 CHEN, Yue
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
A binary search tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
Given any two nodes in a BST, you are supposed to find their LCA.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
Output Specification:
For each given pair of U and V, print in a line "LCA of U and V is A." if the LCA is found and A is the key. But if A is one of U and V, print "X is an ancestor of Y." where X is A and Y is the other node. If U or V is not found in the BST, print in a line "ERROR: U is not found." or "ERROR: V is not found." or "ERROR: U and V are not found.".
Sample Input:6 8 6 3 1 2 5 4 8 7 2 5 8 7 1 9 12 -3 0 8 99 99Sample Output:
LCA of 2 and 5 is 3. 8 is an ancestor of 7. ERROR: 9 is not found. ERROR: 12 and -3 are not found. ERROR: 0 is not found. ERROR: 99 and 99 are not found.
思路
給出先序的二叉搜尋樹,查詢兩個點的公共祖先
建一個結構體,先寫個先序遍歷,把結構體節點都補充完整,-1表示沒有孩子節點
對兩個輸入的數都進行從根節點到葉子節點的查詢(找不到用-1表示,ui,vi),查詢時第一個數時存下路徑上的節點,查詢第二個時比較路徑上的節點與第一個路徑上點是否相同,相同即為公共祖先,祖先向下查詢更新,最新的公共祖先為最近的公共祖先
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
const double eps = 1e-8;
struct Node{
int l, r;
int val;
Node() : l(-1), r(-1) {};
}node[MAXN];
void preOrder(int l, int r) {
if(l <= r) {
//二叉搜尋樹,第一個點是根節點
int l2 = r + 1; //若有右子樹,l2小於等於r
for(int i = l + 1; i <= r; ++i)
if(node[l].val < node[i].val) {
l2 = i;
break;
}
if((l2 <= r && l < l2 - 1) || (l2 > r) && (l < r))
node[l].l = l + 1;
if(l2 <= r)
node[l].r = l2;
if(l2 <= r)
preOrder(l + 1, l2 - 1);
else
preOrder(l + 1, r);
preOrder(l2, r);
}
}
int main() {
//freopen("/1.txt", "r", stdin);
int m, n, u, v, ui, vi, anc;
vector<int> vec;
scanf("%d%d", &m, &n);
for(int i = 0; i < n; ++i)
scanf("%d", &node[i].val);
preOrder(0, n - 1);
// for(int i = 0; i < n; ++i)
// printf("%d %d %d\n", node[i].val, node[i].l, node[i].r);
while(m--) {
scanf("%d%d", &u, &v);
vec.clear();
ui = vi = 0;
while(true) {
vec.push_back(node[ui].val); //將尋找u的途中,經過的節點值都儲存起來
if(u == node[ui].val)
break;
if(u < node[ui].val)
ui = node[ui].l;
else
ui = node[ui].r;
if(ui == -1)
break;
}
int k = 0;
while(true) {
//k可以理解成層數,若是u,v的共同祖先,它一定在同一層上;不斷更新anc,能找到u,v的最近的祖先
if(k < vec.size() && node[vi].val == vec[k])
anc = vec[k];
++k;
if(v == node[vi].val)
break;
if(v < node[vi].val)
vi = node[vi].l;
else
vi = node[vi].r;
if(vi == -1)
break;
}
if(ui > -1 && vi > -1) {
if(anc == u)
printf("%d is an ancestor of %d.\n", anc, v);
else if(anc == v)
printf("%d is an ancestor of %d.\n", anc, u);
else
printf("LCA of %d and %d is %d.\n", u, v, anc);
}
else {
if(ui == -1 && vi == -1)
printf("ERROR: %d and %d are not found.\n", u, v);
else if(ui == -1)
printf("ERROR: %d is not found.\n", u);
else
printf("ERROR: %d is not found.\n", v);
}
}
return 0;
}