7-1 Simple Lie Detection
按题意模拟
#include <bits/stdc++.h>
using namespace std;
int N, T, K;
int get(string &str) {
int score = 0;
if (str[0] == 'f') score += -2;
if (str[N - 1] == 'a') score += -1;
int lst1 = 1, lst2 = 1;
for (int i = 1; i < N; i++) {
if (str[i - 1] == str[i]) lst1++;
else {
if (lst1 > 5) score += 3;
lst1 = 1;
}
if (str[i - 1] == 'a' && (str[i] == 'e' || str[i] == 'h')) {
score += -4;
}
if (str[i - 1] + 1 == str[i]) lst2++;
else {
if (lst2 > 3) score += 5;
lst2 = 1;
}
}
if (lst1 > 5) score += 3;
if (lst2 > 3) score += 5;
return score;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> N >> T >> K;
for (int i = 0; i < K; i++) {
string str;
cin >> str;
int score = get(str);
cout << score;
cout << (score > T ? "!!!\n" : "\n");
}
return 0;
}
The First K Largest Numbers
经典 topk
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k;
priority_queue<int, vector<int>, greater<int>> pq;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int now;
cin >> now;
pq.push(now);
while (pq.size() > k) pq.pop();
}
vector<int> res;
while (pq.size()) {
res.push_back(pq.top());
pq.pop();
}
for (int i = min(n, k) - 1; i >= 0; i--) {
cout << res[i] << " \n"[i == 0];
}
return 0;
}
Is It A Binary Search Tree - Again
层次遍历转中序遍历
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr;
vector<int> seq;
void dfs(int now) {
if (now >= arr.size()) return;
if (arr[now] == -1) return;
dfs(2 * now + 1);
seq.push_back(arr[now]);
dfs(2 * now + 2);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
assert(n > 0);
arr.resize(n);
for (int &it : arr) {
cin >> it;
}
dfs(0);
bool ok = true;
for (int i = 1; i < seq.size(); i++) {
if (seq[i - 1] > seq[i])
ok = false;
}
cout << (ok ? "YES" : "NO") << '\n';
for (int i = 0; i < seq.size(); i++) {
cout << seq[i] << " \n"[i == seq.size() - 1];
}
return 0;
}
The Rescue
大模拟
#include <bits/stdc++.h>
using namespace std;
char mp[105][105];
int dis[105][105], vis[105][105], mark[105][105];
int n, m;
int tx, ty;
int cnt;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
unordered_map<int, string> road;
char getx(int i) {
if (i == 0) return '2';
if (i == 1) return '3';
if (i == 2) return '0';
return '1';
}
void bfs() {
dis[tx][ty] = 0;
vis[tx][ty] = 1;
queue<pair<int, int>> q;
q.push({tx, ty});
while (q.size()) {
auto now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = now.first + dx[i], ny = now.second + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && mp[nx][ny] != '#') {
if (vis[nx][ny] == 0) {
dis[nx][ny] = dis[now.first][now.second] + 1;
vis[nx][ny] = 1;
q.push({nx, ny});
road[nx * 200 + ny] = road[now.first * 200 + now.second] + getx(i);
}
else {
if (dis[now.first][now.second] + 1 == dis[nx][ny]) {
string nnx = road[now.first * 200 + now.second] + getx(i);
string now = road[nx * 200 + ny];
reverse(now.begin(), now.end());
reverse(nnx.begin(), nnx.end());
if (nnx < now) {
reverse(nnx.begin(), nnx.end());
road[nx * 200 + ny] = nnx;
}
}
}
}
}
}
}
string get() {
int mx = 0;
string str = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 1) {
mx = max(mx, dis[i][j]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 1 && mx == dis[i][j]) {
if (str.size() == 0) str = road[i * 200 + j];
else {
if (road[i * 200 + j] < str) {
str = road[i * 200 + j];
}
}
}
}
}
return str;
}
pair<int, int> check(int x, int y, string &op) {
for (char &it : op) {
int nx = dx[it - '0'] + x, ny = dy[it - '0'] + y;
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
if (mp[nx][ny] == 'X') return {nx, ny};
if (mp[nx][ny] == 'O') {
x = nx, y = ny;
}
}
}
return {x, y};
}
void go(string &op) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mark[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j] == 1) {
auto nx = check(i, j, op);
if (mp[nx.first][nx.second] == 'X') {
;
}
else {
mark[nx.first][nx.second] = 1;
}
}
}
}
cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
vis[i][j] = mark[i][j];
cnt += vis[i][j];
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
memset(dis, 0x3f, sizeof dis);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mp[i][j];
if (mp[i][j] == 'O') cnt++;
if (mp[i][j] == 'X') tx = i, ty = j;
}
}
bfs();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
string x = road[i * 200 + j];
reverse(x.begin(), x.end());
road[i * 200 + j] = x;
}
}
while (cnt) {
string now = get();
cout << now;
go(now);
}
cout << '\n';
return 0;
}