画展
没看数据范围想了半天DP。裸贪心。
滑冰场
1.建分层图跑最短路
2.启发式搜索
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int MAX_N = 2000;
constexpr int INF = LLONG_MAX;
int n, m; // Grid dimensions
char grid[MAX_N][MAX_N];
int dist[MAX_N][MAX_N][5];
pair<int, int> start;
const int dx[5] = {0, 1, 0, -1, 0};
const int dy[5] = {0, 0, 1, 0, -1};
struct Node {
int x, y, dir, turns;
};
queue<Node> q;
void bfs() {
for (int i = 1; i <= 4; ++i) {
dist[start.first][start.second][i] = 0;
q.push({start.first, start.second, i, 0});
}
while (!q.empty()) {
Node curr = q.front();
q.pop();
int x = curr.x, y = curr.y, dir = curr.dir, turns = curr.turns;
if (x < 1 || x > n || y < 1 || y > m) continue;
if (grid[x][y] == '.') {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (dist[nx][ny][dir] > dist[x][y][dir] + 1) {
dist[nx][ny][dir] = dist[x][y][dir] + 1;
q.push({nx, ny, dir, turns + 1});
}
}
else {
for (int newDir = 1; newDir <= 4; ++newDir) {
int nx = x + dx[newDir];
int ny = y + dy[newDir];
if (dist[nx][ny][newDir] > dist[x][y][dir] + 1 + turns) {
dist[nx][ny][newDir] = dist[x][y][dir] + 1 + turns;
q.push({nx, ny, newDir, turns + 1});
}
}
}
}
}
void solve() {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> grid[i][j];
if (grid[i][j] == 'G') {
start = {i, j};
}
for (int k = 1; k <= 4; ++k) {
dist[i][j][k] = INF;
}
}
}
bfs();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int res = min({dist[i][j][1], dist[i][j][2], dist[i][j][3], dist[i][j][4]});
if (res == INF) cout << -1 << ' ';
else cout << res << ' ';
}
cout << endl;
}
}
signed main() {
freopen("U.in","r",stdin);
freopen("U.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
solve();
return 0;
}
比赛
这种题还是跳了把...noip前不考虑
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
int T, n, p;
int a[N];
int calculateAnswer(int n, int p, int a[]) {
int ans = 0;
sort(a + 1, a + n + 1);
if (p == 1) {
for (int i = 1; i <= n - 2; ++i) {
if (a[i] + n >= a[n] + 1)
ans += (n + i) * (n - i - 1) / 2 - (n - i - 1) * (i + 1);
}
for (int i = 1; i <= n - 2; ++i) {
if (a[i] + n >= a[n - 1] + 1)
ans += n - i - 2;
}
for (int i = 1; i <= n - 2; ++i) {
if (a[i] + n >= a[n - 2] + 1)
ans++;
}
} else if (p == 2) {
for (int i = 1; i <= n - 2; ++i) {
if (a[i] + n >= a[n] + 1)
ans += (n + i) * (n - i - 1) / 2 - (n - i - 1) * (i + 1);
}
for (int i = 1; i <= n - 1; ++i) {
if (a[i] + n >= a[n - 1] + 1)
ans += n - i - 2;
}
for (int i = 1; i <= n - 1; ++i) {
if (a[i] + n >= a[n - 1] + 1)
ans++;
}
}
return ans;
}
void solve() {
cin >> n >> p;
for (int i = 1; i <= n; ++i)
cin >> a[i];
cout << calculateAnswer(n, p, a) << endl;
}
signed main() {
freopen("T.in", "r", stdin);
freopen("T.out", "w", stdout);
cin >> T;
while (T--) solve();
return 0;
}
标签:11,25,nx,dist,int,2024,ny,ans,dir
From: https://www.cnblogs.com/Kang-shifu/p/18568841