第一題 單位變換
題目
【問題描述】
在計算機存儲中,15.125GB是多少MB?
【答案提交】
這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。
思路
1G=1024M
答案
15488
第二題 約數個數
題目
【問題描述】
1200000有多少個約數(只計算正約數)。
【答案提交】
這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。
思路
枚舉+檢查
public class _02 {
public static void main(String[] args) {
int N = 1200000;
int ans = 0;
for (int i = 1; i <= N; ++i) {
if (N % i == 0)
ans++;
}
System.out.println(ans);
}
}
答案
96
第三題 葉結點數
題目
【問題描述】
一棵包含有2019個結點的二叉樹,最多包含多少個葉結點?
【答案提交】
這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。
思路
n=n0+n1+n2,爲使葉子節點數(n0)最多,必須n1最小,設爲0,而n0=n2+1
得n2=(2019-1)/2=1009
所以n0=1010
答案
1010
第四題 數字9
題目
【問題描述】
在1至2019中,有多少個數的數位中包含數字9?
注意,有的數中的數位中包含多個9,這個數只算一次。例如,1999這個數包含數字9,在計算時只是算一個數。
【答案提交】
這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。
思路
僞代碼
for i from 9 to 2019
if str(i).contains('9')
ans++
代碼:
public class _04 {
static final int N = 2019;
static boolean check(int i) {
String str = "" + i;
return str.indexOf('9') != -1;
}
public static void main(String[] args) {
int ans = 0;
for (int i = 9; i <= N; ++i) {
if (check(i)) {
ans++;
}
}
System.out.println(ans);
}
}
答案
544
第五題 數位遞增的數
題目
【問題描述】
一個正整數如果任何一個數位不大於右邊相鄰的數位,則稱爲一個數位遞增的數,例如1135是一個數位遞增的數,而1024不是一個數位遞增的數。
給定正整數 n,請問在整數 1 至 n 中有多少個數位遞增的數?
【輸入格式】
輸入的第一行包含一個整數 n。
【輸出格式】
輸出一行包含一個整數,表示答案。
【樣例輸入】
30
【樣例輸出】
26
【評測用例規模與約定】
對於 40% 的評測用例,1 <= n <= 1000。
對於 80% 的評測用例,1 <= n <= 100000。
對於所有評測用例,1 <= n <= 1000000。
思路 O(kN)
迭代1~n
轉換爲字符串
迭代字符串的每一位,判斷是否滿足要求
參考代碼
import java.util.Scanner;
public class _05 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true)//死循環爲測試用
work();
}
static void work() {
int n = sc.nextInt();
int ans = 0;
for (int i = 1; i <= n; ++i) {
String s = "" + i;
boolean flag = true;
for (int j = 1; j < s.length(); ++j) {
if (s.charAt(j - 1) > s.charAt(j)) {
flag = false;
break;
}
}
if (flag)
ans++;
}
System.out.println(ans);
}
}
第六題 遞增三元組
題目
【問題描述】
在數列 a[1], a[2], …, a[n] 中,如果對於下標 i, j, k 滿足 0<i<j<k<n+1 且 a[i]<a[j]<a[k],則稱 a[i], a[j], a[k] 爲一組遞增三元組,a[j]爲遞增三元組的中心。
給定一個數列,請問數列中有多少個元素可能是遞增三元組的中心。
【輸入格式】
輸入的第一行包含一個整數 n。
第二行包含 n 個整數 a[1], a[2], …, a[n],相鄰的整數間用空格分隔,表示給定的數列。
【輸出格式】
輸出一行包含一個整數,表示答案。
【樣例輸入】
5
1 2 5 3 5
【樣例輸出】
2
【樣例說明】
a[2] 和 a[4] 可能是三元組的中心。
【評測用例規模與約定】
對於 50% 的評測用例,2 <= n <= 100,0 <= 數列中的數 <= 1000。
對於所有評測用例,2 <= n <= 1000,0 <= 數列中的數 <= 10000。
思路 O(N^2)
枚舉每個元素
該元素與前面的元素比較,找到小的即可
該元素與後面的元素比較,找到大的即可
上面兩項爲真,即說明當前元素可以作爲三元組的中心
O(N^2),因爲N最大爲1000,所以1秒內可以解決戰鬥。
參考代碼
import java.util.Scanner;
public class _06 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true)//死循環爲測試用
work();
}
static void work() {
int n, ans = 0;
n = sc.nextInt();
int[] data = new int[n];
for (int i = 0; i < n; ++i) {
data[i] = sc.nextInt();
}
for (int j = 1; j < n - 1; ++j) {
int hasSmall = 0;
for (int i = 0; i < j; ++i) {
if (data[i] < data[j]) {
hasSmall = 1;
break;
}
}
int hasBig = 0;
for (int k = j + 1; k < n; ++k) {
if (data[j] < data[k]) {
hasBig = 1;
break;
}
}
if ((hasSmall & hasBig) == 1)
ans++;
}
System.out.println(ans);
}
}
第七題 音節判斷
題目
【問題描述】
小明對類似於 hello 這種單詞非常感興趣,這種單詞可以正好分爲四段,第一段由一個或多個輔音字母組成,第二段由一個或多個元音字母組成,第三段由一個或多個輔音字母組成,第四段由一個或多個元音字母組成。
給定一個單詞,請判斷這個單詞是否也是這種單詞,如果是請輸出yes,否則請輸出no。
元音字母包括 a, e, i, o, u,共五個,其他均爲輔音字母。
【輸入格式】
輸入一行,包含一個單詞,單詞中只包含小寫英文字母。
【輸出格式】
輸出答案,或者爲yes,或者爲no。
【樣例輸入】
lanqiao
【樣例輸出】
yes
【樣例輸入】
world
【樣例輸出】
no
【評測用例規模與約定】
對於所有評測用例,單詞中的字母個數不超過100。
思路1
正則表達式
參考代碼1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class _07 {
static Pattern p = Pattern.compile("[^aeiou]+[aeiou]+[^aeiou]+[aeiou]+");
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) { //死循環是爲了測試
if (work())
System.out.println("yes");
else
System.out.println("no");
}
}
static boolean work() {
String word = sc.nextLine();
Matcher m = p.matcher(word);
return m.matches();
}
}
思路2
將單詞的輔音標記爲0,元音標記爲1,那麼符合要求的單詞形態爲0+ 1+ 0+ 1
在限定開頭必須爲0,最後必須爲1的情況下,01交替爲3次,即前後元素相加結果爲1的次數必須爲3
參考代碼2
import java.util.Scanner;
public class _07_2 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) { //死循環是爲了測試
if (work())
System.out.println("yes");
else
System.out.println("no");
}
}
static boolean isYuan(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
static boolean work() {
String word = sc.nextLine();
int size = word.length();
if (size < 4)
return false;
if (isYuan(word.charAt(0)) || !isYuan(word.charAt(size - 1)))
return false;
int[] h = new int[size];
for (int i = 0; i < size; ++i) {
if (isYuan(word.charAt(i)))
h[i] = 1;
else
h[i] = 0;
}
int cnt = 0;
for (int i = 1; i < size; ++i) {
if (h[i - 1] + h[i] == 1) cnt++;
}
return cnt == 3;
}
}
第八題 長草
題目
【問題描述】
小明有一塊空地,他將這塊空地劃分爲 n 行 m 列的小塊,每行和每列的長度都爲 1。
小明選了其中的一些小塊空地,種上了草,其他小塊仍然保持是空地。
這些草長得很快,每個月,草都會向外長出一些,如果一個小塊種了草,則它將向自己的上、下、左、右四小塊空地擴展,這四小塊空地都將變爲有草的小塊。
請告訴小明,k 個月後空地上哪些地方有草。
【輸入格式】
輸入的第一行包含兩個整數 n, m。
接下來 n 行,每行包含 m 個字母,表示初始的空地狀態,字母之間沒有空格。如果爲小數點,表示爲空地,如果字母爲 g,表示種了草。
接下來包含一個整數 k。
【輸出格式】
輸出 n 行,每行包含 m 個字母,表示 k 個月後空地的狀態。如果爲小數點,表示爲空地,如果字母爲 g,表示長了草。
【樣例輸入】
4 5
.g...
.....
..g..
.....
2
【樣例輸出】
gggg.
gggg.
ggggg
.ggg.
【評測用例規模與約定】
對於 30% 的評測用例,2 <= n, m <= 20。
對於 70% 的評測用例,2 <= n, m <= 100。
對於所有評測用例,2 <= n, m <= 1000,1 <= k <= 1000。
思路 O(N*M)
典型的bfs,基本是個模板題。時間複雜度最多爲O(N*M)。
參考代碼
import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;
public class _08 {
static final int[] dx = {1, 0, -1, 0};
static final int[] dy = {0, 1, 0, -1};
static Scanner sc ;
static int[][] vis = new int[1000][1000];
static int N, M, K;
public static void main(String[] args) throws IOException {
long now = System.currentTimeMillis();
//System.setIn(new FileInputStream(new File("***.in")));
//System.setOut(new PrintStream(new File("***.out")));
sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
sc.nextLine();
LinkedList<Block> q = new LinkedList<Block>();
for (int i = 0; i < N; i++) {
String line = sc.nextLine();
for (int j = 0; j < M; j++) {
if (line.charAt(j) == 'g') {
q.addLast(new Block(i, j, 0));
vis[i][j] = 1;
}
}
}
K = sc.nextInt();
while (!q.isEmpty()) {
Block b = q.removeFirst();
int month = b.month;
if (month < K) {
for (int i = 0; i <= 3; i++) {
int nx = b.i + dx[i];
int ny = b.j + dy[i];
if (0 <= nx && nx < N && 0 <= ny && ny < M && vis[nx][ny] == 0) {
vis[nx][ny] = 1;
q.addLast(new Block(nx, ny, month + 1));
}
}
}
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (vis[i][j] == 1) writer.write('g');
else writer.write('.');
}
writer.write('\n');
}
writer.flush();
System.err.println(System.currentTimeMillis()-now);
}
//草地上的一塊
private static class Block {
int i;
int j;
int month;
public Block(int i, int j, int month) {
this.i = i;
this.j = j;
this.month = month;
}
}
}
第九題 序列計數
題目
【問題描述】
小明想知道,滿足以下條件的正整數序列的數量:
1. 第一項爲 n;
2. 第二項不超過 n;
3. 從第三項開始,每一項小於前兩項的差的絕對值。
請計算,對於給定的 n,有多少種滿足條件的序列。
【輸入格式】
輸入一行包含一個整數 n。
【輸出格式】
輸出一個整數,表示答案。答案可能很大,請輸出答案除以10000的餘數。
【樣例輸入】
4
【樣例輸出】
7
【樣例說明】
以下是滿足條件的序列:
4 1
4 1 1
4 1 2
4 2
4 2 1
4 3
4 4
【評測用例規模與約定】
對於 20% 的評測用例,1 <= n <= 5;
對於 50% 的評測用例,1 <= n <= 10;
對於 80% 的評測用例,1 <= n <= 100;
對於所有評測用例,1 <= n <= 1000。
思路:記憶型遞歸 O(N^3)
題幹第三點,是一個遞歸定義,可以得到遞歸式:
f(pre,cur) = f(cur,1) + f(cur,2) + ... +f(cur,abs(pre-cur)-1) + 1
pre表示前一個數,cur代表當前的數,選定之後,序列種數等於以cur爲前序,以1到abs-1爲當前的序列數的總和再加1.
如f(5,2) = f(2,1)+f(2,2).
但是暴力遞歸的複雜度是指數級;
基本的優化方案是加狀態記憶:輸入1000時,實測運行時間爲1000~2000ms;
參考代碼:記憶型遞歸
import java.util.Scanner;
public class _09_01 {
static final int MOD = 10000;
static int N;
static long ans;
static long[][] mem = new long[1001][1000];
static Scanner sc;
static long dfs(int pre, int cur) {
// 詢問狀態
if (mem[pre][cur] != 0)
return mem[pre][cur];
long ans = 1;
for (int j = 1; j < Math.abs(pre - cur); j++) {
ans = (ans + dfs(cur, j)) % MOD;
}
mem[pre][cur] = ans;
return ans;
}
static void work() {
ans = 0;
N = sc.nextInt();
long ago = System.currentTimeMillis();
// f(pre,cur) = sum(f(cur,_new))|_new from 1 to abs(pre-cur)-1
for (int x = 1; x <= N; ++x) ans = (ans + dfs(N, x)) % MOD;
System.out.println(ans);
System.err.println(System.currentTimeMillis() - ago);
}
public static void main(String[] args) {
sc = new Scanner(System.in);
while (true) {
work();
}
}
}
進一步優化
至此,能通過80%的數據(在1000ms限制下);
解空間是N的平方(詳細爲N*N)表格,但是每次都要循環加總,所以成了N的立方,在同樣的解空間下,避免循環加總,即可優化到N的平方
重新考慮狀態的轉移:
如果我們用f(i,j)表示前一個數是i,當前數是1到j的合法序列的個數;有f(i,j) = 1 + f(i,j-1) + f(j,abs(i-j)-1)即分爲兩個部分1)i作爲前一個數,從1到j-1爲當前數的合法序列的個數已經計算好,2)求以j爲尾數,後面選擇1到abs(i-j)-1的合法序列的個數。
如 f(10,5)=f(10,4)+f(5,4);而不是枚舉1到5;這樣每次解答樹只展開兩個節點,相當於減少一層循環,雖然解答樹的層次還是很深,但是由於記憶的存在,解空間仍然是N的平方。可在100ms內解決。
參考代碼:
import java.util.Scanner;
public class _09_02 {
static final int MOD = 10000;
static int N;
static long ans;
static long[][] mem = new long[1001][1000];
static Scanner sc;
static long dfs(int pre, int cur) {
if (cur <= 0) return 0;
// 詢問狀態
if (mem[pre][cur] != 0)
return mem[pre][cur];
mem[pre][cur] = (1 + dfs(pre, cur - 1) + dfs(cur, Math.abs(pre - cur) - 1)) % MOD;
return mem[pre][cur];
}
static void work() {
ans = 0;
N = sc.nextInt();
long ago = System.currentTimeMillis();
System.out.println(dfs(N, N));
System.err.println(System.currentTimeMillis() - ago);
}
public static void main(String[] args) {
sc = new Scanner(System.in);
while (true) {
work();
}
}
}
第十題 晚會節目單
題目
【問題描述】
小明要組織一臺晚會,總共準備了 n 個節目。然後晚會的時間有限,他只能最終選擇其中的 m 個節目。
這 n 個節目是按照小明設想的順序給定的,順序不能改變。
小明發現,觀衆對於晚會的喜歡程度與前幾個節目的好看程度有非常大的關係,他希望選出的第一個節目儘可能好看,在此前提下希望第二個節目儘可能好看,依次類推。
小明給每個節目定義了一個好看值,請你幫助小明選擇出 m 個節目,滿足他的要求。
【輸入格式】
輸入的第一行包含兩個整數 n, m ,表示節目的數量和要選擇的數量。
第二行包含 n 個整數,依次爲每個節目的好看值。
【輸出格式】
輸出一行包含 m 個整數,爲選出的節目的好看值。
【樣例輸入】
5 3
3 1 2 5 4
【樣例輸出】
3 5 4
【樣例說明】
選擇了第1, 4, 5個節目。
【評測用例規模與約定】
對於 30% 的評測用例,1 <= n <= 20;
對於 60% 的評測用例,1 <= n <= 100;
對於所有評測用例,1 <= n <= 100000,0 <= 節目的好看值 <= 100000。
錯誤思路
如果用兩次排序求解,那就錯了。因爲並不是要選出的方案的好看值總和最大,而是要從前往後儘量好看。即選出的M個數字典序最大
思路 O(N^2)
此題關鍵在於“第一個節目儘可能好看”並希望“第二個節目儘可能好看”……那麼我們選擇的第一節目就是max(g[0]~g[n-m])閉區間,要選擇的第二個節目是max(g[lastMax+1],g[n-m+1])及從上一個節目往下到n-m+1這個區間裏面選最好看的,直到剩下的必須全部選擇。
算法用尺取法,雙指針移動。理論上的複雜度是O(M*(N-M)),極端情況是M=N/2,整體達到(N^2)/2。如果輸入數據爲:
100000 50000
100000 99999 ...
實測運行時間爲:10秒以上
參考代碼
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class _10_1 {
public static Scanner sc = new Scanner(System.in);
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int N, M;
public static void main(String[] args) throws IOException {
N = sc.nextInt();
M = sc.nextInt();
int[] games = new int[N];
for (int i = 0; i < N; i++) {
games[i] = sc.nextInt();
}
int pos_max = 0, pos_1 = 0, pos_2 = N - M;
while (pos_1 < pos_2 && pos_2 < N) {
while (pos_1 < pos_2)
if (games[++pos_1] > games[pos_max]) pos_max = pos_1;
bw.write(games[pos_max] + " ");
pos_1 = pos_max + 1;
pos_2++;
pos_max = pos_1;
}
while (pos_2 != N) {
bw.write(games[pos_2++] + " ");
}
bw.write('\n');
bw.flush();
}
}
優化:區間最值查詢 O(NlogN)
while (pos_1 < pos_2)
if (games[++pos_1] > games[pos_max])pos_max = pos_1;
這一段代碼是區間內查詢最大值,反覆多次,且數據是靜態的,所以選擇ST做RMQ。
f[i][j]表示以 i 爲起點,連續 2^j 個數中的最大值(的下標);
轉移方程就是:f[i][j] = data[f[i][j-1]] >= data[f[i+pow_2(j-1)][j-1]]?f[i][j-1]:f[i+pow_2(j-1)][j-1];
注:比較原始數據,記錄下標
由於預處理是O(nlogn),M次查詢是O(M),每次查詢是O(1),所以整體複雜度爲O(nlogn)。
下列代碼實測運行時間100ms以內
參考代碼
import java.io.*;
import java.util.Scanner;
public class _10_2 {
public static Scanner sc;
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int N, M;
private static int[] data;
/*===st rmq begin===*/
private static int[][] st;
private static int[] Log;
private static int pow_2(int x) {
return 1 << x;
}
private static void initLog() {
Log = new int[N + 1];
Log[1] = 0;
for (int i = 2; i <= N; i++) {
Log[i] = Log[i / 2] + 1;
}
}
private static void initSt() {
st = new int[N][Log[N]+1];
for (int i = 0; i < N; i++) {
st[i][0] = i;//注意此處記錄索引
}
for (int j = 1; pow_2(j) < N; j++) {
for (int i = 0; i + pow_2(j - 1) < N; i++) {
int index1 = st[i][j - 1];
int index2 = st[i + pow_2(j - 1)][j - 1];
st[i][j] = data[index1] >= data[index2] ? index1 : index2;
}
}
}
private static int query(int l, int r) {
int len = r - l + 1;
int k = Log[len];
int index1 = st[l][k];
int index2 = st[r - pow_2(k) + 1][k];
return data[index1] >= data[index2] ? index1 : index2;
}
/*===st rmq end===*/
public static void main(String[] args) throws IOException {
long ago = System.currentTimeMillis();
System.setIn(new FileInputStream(new File("E:\\data\\my10_1.in")));
sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
data = new int[N];
for (int i = 0; i < N; i++) {
data[i] = sc.nextInt();
}
// 初始化st數據
initLog();
initSt();
int pos_max = 0, pos_1 = 0, pos_2 = N - M;
while (pos_1 < pos_2 && pos_2 < N) {
// while (pos_1 < pos_2)
// if (data[++pos_1] > data[pos_max]) pos_max = pos_1;
pos_max = query(pos_1, pos_2);
bw.write(data[pos_max] + " ");
pos_1 = pos_max + 1;
pos_2++;
// pos_max = pos_1;
}
while (pos_2 != N) {
bw.write(data[pos_2++] + " ");
}
bw.write('\n');
bw.flush();
System.err.println(System.currentTimeMillis() - ago);
}
}