T659310 宾果
-
时间复杂度:
-
正确代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N, T;
cin >> N >> T;
vector<int> cnt1(N + 1, 0); // 记录每行被标记的数量
vector<int> cnt2(N + 1, 0); // 记录每列被标记的数量
int x = 0;
int y = 0;
for (int ans = 1; ans <= T; ++ans) {
int A;
cin >> A;
int i = (A - 1) / N + 1;
int j = (A - 1) % N + 1;
// 检查行是否达成宾果
if (++cnt1[i] == N) {
cout << ans << endl;
return 0;
}
// 检查列是否达成宾果
if (++cnt2[j] == N) {
cout << ans << endl;
return 0;
}
// 检查主对角线是否达成宾果
if (i == j) {
if (++x == N) {
cout << ans << endl;
return 0;
}
}
// 检查副对角线是否达成宾果
if (i + j == N + 1) {
if (++y == N) {
cout << ans << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}T659306 落落的去维护数组
-
时间复杂度:
-
正确思路:
利用线段树并按照题目的要求快速查询和修改
-
正确代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n;
struct node{
int l, r;
int id;
int sum;
int cnt;
}tree[4 * N];
int a[N];
void push_up(int id) {
tree[id].sum = tree[id * 2].sum + tree[id * 2 + 1].sum;
}
void push_down(int id) {
if (tree[id].cnt != 0) {
tree[id * 2].sum = (tree[id * 2].r - tree[id * 2].l + 1) * tree[id].cnt;
tree[id * 2].cnt = tree[id].cnt;
tree[id * 2 + 1].sum = (tree[id * 2 + 1].r - tree[id * 2 + 1].l + 1) * tree[id].cnt;
tree[id * 2 + 1].cnt = tree[id].cnt;
tree[id].cnt = 0;
}
}
void build(int id,int l,int r) {
tree[id].id = id;
tree[id].l = l;
tree[id].r = r;
if (l == r) {
tree[id].sum = a[l];
return ;
}
int mid = (l + r) >> 1;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
push_up(id);
}
void add(int id, int x, int val) {
if (tree[id].l == x && tree[id].r == x) {
tree[id].sum += val;
return ;
}
push_down(id);
int mid = (tree[id].l + tree[id].r) >> 1;
if (x <= mid){
add(id * 2, x, val);
} else {
add(id * 2 + 1, x, val);
}
push_up(id);
}
int query (int id, int x) {
if (tree[id].l == tree[id].r){
return tree[id].sum;
}
push_down(id);
int mid = (tree[id].l + tree[id].r) >> 1;
if (x <= mid){
return query(id * 2, x);
} else {
return query(id * 2 + 1, x);
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
int tmp;
cin >> tmp;
while (tmp--) {
int op;
cin >> op;
if (op == 1) {
int k;
cin >> k;
tree[1].cnt = k;
} else if (op == 2) {
int x, k;
cin >> x >> k;
add(1, x, k);
} else {
int x;
cin >> x;
cout << query(1, x) << endl;
}
}
return 0;
}T659291 小梦的守卫
-
时间复杂度:
-
正确思路:
先预处理体力值的前缀和和后缀和,然后使用二分去查找两边死掉的人
-
正确代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10, INF = 1E9;
int n, Q, a[N], s1[N], s2[N];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> Q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
s1[i] = s1[i - 1] + a[i];
}
for (int i = n; i >= 1; i--) {
s2[n - i + 1] = s2[n - i] + a[i];
while (Q--) {
int x;
cin >> x;
int l = upper_bound(s1 + 1, s1 + 1 + n, x) - s1 - 1; //用upper算出左右两边死了几个人
int r = upper_bound(s2 + 1, s2 + 1 + n, x) - s2 - 1;
if (l + r > n) { //如果死的人大于了n
cout << "Zombies ate your brain!\n";
} else {
cout << n - l - r << endl;
}
}
return 0;
}T659323 兜兜转转还是你
-
时间复杂度:
-
正确思路:
首先把初始点给封住,让后再bfs周围点,所以此题只需要4次bfs。然后看是否能到达另外3个点。
-
正确代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, sx, sy, tot = 1;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char c;
struct node {
int x, y;
};
queue<node> q;
vector<int> vis[N];
int main() {
ios::sync_with_stdio(0);
cout.tie(0); cin.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 >> c;
if (c == '#') {
vis[i].push_back(1);
} else if (c == '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.size()) {
q.pop();
}
q.push( {x, y} );
while (q.size()) {
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;
}