D - Not Divisible
从小到大排序,有相同数不计入,暴力计算
// atcoder.cpp :
//
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <vector>
#include <regex>
#include <queue>
#include <climits>
using namespace std;typedef pair<int, int> pii;
typedef long long LL;
typedef vector<int> vi;int n;
int a[200020];
bool h[1000010];int main()
{//freopen("in.txt", "r", stdin);scanf("%d", &n);for (int i = 0; i < n; ++i)scanf("%d", a + i);map<int, int> cnt;for (int i = 0; i < n; ++i) {cnt[a[i]] += 1;}set<int> bs;for (auto it : cnt) {if (it.second == 1) {bs.insert(it.first);}}sort(a, a + n);int ans = 0;for (int i = 0; i < n; ++i) {if (bs.count(a[i]) != 0) {int j = 1, t = a[i];int ok = 1;while (j * j <= t) {if (t % j == 0) {if (h[j]) {ok = 0;break;}if (h[t / j]) {ok = 0;break;}}j += 1;}ans += ok;}h[a[i]] = 1; }printf("%d\n", ans);
}
E - Smart Infants
模拟题,使用stl中的multiset就可以
维护幼儿园的最大堆,以及总体的最小堆
// atcoder.cpp :
//
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <vector>
#include <regex>
#include <queue>
#include <climits>
using namespace std;typedef pair<int, int> pii;
typedef long long LL;
typedef vector<int> vi;multiset<int> top;
multiset<int, std::greater<int>> kin[200010];int n, q;
int a[200010]; // 记录val
int g[200010]; // 记录每个孩子的kinint main()
{//freopen("in.txt", "r", stdin);scanf("%d%d", &n, &q);for (int i = 0; i < n; ++i) {int x, y;scanf("%d%d", &x, &y);kin[y].insert(x);a[i] = x;g[i] = y;}for (int i = 1; i <= 200000; ++i) {if (kin[i].size() > 0) {auto bg = kin[i].begin();top.insert(*bg);}}for (int i = 0; i < q; ++i) {int c, d;scanf("%d%d", &c, &d);c--;int v = a[c], k = g[c];auto p = kin[k].find(v);int t = *kin[k].begin();kin[k].erase(p); // erase 迭代器只删除一个// 不能用erase(v)// 维护topif (kin[k].size() == 0 || *kin[k].begin() != t) {auto p1 = top.find(t);top.erase(p1);if (kin[k].size() != 0) {top.insert(*kin[k].begin());}}g[c] = d;t = -1;if (kin[d].size() > 0) {t = *kin[d].begin();}kin[d].insert(v);if (t == -1) {top.insert(*kin[d].begin());}else if (*kin[d].begin() != t) {auto p2 = top.find(t);top.erase(p2);top.insert(*kin[d].begin());}printf("%d\n", *top.begin());}return 0;
}
F - Pond Skater
BFS ,采用dist数组标记,如果已经预先访问到了,而且计算得到的步数是相同的,那么就break,不需要再浪费后面的步数去计算
// atcoder.cpp :
//
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <vector>
#include <regex>
#include <queue>
#include <climits>
using namespace std;typedef pair<int, int> pii;
typedef long long LL;
typedef vector<int> vi;int n, m, k;
int bx, by, ex, ey;
vector<vi> mat;
vector<vi> dist;
char s[1000200];
int dirs[4][2] = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
const int INF = 1 << 30;int main()
{//freopen("in.txt", "r", stdin);scanf("%d%d%d", &n, &m, &k);scanf("%d%d%d%d", &bx, &by, &ex, &ey);bx--, by--, ex--, ey--;for (int i = 0; i < n; ++i) {scanf("%s", s);vi vec;vi temp(m, INF);dist.push_back(temp);for (int j = 0; j < m; ++j) {if (s[j] == '.')vec.push_back(0);elsevec.push_back(1);}mat.push_back(vec);}queue<pii> qu;qu.push(pii(bx, by));dist[bx][by] = 0;while (!qu.empty()) {pii top = qu.front();int x = top.first, y = top.second;qu.pop();for (int j = 0; j < 4; ++ j) {for (int i = 1; i <= k; ++ i) {int nx = x + dirs[j][0] * i, ny = y + dirs[j][1] * i;if (nx >= 0 && nx < n && ny >= 0 && ny < m) {if (mat[nx][ny] == 1)break;if (dist[nx][ny] <= dist[x][y])break;if (dist[nx][ny] > dist[x][y] + 1) {qu.push(pii(nx, ny));dist[nx][ny] = dist[x][y] + 1;}}}}}if (dist[ex][ey] == 1 << 30) {printf("-1\n");}else {printf("%d\n", dist[ex][ey]);}return 0;
}