网络选拔赛第一场
使用js尝试
提交的记录中貌似只有我使用的js
下次试试使用C++参与
C Smart Browser
链接: Smart Browser.
let str = readline();
let ans = 0;
let n = 0;
let i = 0;
while(i < str.length) {
while(str[i++] === 'w') {
n++;
}
if (n) {
ans = ans + 2 * n - 1;
n = 0;
}
}
print(ans);
D Router Mesh
链接: Router Mesh.
图
使用tarjin算法可以解决的一道题,使用js写出来没有全过,可能还有一点问题。
const N = 3e5 + 10;
const v = new Array(N).fill(0).map(i => []);
const dfn = [];
const low = [];
const f = new Array(N).fill(0);
let tot = 0, ans = 0;
const tarjan = (x, per) => {
if (dfn[x]) return;
dfn[x] = low[x] = ++tot;
for (let i = 0; i < v[x].length; i++) {
let to = v[x][i];
if (dfn[to]) {
low[x] = Math.min(low[x], dfn[to]);
continue;
}
tarjan(to, x);
if (dfn[x] <= low[to]) f[x]++;
low[x] = Math.min(low[x], low[to]);
}
if (per === 0) f[x]--;
}
let [n, m] = readline().split(' ').map(Number);
while(m--) {
let [x, y] = readline().split(' ').map(Number);
v[x].push(y);
v[y].push(x);
}
for (let i = 1; i <= n; ++i) {
if (!dfn[i]) {
tarjan(i, 0);
ans++;
}
}
print(f.splice(1, n).map(i => i + ans).join(' '));
I Walking Machine
链接: Walking Machine.
DFS的一个题目
let [n, m] = readline().split(' ').map(Number);
const maze = new Array(n);
const ans = new Array(n);
for (let i = 0; i < n; ++i) {
maze[i] = readline().split('');
ans[i] = new Array(m);
}
const bfs = (i, j) => {
if (i < 0 || i >= n || j < 0 || j >= m) {
return 2;
}
if (ans[i][j] === 0 || ans[i][j] === 3) return 3;
if (ans[i][j] === 2) return 2;
ans[i][j] = 0;
let cur = maze[i][j];
if (cur === 'A') {
ans[i][j] = bfs(i, j - 1);
return ans[i][j];
}
if (cur === 'D') {
ans[i][j] = bfs(i, j + 1);
return ans[i][j];
}
if (cur === 'W') {
ans[i][j] = bfs(i - 1, j);
return ans[i][j];
}
if (cur === 'S') {
ans[i][j] = bfs(i + 1, j);
return ans[i][j];
}
}
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
ans[i][j] = bfs(i, j);;
}
}
let res = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < m; ++j) {
if (ans[i][j] === 2) {
res++;
}
}
}
console.log(res);
A Intelligent Warehouse
链接: Intelligent Warehouse .
暴力法超时
let n = readline();
const a = readline().split(' ').map(Number).sort((a, b) => a - b);
const group = new Array(n);
group[0] = 1;
for (let i = 1; i < n; ++i) {
let cur = a[i];
let max = 1;
for (let j = 0; j < i; j++) {
if (cur % a[j] === 0) {
max = Math.max(max, group[j] + 1);
}
}
group[i] = max;
}
let ans = 0;
for (let i = 0; i < n; ++i) {
ans = Math.max(group[i], ans);
}
print(ans);
dp 通过50% T-T
let n = readline();
const a = readline().split(' ').map(Number);
const M = 1e7;
const ctn = new Array(M + 5).fill(0);
const dp = new Array(M + 5).fill(0);
a.forEach(i => {
ctn[i]++;
});
let ans = 0;
for (let i = 1; i <= M; ++i) {
if (!ctn[i]) continue;
dp[i] += ctn[i];
for (let j = i + i; j <= M; j += i) {
dp[j] = Math.max(dp[j], dp[i]);
}
ans = Math.max(ans, dp[i]);
}
print(ans);