| A | B | C | D | E | F | 总分 |
|---|---|---|---|---|---|---|
| 100 | 100 | 0 | 20 | - | - | 220 |
错题分析
P4779 单源最短路径
错误原因
数组开小导致 RE
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = (1LL << 31) - 1;
struct node
{
int y, w;
bool operator < (const node & tmp) const
{
return w > tmp.w;
}
} ;
vector<node> nbr[100005];
int n, m, s;
int dis[100005];
bool vis[1000005];
void dij()
{
priority_queue<node> qp;
for (int i = 1; i <= n; i++)
dis[i] = INF;
memset(vis, false, sizeof vis);
dis[s] = 0;
qp.push({s, dis[s]});
while (!qp.empty())
{
int x = qp.top().y;
qp.pop();
if (vis[x] == true)
continue;
vis[x] = true;
for (auto a : nbr[x])
{
int y = a.y, w = a.w;
if (dis[x] + w < dis[y])
{
dis[y] = dis[x] + w;
qp.push({y, dis[y]});
}
}
}
}
signed main()
{
cin >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
int u, v, w;
cin >> u >> v >> w;
nbr[u].push_back({v, w});
}
dij();
for (int i = 1; i <= n; i++)
cout << dis[i] << " ";
return 0;
}
P3385 负环
错误原因
储存图的 vector 没清空(多组询问)
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
int y, w;
} ;
vector<node> e[2005];
int n, m, u, v, w, cnt[2005], dis[2005];
bool vis[2005];
bool SPFA(int s)
{
memset(dis, 0x3f, sizeof dis);
memset(vis, false, sizeof vis);
memset(cnt, 0, sizeof cnt);
queue <int> q;
dis[s] = 0;
vis[s] = true;
q.push(s);
cnt[s] = 0;
while (q.size())
{
int x = q.front();
q.pop();
vis[x] = false;
for (auto tmp : e[x])
{
int y = tmp.y, w = tmp.w;
if (dis[x] + w < dis[y])
{
dis[y] = dis[x] + w;
cnt[y] = cnt[x] + 1;
if (cnt[y] >= n)
return true;
if (!vis[y])
{
q.push(y);
vis[y] = true;
}
}
}
}
return false;
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
e[i].clear();
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
e[u].push_back({v, w});
if (w >= 0)
{
e[v].push_back({u, w});
}
}
if (SPFA(1))
{
cout << "YES\n";
} else
{
cout << "NO\n";
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
P7167 [eJOI 2020] Fountain (Day1)
题意
给你 个圆盘
直径 容量
如果倒入的水比 大,那么水就会流到往下数第一个 的圆盘里,如果没有,则是流入水池
给定 组询问:向第 个圆盘里倒入 的水,问水最后会流到哪个圆盘里,如果流到了水池里则输出 否则输出圆盘编号
思路
subtask1
对于 subtask1,直接模拟即可
- 模拟时检查现在枚举的圆盘是不是半径更大,如果是,就检查这个圆盘容量是否 ,如果不是, 将 减去 ,继续枚举,并维护 表示上一个圆盘,将 赋值为
#include <bits/stdc++.h>
using namespace std;
long long n, q, d[100005], c[100005], r, v;
int main()
{
cin >> n >> q;
d[n + 1] = c[n + 1] = 8e18;
for (int i = 1; i <= n; i++)
cin >> d[i] >> c[i];
while (q--)
{
cin >> r >> v;
int ans = r;
for (int i = r + 1; i <= n + 1; i++)
{
if (v <= c[ans])
break;
if (d[i] > d[ans])
{
v -= c[ans];
ans = i;
}
}
cout << (ans > n ? 0 : ans) << "\n";
}
return 0;
}subtask2
对于 subtask2,每个圆盘的直径都是递增的,所以如果溢出,直接切换到下一个圆盘
#include <bits/stdc++.h>
using namespace std;
long long n, q, d[100005], c[100005], r, v, sum[100005];
int main()
{
cin >> n >> q;
d[n + 1] = c[n + 1] = 8e18;
for (int i = 1; i <= n; i++)
{
cin >> d[i] >> c[i];
sum[i] = sum[i - 1] + c[i];
}
sum[n + 1] = sum[n] + c[n + 1];
while (q --)
{
cin >> r >> v;
if (n <= 1000)
{
int ans = r;
for (int i = r + 1; i <= n + 1; i++)
{
if (v <= c[ans]) break;
if (d[i] > d[ans])
{
v -= c[ans];
ans = i;
}
}
cout << (ans > n ? 0 : ans) << "\n";
}
else
{
int ans = lower_bound(sum + r, sum + n + 2, v + sum[r - 1]) - sum;
cout << (ans > n ? 0 : ans) << "\n";
}
}
return 0;
}
subtask3
subtask3 就需要正解了
-
一个圆盘中的水往下流水,接住这些水的圆盘编号是确定的,就是向下第一个半径比向下流水的圆盘的半径更大的那一个,所以考虑用单调栈做
-
维护 表示 号圆盘向下流动 步区间内所有的圆盘容量之和,维护 表示 号圆盘向下流的 步到达的圆盘编号,注意:两个状态都不含 他本身
代码 dp[i][j] = dp[i][j - 1] + dp[nxt[i][j - 1]][j - 1]; nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; -
可以观察到 ,所以任何整数 都可以从高位到低位唯一一个确定的二进制数位
-
对于一个询问 ,倒序枚举指数 ,找到第一个 的 ,跳跃,并将 减去 ,继续枚举
-
最后往下移动一步才是答案
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, q;
int a[N], b[N], dp[N][25], nxt[N][25];
stack<int> stk;
void init()
{
for (int j = 1; (1 << j) <= n; j++)
{
dp[i][j] = dp[i][j - 1] + dp[nxt[i][j - 1]][j - 1];
nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
}
}
int f(int x, int y)
{
if (b[x] >= y)
return x;
y -= b[x];
for (int i = 19; i >= 0; i--)
if (nxt[x][i] != 0 && dp[x][i] < y)
{
y -= dp[x][i];
x = nxt[x][i];
}
return nxt[x][0];
}
int main()
{
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i] >> b[i];
for (int i = 1; i <= n; i++)
{
while (stk.empty() == 0 && a[i] > a[stk.top()])
{
dp[stk.top()][0] = b[i];
nxt[stk.top()][0] = i;
stk.pop();
}
stk.push(i);
}
init();
while (q--)
{
int r, v;
cin >> r >> v;
cout << f(r, v) << "\n";
}
return 0;
}P2564 [SCOI2009] 生日礼物
题意
给你 个种类,第 种彩珠有 个,然后再给出第 种 个的彩珠出现的坐标
,这第 种 个彩珠分别出现的位置
让你划出一个区间,要求这里面有所有类型的彩珠
求区间的最小长度
思路
-
先按坐标将彩珠排序
-
尺取法枚举左右边界
-
在枚举左右边界的同时统计此区间内的彩珠类型数量
-
当类型数量等于 时,更新答案
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
struct node
{
int pos, val;
} a[N];
int cnt[65];
bool cmp(node x, node y)
{
return x.pos < y.pos;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
int idx = 0;
for (int i = 1; i <= k; i++)
{
int t;
cin >> t;
while (t--)
{
int x;
cin >> x;
a[++idx] = {x, i};
}
}
sort(a + 1, a + n + 1, cmp);
int ans = 2e9, r = 0, tot = 0;
for (int l = 1; l <= n; l++)
{
while (r < n && tot < k)
{
r++;
cnt[a[r].val]++;
if (cnt[a[r].val] == 1) tot++;
}
if (tot == k)
ans = min(ans, a[r].pos - a[l].pos);
cnt[a[l].val]--;
if (cnt[a[l].val] == 0)
tot--;
}
cout << ans;
return 0;
}
总结
我这次没考好是因为我只复习了5月练习题单,没有做课堂上的题和作业题,接下来我会做:
-
我会将这个学期在课堂上的题和作业题都地毯式地再做、复习一遍
-
以后的学习过程中也会巩固,复习到位