T4:
解题思路: 给定一个广告牌A还有一个广告牌B。
让我们求解A还剩余多少被遮挡,需要一块多大得防水布才能完全遮挡。
但是防水布只能是矩阵,因此我们得防水布可能不完全等于未遮挡得面积,所以我们要求解得是遮挡未覆盖得面积最小得矩阵需要多大
代码:
#include<bits/stdc++.h>
using namespace std;
int vis[2010][2010];
int main(){
int x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4;
cin >> x1 >> y1 >> x2 >> y2;
x1 += 1000 , y1 += 1000 , x2 += 1000 , y2 += 1000;
for(int i = x1;i < x2;i ++){
for(int j = y1;j < y2;j ++){
vis[i][j] = 1;
}
}
cin >> x3 >> y3 >> x4 >> y4;
x3 += 1000 , y3 += 1000 , x4 += 1000 , y4 += 1000;
for(int i = x3;i < x4;i ++){
for(int j = y3;j < y4;j ++){
vis[i][j] = 0;
}
}
int mx = -1e9 , my = -1e9 , nx = 1e9 , ny = 1e9;
for(int i = 0;i <= 2000;i ++){
for(int j = 0;j <= 2000;j ++){
if(vis[i][j]){
mx = max(mx , i);
my = max(my , j);
nx = min(nx , i);
ny = min(ny , j);
}
}
}
if(mx == -1e9)
cout << 0;
else
cout << (mx - nx + 1) * (my - ny + 1);
return 0;
}T5:
解题思路: 这道题目要求我们找到从起点 (1, 1) 到终点 (n, m) 的任意一条路径,并且保证路径的长度不超过 100,000。题目中给出的地图是一个二维矩阵,其中 ’.’ 表示可以通过的区域,’*’ 表示不可通过的障碍物。我们需要使用广度优先搜索(BFS)来找到这样一条路径,因为 BFS 可以保证找到的路径是最短的,同时也能够满足题目对路径长度的限制。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 160;
struct node {
int x, y;
};
node pre[N][N];
int vis[N][N];
char s[N][N];
int n, m;
int d[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
void bfs(int x, int y) {
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
vis[i][j] = -1;
}
}
vis[x][y] = 0;
queue<node>q;
q.push({x, y});
while (q.size()) {
node t = q.front();
q.pop();
if (t.x == n && t.y == m) {
return ;
}
for (int i = 0; i < 4; i ++) {
int px = t.x + d[i][0], py = t.y + d[i][1];
if (px < 1 || px > n || py < 1 || py > m)
continue;
if (vis[px][py] != -1)
continue;
if (s[px][py] == '*')
continue;
vis[px][py] = vis[t.x][t.y] + 1;
pre[px][py] = t;
q.push({px, py});
}
}
}
node ans[N * N];
int idx;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
cin >> s[i][j];
}
}
bfs(1 , 1);
int x = n, y = m;
while (x != 0 && y != 0) {
ans[++ idx] = {x, y};
int nx = pre[x][y].x, ny = pre[x][y].y;
x = nx, y = ny;
}
for (int i = idx; i >= 1; i -- ) {
cout << ans[i].x << " " << ans[i].y << endl;
}
return 0;
}T6:
解题思路: 给定了n个数字,我们要将每个数字变成奇数,也就是如果一个数字是一个偶数我们要将这个数字除以2,并且变成两个数字。例如一个数字x是个偶数,那么我们会将他变成x / 2,一个数字X,他最终切出来的数字一定相等,因为每一次都在平分。因此每一个数字切出来的部分,应该用一个结构体来存储
代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
int x , cnt;
};
long long sum[1000010];
node a[1000010];
int main(){
int n , q;
long long x;
cin >> n;
for(int i = 1;i <= n;i ++){
cin >> x;
int cnt = 1;
while(x % 2 == 0){
x /= 2;
cnt *= 2;
}
a[i].x = x , a[i].cnt = cnt;
sum[i] = sum[i - 1] + a[i].cnt;
}
cin >> q;
while(q --){
cin >> x;
int pos = lower_bound(sum + 1 , sum + 1 + n , x) - sum;
cout << a[pos].x << endl;
}
return 0;
}T7未讲解。