目录
A小竹与妈妈
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
ll a,b,x;
cin >> a >> b >> x;
cout << (x - b) / a;
}
B走失的小竹
每次看看劫匪占没占据出口,没占全部出口可走,否则减去房间内有的出口
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int st[N];
int main()
{
int n,m,q;
int cnt = 0;
cin >> n >> m >> q;
for(int i = 1; i <= n; i ++)
{
int x;
cin >> x;
st[x] ++;
}
while(q --)
{
int x;
cin >> x;
cout << n - st[x] << endl;
}
}
C小竹关禁闭
用f[ i ]表示从前 i 个物品中选的最大长度,如果选择第 i 个物品,说明之前 k 个物品都没选
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 2010;
int a[N];
int f[N];
int n,k;
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i <= n ; i ++)
{
if(i - k - 1 < 1)
f[i] = max(f[i - 1],a[i]);
else
f[i] = max(f[i - 1],f[i - k - 1] + a[i]);
}
cout << f[n] << endl;
}
D游戏购买
先 bfs 出从起点能到的位置,再 bfs 出从终点能到的位置,枚举两个点都能到的位置,选出大于 x 且最短得距离,当时做的时候我以为要买好多个游戏。。。。。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define long long int
typedef pair<int, int> PII;
int n, m, s;
int sx, sy, ex, ey;
const int maxn = 2010;
int p[maxn][maxn], d[maxn][maxn], dis[maxn][maxn];
void bfs1()
{
memset(d, -1, sizeof(d));
queue<PII> q;
q.push({ex, ey});
d[ex][ey] = 0;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
while(!q.empty())
{
auto t = q.front();
q.pop();
for(int i = 0; i < 4; i ++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x < 1 || x > n || y < 1 || y > m || p[x][y] == -1 || d[x][y] != -1)
continue;
d[x][y] = d[t.first][t.second] + 1;
q.push({x, y});
}
}
}
int bfs()
{
int mn = 0x3f3f3f3f;
memset(dis, -1, sizeof(dis));
queue<PII> q;
q.push({sx, sy});
dis[sx][sy] = 0;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
while(!q.empty())
{
auto t = q.front();
q.pop();
for(int i = 0; i < 4; i ++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x < 1 || x > n || y < 1 || y > m || p[x][y] == -1 || dis[x][y] != -1)
continue;
dis[x][y] = dis[t.first][t.second] + 1;
q.push({x, y});
}
}
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
if(p[i][j] > s && d[i][j] != -1 && dis[i][j] != -1)
mn = min(mn, d[i][j] + dis[i][j]);
}
if(mn == 0x3f3f3f3f)
return -1;
return mn;
}
void solve()
{
cin >> n >> m >> s;
cin >> sx >> sy >> ex >> ey;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
cin >> p[i][j];
bfs1();
cout << bfs() << endl;
}
int main()
{
solve();
return 0;
}
E寻找小竹
用f[ i ] 表示以 i 为根节点的最大联通块
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 5e6 + 10;
int h[N], e[N], ne[N], idx;
int a[N],f[N];
int res;
int cnt[N];
bool st[N];
void init()
{
for (int i = 2; i <= N; i++)
{
if (!st[i])
{
for(int j = 2 * i; j <= N; j += i)
st[j] = true, cnt[j] ++;
}
}
memset(st,0,sizeof st);
for(int i = 2; i <= N; i ++)
{
if(cnt[i] >= 2)
st[i] = true;
}
}
void add(int a, int b)
{
e[idx] = b,ne[idx] = h[a],h[a] = idx ++;
}
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
void dfs(int u, int father)
{
f[u] = 1;
for (int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if (j == father)
continue;
dfs(j, u);
if (st[gcd(a[j], a[u])])
f[u] += f[j];
}
res = max(res, f[u]);
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
init();
memset(h, -1, sizeof h);
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
add(a, b), add(b,a);
}
dfs(1, -1);
cout << res << endl;
}
F被抓住的小竹
经过不懈努力,终于。。。。。找到了题解