前言
整体评价
A. 大小写转换
Q: 把字符串s统一成小写字母形态
题型:签到
知识点: 考察字符串的API题
c++可以借助transform函数,进行转化
#include <bits/stdc++.h>using namespace std;int main() {string s;cin >> s;// 把自己转化为小写/大写态// tolower/touppertransform(s.begin(), s.end(), s.begin(), ::tolower);cout << s << endl;return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;public class Main {public static void main(String[] args) {AReader sc = new AReader();String s = sc.next();System.out.println(s.toLowerCase());}staticclass AReader {private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));private StringTokenizer tokenizer = new StringTokenizer("");private String innerNextLine() {try {return reader.readLine();} catch (IOException ex) {return null;}}public boolean hasNext() {while (!tokenizer.hasMoreTokens()) {String nextLine = innerNextLine();if (nextLine == null) {return false;}tokenizer = new StringTokenizer(nextLine);}return true;}public String nextLine() {tokenizer = new StringTokenizer("");return innerNextLine();}public String next() {hasNext();return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}// public BigInteger nextBigInt() {
// return new BigInteger(next());
// }// 若需要nextDouble等方法,请自行调用Double.parseDouble包装}}
B. 不合群数
Q: 不能被 [2, a]范围内的数整除的数,被称为不合群数, 求[2, b]内最大的不合群数
思路: 枚举
核心就是,一个前置的知识点:
10 亿内的最大质数是 999999937 ,且相邻质数之间的差值均不超过 300 10 亿内的最大质数是 999999937,且相邻质数之间的差值均不超过 300 10亿内的最大质数是999999937,且相邻质数之间的差值均不超过300
因为大于a的质数,一定是不合群数,而每300数,必然有一个质数存在。因此找到这个质数即可。
从b开始,从大到小进行枚举,判定即可。
当然不见得质数才满足需求
比如
input
5 50output
49
# 因为49=7*7, 其不被[2, 5]区间的数整除
但是质数,给了枚举范围的上限值,所以这是一道很有趣的题。
#include <bits/stdc++.h>using namespace std;using int64 = int64_t;// 判定质数, O(sqrt(n))
bool judge(int64 v, int64 a) {if (v <= a) return false;for (int64 i = 2; i <= v / i && i <= a; i++) {if (v % i == 0) {return false;}}return true;
}int main() {int64 a, b;cin >> a >> b;int64 res = -1;for (int64 i = b; i >= max(b - 300, a + 1); i--) {if (judge(i, a)) {res = i;break;}}cout << res << endl;return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;public class Main {static boolean isPrime(long v) {if (v <= 1) return false;for (int i = 2; i <= v / i; i++) {if (v % i == 0) return false;}return true;}public static void main(String[] args) {AReader sc = new AReader();long a = sc.nextLong(), b = sc.nextLong();long res = -1;for (long i = b; i > a && i >= b - 300; i--) {// a^2if (isPrime(i)) {res = i;break;}if (i >= a * a) {boolean ok = true;for (int j = 2; j <= a; j++) {if (i % j == 0) {ok = false;}}if (ok) {res = i;break;}}}System.out.println(res);}staticclass AReader {private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));private StringTokenizer tokenizer = new StringTokenizer("");private String innerNextLine() {try {return reader.readLine();} catch (IOException ex) {return null;}}public boolean hasNext() {while (!tokenizer.hasMoreTokens()) {String nextLine = innerNextLine();if (nextLine == null) {return false;}tokenizer = new StringTokenizer(nextLine);}return true;}public String nextLine() {tokenizer = new StringTokenizer("");return innerNextLine();}public String next() {hasNext();return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}// public BigInteger nextBigInt() {
// return new BigInteger(next());
// }// 若需要nextDouble等方法,请自行调用Double.parseDouble包装}}
C. 最短距离
Q: 在一个无向图中,同类点需保证任意两点间最短距离为0,然后求非同类点的距离矩阵
思路: 图论+缩点思路
这边不是tanjar求缩点,
借助并查集来合并 借助并查集来合并 借助并查集来合并
把边长为0的两点进行合并,这样就能快速判定是否满足第一个条件(注意借桥搭路)
然后根据缩点后,进行路径的压缩k*k
跑一遍floyd就可以出来了
#include <bits/stdc++.h>using namespace std;class Dsu {
public:Dsu(int n) : n(n), arr(n + 1) {}int find(int u) {if (arr[u] == 0) return u;return arr[u] = find(arr[u]);}void merge(int u, int v) {int ai = find(u), bi = find(v);if (ai != bi) {arr[ai] = bi;}}bool isSame(int u, int v) {return find(u) == find(v);}private:int n;vector<int> arr;
};int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m, k;cin >> n >> m >> k;vector<int> cats(k);for (int i = 0; i < k; i++) {cin >> cats[i];}Dsu dsu(n);vector<array<int, 3>> edges;for (int i = 0; i < m; i++) {int u, v, w;cin >> u >> v >> w;edges.push_back({u, v, w});if (w == 0) {dsu.merge(u, v);}}bool ok = true;int start = 1;vector<int> mp(n + 1);for (int i = 0; i < cats.size(); i++) {for (int j = start + 1; j < start + cats[i]; j++) {if (!dsu.isSame(start, j)) {ok = false;break;}}for (int j = start; j < start + cats[i]; j++) mp[j] = i;start = start + cats[i];}if (!ok) {cout << "No" << endl;} else {cout << "Yes" << endl;int inf = -1;vector<vector<int>> path(k, vector<int>(k, -1));for (int i = 0; i < k; i++) path[i][i] = 0;for (auto e: edges) {int u = mp[e[0]], v = mp[e[1]];if (path[u][v] == -1 || path[u][v] > e[2]) {path[u][v] = path[v][u] = e[2];}}for (int i = 0; i < k; i++) {for (int j = 0; j < k; j++) {if (path[j][i] == -1) continue;for (int t = 0; t < k; t++) {if (path[i][t] == -1) continue;if (path[j][t] == -1 || path[j][t] > path[j][i] + path[i][t]) {path[j][t] = path[j][i] + path[i][t];}}}}for (int i = 0; i < k; i++) {for (int j = 0; j < k; j++) {cout << path[i][j] << " \n"[j == k - 1];}}}return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;public class Main {static class Dsu {int n;int[] arr;int[] gz;public Dsu(int n) {this.n = n;this.arr = new int[n + 1];this.gz = new int[n + 1];Arrays.fill(gz, 1);}int find(int u) {if (arr[u] == 0) return u;return arr[u] = find(arr[u]);}void union(int u,int v) {int ai = find(u), bi = find(v);if (ai != bi) {arr[ai] = bi;gz[bi] += gz[ai];}}int gSize(int u) {return gz[find(u)];}}public static void main(String[] args) {AReader sc = new AReader();int n = sc.nextInt(), m = sc.nextInt();int k = sc.nextInt();int[] arr = new int[k];int acc = 1;TreeMap<Integer, Integer> hash = new TreeMap<>();for (int i = 0; i < k; i++) {arr[i] = sc.nextInt();hash.put(acc, i);acc += arr[i];}long inf = Long.MAX_VALUE / 10;long[][] dp = new long[k][k];for (int i = 0; i < k; i++) {Arrays.fill(dp[i], inf);dp[i][i] = 0;}Dsu dsu = new Dsu(n);for (int i = 0; i < m; i++) {int u = sc.nextInt(), v = sc.nextInt();int x = sc.nextInt();Map.Entry<Integer, Integer> e1 = hash.floorEntry(u);Map.Entry<Integer, Integer> e2 = hash.floorEntry(v);int p1 = e1.getValue(), p2 = e2.getValue();if (p1 == p2) {} else {dp[p1][p2] = Math.min(dp[p1][p2], x);dp[p2][p1] = Math.min(dp[p2][p1], x);}if (x == 0) {dsu.union(u, v);}}boolean check = true;acc = 1;for (int i = 0; i < k; i++) {for (int j = acc; j < acc + arr[i]; j++) {if (dsu.find(acc) != dsu.find(j)) check = false;}acc += arr[i];}if (!check) {System.out.println("No");} else {System.out.println("Yes");for (int i = 0; i < k; i++) {for (int j = 0; j < k; j++) {for (int t = 0; t < k; t++) {if (dp[j][t] > dp[j][i] + dp[i][t]) {dp[j][t] = dp[j][i] + dp[i][t];}}}}for (int i = 0; i < k; i++) {for (int j = 0; j < k; j++) {if (dp[i][j] == inf) dp[i][j] = -1;}System.out.println(Arrays.stream(dp[i]).mapToObj(String::valueOf).collect(Collectors.joining(" ")));}}}staticclass AReader {private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));private StringTokenizer tokenizer = new StringTokenizer("");private String innerNextLine() {try {return reader.readLine();} catch (IOException ex) {return null;}}public boolean hasNext() {while (!tokenizer.hasMoreTokens()) {String nextLine = innerNextLine();if (nextLine == null) {return false;}tokenizer = new StringTokenizer(nextLine);}return true;}public String nextLine() {tokenizer = new StringTokenizer("");return innerNextLine();}public String next() {hasNext();return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}// public BigInteger nextBigInt() {
// return new BigInteger(next());
// }// 若需要nextDouble等方法,请自行调用Double.parseDouble包装}}