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 宾果
题目描述
有一个纵向 行、横向 列的网格,对于从上到下第 行、从左到右第 列的格子,格子中写有整数 。
接下来会进行 个回合,每回合会宣布一个互不相同的整数。在第 回合,会宣布 ,并在写有 的格子上做标记。请你求出首次达成“宾果”(Bingo)的回合数。如果在 个回合内都无法达成宾果,请输出 。
这里,“达成宾果”指的是以下条件至少满足一项:
- 存在某一行,该行的 个格子全部被标记。
- 存在某一列,该列的 个格子全部被标记。
- 存在某一条对角线,该对角线的 个格子全部被标记。
输入格式
第一行输入 第二行输入
输出格式
如果在 个回合内达成宾果,请输出首次达成宾果的回合数;否则输出 。
错误代码
#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分。
正确思路
- 经过观察我们可以发现:如果输入的是, 那么被标记的是第行第 暴力核心代码框架如下:
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:
我们可以尽可能的减少函数调用的次数。
二分回合数:
因为回合数越大, 越容易宾果, 所以我们可以分析出, 题目具有单调性。
我们还可以很轻松的想到一旦第回合宾果了, 后面所以的回合一定宾果。
我们可以用一个数组存储操作;
还可以用一个二维数组, 表示第行第列是什么时候标记的。
二分代码框架如下:
int l = 0, r = t + 1;
while () {
int mid = _____;//mid:执行到了第mid回合
if (check()) //从第1回合枚举到第mid回合进行标记, 然后再检查
}-
思路2:
和八皇后问题一样:使用行, 列, 对角线的标记数组
如果输入的是, 那么, , 这里的和指的是是吗暴力代码中的行和列。 如果, 那么主对角线被标记的数字加; 如果, 那么副对角线被标记的数字加
我们可以推一下样例:
输入,标记这些:
输入,标记:
输入,标记:
输入,标记:
//已经是, 输出(也就是4)
输入,标记:
因此我们的数量是正确的
正确代码
#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 兜兜转转还是你
题目描述
我们有一个网格,从上到下有 行,从左到右有 列。让 表示从上到下 的第 行,从左到右 的第 列。
每个方格都是以下其中之一:初始点、道路和障碍物。 格子 的状态用字符 表示,若 ,则为起点;若 ,则为道路;若 ,则为障碍物。起点格子仅有一个。
请判断是否有一条长度为 或更长的路径,从初始点开始,重复垂直或水平移动到相邻的方格,然后返回初始点,除了开始和结束,不经过障碍物或多次访问同一方格。 更具体地说,请判断是否存在满足以下条件的整数 和方格序列 。
- 对于 ,有
- 对于 ,有
- 对于 ,格子 与 必须在上下或左右相邻
输入格式
第一行输入 h 和 w
接下来 h 行, 每行输入 w 个字符。
输出格式
如果存在满足题意的路径,输出 Yes,否则输出 No。
说明/提示
数据范围
对于 的数据:
均为不小于 的整数
只会是 S、.、# 之一
仅有一个格子 S
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
cout << "Yes";
return 0;
}错误原因
思路错误
正确思路
-
由于本题时间很大, 因此我们要用
-
我们有两种输入的方法
方法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;
}-
我们可以进行四次
从位置出发, 如果能搜到就输出
从位置出发, 如果能搜到就输出
从位置出发, 如果能搜到就输出
从位置出发, 如果能搜到就输出
核心代码如下:
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;
}
