TUNIVERSE

PAT算法杂烩笔记

字数统计: 698阅读时长: 4 min
2023/09/02

算法笔记第四章习题与笔记汇总

活用递推

[1093] Count PAT’s

题目

The string APPAPT contains two PAT‘s as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.

Now given any string, you are supposed to tell the number of PAT‘s contained in the string.

Input Specification:

Each input file contains one test case. For each case, there is only one line giving a string of no more than $10^5$ characters containing only P, A, or T.

Output Specification:

For each test case, print in one line the number of PAT‘s contained in the string. Since the result may be a huge number, you only have to output the result moded by 1000000007.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
const int d = 1000000007;
vector<int> leftNum;

int main() {
string str;
cin >> str;
int len = str.size(), ans = 0, rightNum = 0;
leftNum.resize(len, 0);

for (int i = 0; i < len; i++) {
if (i > 0) leftNum[i] = leftNum[i-1];
if (str[i] == 'P') leftNum[i]++;
}
for (int i = len - 1; i >= 0; i--) {
if (str[i] == 'T') rightNum++;
else if (str[i] == 'A')
ans = (ans + rightNum * leftNum[i]) % d;
}
cout << ans;
return 0;
}

[1101] Quick Sort

题目

There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?

For example, given $N=5$ and the numbers 1, 3, 2, 4, and 5. We have:

  • 1 could be the pivot since there is no element to its left and all the elements to its right are larger than it;
  • 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well;
  • 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well;
  • and for the similar reason, 4 and 5 could also be the pivot.
    Hence in total there are 3 pivot candidates.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer $N (≤10^5)$. Then the next line contains N distinct positive integers no larger than $10^9$. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
using namespace std;
vector<int> num, l, r;
set<int> ans;

int main() {
int n;
cin >> n;
num.resize(n);
l.resize(n);
r.resize(n);
l[0] = 0;
for (int i = 0; i < n; i++) {
cin >> num[i];
l[i] = max(l[i-1], num[i-1]);
}
r[n-1] = INT_MAX;
for (int i = n - 2; i >= 0; i--)
r[i] = min(r[i+1], num[i+1]);
for (int i = 0; i < n; i++)
if (l[i] < num[i] && r[i] > num[i])
ans.insert(num[i]);
cout << ans.size() << endl;
for (auto it = ans.begin(); it != ans.end(); it++) {
if (it == ans.begin()) cout << *(it);
else cout << " " << *(it);
}
cout << endl;
return 0;
}
CATALOG
  1. 1. 活用递推
    1. 1.1. [1093] Count PAT’s
      1. 1.1.1. 题目
        1. 1.1.1.1. Input Specification:
        2. 1.1.1.2. Output Specification:
      2. 1.1.2. 代码
    2. 1.2. [1101] Quick Sort
      1. 1.2.1. 题目
        1. 1.2.1.1. Input Specification:
        2. 1.2.1.2. Output Specification:
      2. 1.2.2. 代码