7.29综合测试总结
T638298 小明搭公交
题目描述
小明为了不迟到,必须在
懒惰的小明为了尽可能晚些出发,查阅了
请帮助小明,在所有能够使他不迟到的公交车中,找出最晚出发的那一辆,并输出该公交车距离现在还有多少分钟会从车站出发。如果没有任何一辆公交车能让他准时到达学校,请输出
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, x, ans = -1;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> x;
for (int i = 1; i <= n; i++) {
int s, t;
cin >> s >> t;
if (s + t > x) continue;
ans = max(ans, s);
}
cout << ans;
return 0;
}
T638908 宾果
题目描述
有一个纵向
接下来会进行
这里,“达成宾果”指的是以下条件至少满足一项:
- 存在某一行,该行的
N 个格子全部被标记。 - 存在某一列,该列的
N 个格子全部被标记。 - 存在某一条对角线,该对角线的
N 个格子全部被标记。
输入格式
第一行输入
第二行输入
输出格式
如果在
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, t;
map<int, map<int, bool> > vis;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> t;
for (int tt = 1; tt <= t; tt++) {
int a;
cin >> a;
if (a % n == 0) vis[a / n][n] = true;
else vis[a / n + 1][a % n] = true;
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= n; j++) if (vis[i][j] == true) sum++;
if (sum == n) {
cout << tt; return 0;
}
}
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= n; j++) if (vis[j][i] == true) sum++;
if (sum == n) {
cout << tt; return 0;
}
}
int sum = 0;
for (int i = 1; i <= n; i++) if (vis[i][i] == true) sum++;
if (sum == n) {
cout << tt; return 0;
}
sum = 0;
for (int i = 1; i <= n; i++) if (vis[i][n + 1 - i] == true) sum++;
if (sum == n) {
cout << tt; return 0;
}
}
cout << -1;
return 0;
}
错误原因
由于没算清复杂度, 超时仅拿了44分。
正确思路
-
经过观察我们可以发现:如果输入的是
x , 那么被标记的是第(x - 1) / n + 1 行第(x - 1) mod n + 1 暴力核心代码框架如下:bool check() { //返回是否达成宾果 } for (int i = 1; i <= t; i++) { int x; cin >> x; int r = (x - 1) / n + 1; int c = (x - 1) % n + 1; vis[r][c] = 1; if (check()) {cout << i; return 0;} } -
我们有两个思路
-
思路1:
我们可以尽可能的减少
check 函数调用的次数。二分回合数:
因为回合数
t 越大, 越容易宾果, 所以我们可以分析出, 题目具有单调性。我们还可以很轻松的想到一旦第
i 回合宾果了, 后面所以的回合一定宾果。我们可以用一个
x[] 数组存储操作;还可以用一个
time[][] 二维数组,time[i][j] 表示第i 行第j 列是什么时候标记的。二分代码框架如下:
int l = 0, r = t + 1;
while () {
int mid = _____;//mid:执行到了第mid回合
if (check()) //从第1回合枚举到第mid回合进行标记, 然后再检查
}
-
思路2:
和八皇后问题一样:使用行, 列, 对角线的标记数组
如果输入的是
x , 那么hang[r]++ ,lie[c]++ , 这里的r 和c 指的是是吗暴力代码中的行和列。 如果r == c , 那么主对角线被标记的数字加1 ; 如果r + c == n + 1 , 那么副对角线被标记的数字加1 我们可以推一下样例:
输入
5 ,标记这些:hang[2]++ lie[2]++ if (r == c)$ $zdj++ if (r + c == n + 1)$ $fdj++ 输入
1 ,标记:hang[1]++ lie[1]++ zdj++ 输入
8 ,标记:hang[3]++ lie[2]++ 输入
9 ,标记:hang[3]++ lie[3]++ 输入$7$,标记: $hang[3]++ lie[1]++ fdj++ 因此我们的数量是正确的
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, t;
int hang[100700], lie[100700], zdj, fdj;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> t;
for (int tt = 1; tt <= t; tt++) {
int a;
cin >> a;
int x = (a - 1) / n + 1;
int y = (a - 1) % n + 1;
hang[x]++; lie[y]++;
if (x == y) zdj++;
if (x + y == n + 1) fdj++;
for (int i = 1; i <= n; i++)
if (hang[i] == n) {
cout << tt; return 0;
}
for (int i = 1; i <= n; i++)
if (lie[i] == n) {
cout << tt; return 0;
}
if (zdj == n) {
cout << tt; return 0;
}
if (fdj == n) {
cout << tt; return 0;
}
}
cout << -1;
return 0;
}
T641930 长度为n的第k个排列
题目描述
求
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k;
int cnt;
bool flag = false;
int a[1000007];
bool vis[1000007];
void dfs(int row) {
if (flag == true) return;
if (row == n + 1) {
cnt++;
if (cnt == k) {flag = true; for (int i = 1; i <= n; i++) cout << a[i] << " ";}
return;
}
for (int i = 1; i <= n; i++)
if (vis[i] == false) {
a[row] = i;
vis[i] = true;
dfs(row + 1);
a[row] = 0;
vis[i] = false;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
dfs(1);
return 0;
}
T638280 兜兜转转还是你
题目描述
我们有一个网格,从上到下有
每个方格都是以下其中之一:初始点、道路和障碍物。
格子
请判断是否有一条长度为
更具体地说,请判断是否存在满足以下条件的整数
-
n \geq 4 -
C_{x_0, y_0} = C_{x_n, y_n} = \texttt{S} - 对于
1 \leq i \leq n-1 ,有C_{x_i, y_i} = \texttt{.} - 对于
1 \leq i < j \leq n-1 ,有(x_i, y_i) \neq (x_j, y_j) - 对于
0 \leq i \leq n-1 ,格子(x_i, y_i) 与(x_{i+1}, y_{i+1}) 必须在上下或左右相邻
输入格式
第一行输入 h 和 w
接下来 h 行, 每行输入 w 个字符。
输出格式
如果存在满足题意的路径,输出 Yes,否则输出 No。
说明/提示
数据范围
对于 100\% 的数据:
- 我们有两种输入的方法
方法1:
vector<char> g[N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
g[i].push_back(0);
for (int j = 1; j <= m; j++) {
char x; cin >> x;
g[i].push_back(x);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << g[i][j];
}
cout << endl;
}
return 0;
}
方法2:
vector<vector<char> > g;
int main() {
cin >> n >> m;
//设置二维动态数组g的行数和列数, 然后就可以当正常数组用
g.resize(n + 5, vector<char>(m + 5) );
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> g[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << g[i][j];
}
cout << endl;
}
return 0;
}
-
我们可以进行四次
bfs bfs(1):$从位置$1$出发, 如果能搜到$1, 2, 3, 4$就输出$YES bfs(2):$从位置$2$出发, 如果能搜到$1, 2, 3, 4$就输出$YES bfs(3):$从位置$3$出发, 如果能搜到$1, 2, 3, 4$就输出$YES bfs(4):$从位置$4$出发, 如果能搜到$1, 2, 3, 4$就输出$YES
核心代码如下:
for (int i = 0; i < 4; i++) {
int x = sx + dx[i], y = sy + dy[i];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (!vis[x][y]) {
vis[x][y] = 1;
while (!q.empty()) q.pop();
q.push({x, y});
while (!q.empty()) {
node k = q.front();
q.pop();
for (int j = 0; j < 4; j++) {
int xx = k.x + dx[j], yy = k.y + dy[j];
if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue;
vis[xx][yy] = 1;
if (xx == sx + 1 && yy == sy || xx == sx - 1 && yy == sy || xx == sx && yy == sy + 1 || xx == sx && yy == sy - 1) {
cout << "Yes" << endl;
return 0;
}
q.push({xx, yy});
}
}
}
}
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 7;
int n, m, sx, sy;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char ch;
struct node {
int x, y;
};
queue<node> q;
vector<int> vis[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
vis[i].push_back(0);
for (int j = 1; j <= m; j++) {
cin >> ch;
if (ch == '#') vis[i].push_back(1);
else if (ch == 'S') sx = i, sy = j, vis[i].push_back(1);
else vis[i].push_back(0);
}
}
for (int i = 0; i < 4; i++) {
int x = sx + dx[i], y = sy + dy[i];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (!vis[x][y]) {
vis[x][y] = 1;
while (!q.empty()) q.pop();
q.push({x, y});
while (!q.empty()) {
node k = q.front();
q.pop();
for (int j = 0; j < 4; j++) {
int xx = k.x + dx[j], yy = k.y + dy[j];
if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue;
vis[xx][yy] = 1;
if (xx == sx + 1 && yy == sy || xx == sx - 1 && yy == sy || xx == sx && yy == sy + 1 || xx == sx && yy == sy - 1) {
cout << "Yes" << endl;
return 0;
}
q.push({xx, yy});
}
}
}
}
cout << "No" << endl;
return 0;
}