rt
P14362 [CSP-S 2025] 道路修复 / road(官方数据)
赛场上脑子成啥了没场切…
首先注意到 ,容易想到 状压/子集枚举. 由于生成树保证连通性, 在原图中得到的最小生成树的 条边事有效的, 其余的边都是多余的, 因此我们做到将边数从 降到 . 得到 pts
优化 : 我们可以将 条边先离散化, 然后枚举的时候进行桶排. 注意 Kruscal 中并查集要把两种优化都打上
复杂度
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 10, MAXM = 1e6 + 10;
typedef long long ll;
inline ll read(){
ll x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
ll n, m, k, c[25], p[25][MAXN], tot, fa[MAXN], ans = 0x3f3f3f3f3f3f3f3f;
int siz[MAXN];
bool ok[25];
struct EdgeNode{
ll u, v, w;
}e1[MAXM], e2[MAXM], e3[MAXM];
void init(){
for(int i = 1; i <= n+k; i++)
fa[i] = i, siz[i] = 1;
for(int i = 1; i <= n-1; i++)
e3[i] = e2[i];
}
int findd(int u){
return u == fa[u] ? fa[u] : fa[u] = findd(fa[u]);
}
bool cmp(EdgeNode x, EdgeNode y){
return x.w < y.w;
}
void dfs(int id){
if(id == k){
init();
ll res = 0, tp = 0, as = 0;
// for(int i = 1; i <= k; i++){
// if(ok[i]){
// as++;
// res += c[i];
// for(int j = 1; j <= n; j++){
// e3[++tp] = (EdgeNode){n+i, j, p[i][j]};
// }
// }
// }
// sort(e3+1, e3+1+tp, cmp);
// cout << "debug : \n";
for(int i = 1; i <= k; i++)
if(ok[i]) res += c[i], as++;
for(int i = 1; i <= n-1+k*n; i++){
if(e2[i].u <= n || ok[e2[i].u-n]){
// if(ok[e2[i].u])
// cout << e2[i].u << ' ' << e2[i].v << ' ' << e2[i].w << endl;
e3[++tp] = e2[i];
}
}
// cout << "\n\n";
// N = n+as
int cnt = 0;
for(int i = 1; i <= tp; i++){
int u = e3[i].u, v = e3[i].v;
int fu = findd(u), fv = findd(v);
if(fu == fv) continue;
if(siz[fu] > siz[fv]){
fa[fv] = fu;
siz[fu] += siz[fv];
}else {
fa[fu] = fv;
siz[fv] += siz[fu];
}
cnt++;
res += e3[i].w;
if(cnt == n+as-1) break;
}
// cout << res << endl;
ans = min(ans, res);
return ;
}
ok[id+1] = true;
dfs(id+1);
ok[id+1] = false;
dfs(id+1);
}
int main(){
n = read(), m = read(), k = read();
for(int i = 1; i <= m; i++){
ll u = read(), v = read(), w = read();
e1[i] = (EdgeNode){u,v,w};
}
init();
sort(e1+1, e1+1+m, cmp);
for(int i = 1; i <= m; i++){
int u = e1[i].u, v = e1[i].v;
int fu = findd(u), fv = findd(v);
if(fu == fv) continue;
if(siz[fu] > siz[fv]){
fa[fv] = fu;
siz[fu] += siz[fv];
}else {
fa[fu] = fv;
siz[fv] += siz[fu];
}
e2[++tot] = e1[i];
if(tot == n-1) break;
}
// int ct = 0;
for(int i = 1; i <= k; i++){
c[i] = read();
for(int j = 1; j <= n; j++){
e2[++tot] = (EdgeNode){n+i, j, read()};
}
}
sort(e2+1, e2+1+tot, cmp);
dfs(0);
cout << ans;
return 0;
}
// 反思:这道题在考场上没做出来的主要原因是没有意识到有效边只有 n-1 条
// 其实这 trick 以前在 mx 上做过,类似于生成树只跑有效边
// 或许考场上应该先猜后证?
// 那放到最短路里也有类似的思路吗?
//
P11725 [JOIG 2025] 修学旅行 / School Trip
考虑到每次修改最多影响 个结果,直接模拟即可。
其实过程树是一棵 三叉线段树, 知道这个基本就没问题了.
n 叉线段树注意边界
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
char a[MAXN], sgt[10000010];
int n, lc[MAXN], mc[MAXN], rc[MAXN], pos[MAXN], tot, fa[MAXN];
void pushup(int u){
int ca = 0, cb = 0;
if(sgt[lc[u]] == 'A') ca++;
else cb++;
if(sgt[mc[u]] == 'A') ca++;
else cb++;
if(sgt[rc[u]] == 'A') ca++;
else cb++;
if(ca > cb) sgt[u] = 'A';
else sgt[u] = 'B';
}
void build(int u, int tL, int tR){
// cout << u << ' ' << tL << ' ' << tR << endl;
// _sleep(10);
if(tL == tR){
sgt[u] = a[tL];
pos[tL] = u;
return ;
}
int p1 = tL + (tR-tL+1) / 3;
int p2 = tL + (tR-tL+1) / 3 * 2;
lc[u] = ++tot;
build(lc[u], tL, p1-1);
mc[u] = ++tot;
build(mc[u], p1, p2-1);
rc[u] = ++tot;
build(rc[u], p2, tR);
fa[lc[u]] = fa[mc[u]] = fa[rc[u]] = u;
pushup(u);
}
void update(int id){
int u = pos[id];
if(sgt[u] == 'A') sgt[u] = 'B';
else sgt[u] = 'A';
while(~u){
u = fa[u];
if(u != -1)
pushup(u);
}
}
int main(){
ios :: sync_with_stdio(0);
int q;
cin >> n >> q;
n = pow(3, n);
for(int i = 1; i <= n; i++){
cin >> a[i];
}
tot = 1;
fa[1] = -1;
build(1, 1, n);
while(q--){
int x;
cin >> x;
update(x);
putchar(sgt[1]);
puts("");
}
return 0;
}P8945 Inferno
思路历程:
回顾 最大子段和 如何求:
一是 贪心/DP 视角, 不赘述
二是 前缀和 视角, 记 表示序列 的前 项和, 则某一段区间和就是 . 当固定右端点, 我们取 的前缀最小值, 就可以得到 的最大值, 复杂度
有了第二个视角, 我们来看题目:
有一个由 或 组成的长度为 的序列, 其中一些位置有空缺. 你需要往空缺的位置填 个 , 其余填入 , 需要最大化这个序列的最大子段和.
固定右端点 , 记 表示让 中有 个空缺的最大的 (其实这里最大最小都可), 表示前缀和, 表示空缺位置个数.
对于一个左端点 , 对其进行分类讨论:
如果 , 那么可以填满 个 , 则 , 显然我们只需要维护 的前缀最小值即可
如果 , 那么我们 个 用不完, 则 , 显然我们只需要维护 的区间最值即可
然而题目中 , 对 的维护我们无法使用线段树, 该怎么办呢 ?
注意这样一个性质: 是单增的, 也就是说我们询问的区间左右端点都是一直往右移的, 因此可以使用 单调队列 进行维护
复杂度
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e7 + 10;
typedef long long ll;
inline ll read(){
ll x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int n, k, a[MAXN], pre[MAXN], cnt[MAXN], f[MAXN], g[MAXN], p[MAXN];
int h[MAXN], q[MAXN], L, R;
int main(){
n = read(), k = read();
for(int i = 1; i <= n; i++){
a[i] = read();
pre[i] = pre[i-1] + a[i];
if(a[i] == 0) cnt[i] = cnt[i-1] + 1;
else cnt[i] = cnt[i-1];
// cout << cnt[i] << ' ';
}
// cout << endl;
for(int i = 1; i <= n; i++)
f[i] = pre[i] + cnt[i],
g[i] = pre[i] - cnt[i];
for(int i = 1; i <= n; i++){
if(cnt[i] >= k)
p[i] = h[cnt[i]-k]+1;
else p[i] = 1;
if(cnt[i] >= 0) h[cnt[i]] = i;
}
// for(int i = 1; i <= n; i++)
// cout << p[i] << ' ';
// cout << endl;
ll ans = 0;
pre[0] = 0x3f3f3f3f;
for(int i = 1; i <= n; i++)
pre[i] = min(pre[i-1], g[i]);
// for(int i = 1; i <= n; i++)
// cout << f[i] << ' ';
// cout << endl;
for(int i = 1; i <= n; i++){
ans = max(ans, 1ll * (g[i]-pre[p[i]-1] + 2*k));
}
for(int i = 1; i <= n; i++){
while(L <= R && q[L] < p[i])
L++;
ans = max(ans, 1ll * (f[i] - f[q[L]-1]));
// cout << f[i] - f[q[L]-1] << ' ' << i << ' ' << q[L] << endl;
while(L <= R && f[q[R]-1] >= f[i])
R--;
q[++R] = i;
}
cout << ans;
return 0;
}P5658 [CSP-S 2019] 括号树
先来考虑序列上怎么做:
设 表示以 为结尾的合法括号串个数, 记与 配对的位置为 , 那么显然有
表示 及之前可以选或不选
放到树上同理.
在序列上, 直接用栈求即可, 在树上也是.
但要注意的是, 在回溯过程中, 要把 栈 也还原回去.
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 10;
typedef long long ll;
ll n, f[MAXN], fa[MAXN], g[MAXN];
char a[MAXN];
vector <int> tr[MAXN];
struct stkNode{
ll id;
char c;
};
stack <stkNode> stk;
void debug(){
cout << "debug : " << endl;
stack <stkNode> s = stk;
while(!s.empty()){
cout << s.top().id << ' ' << s.top().c << endl;
s.pop();
}
cout << "\n\n";
}
void dfs(int u){
// cout << u << ":\n";
// debug();
int len = tr[u].size();
g[u] = f[u] + g[fa[u]];
for(int i = 0; i < len; i++){
int v = tr[u][i];
// if(v == fa[i]) continue;
stkNode tmp = (stkNode){-1,' '};
if(a[v] == '('){
stk.push((stkNode){v,a[v]});
}else {
// cout << v << " " << stk.size() << endl;
if(!stk.empty() && stk.top().c == '('){
// cout << "hym : " << v << "\n\n";
tmp = stk.top();
f[v] = f[fa[stk.top().id]] + 1;
stk.pop();
}
}
dfs(v);
if(a[v] == '(') stk.pop();
else if(tmp.id != -1) stk.push(tmp);
}
}
int main(){
ios :: sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 2; i <= n; i++){
cin >> fa[i];
tr[fa[i]].push_back(i);
}
// cout << 111;
// cout << 1111;
stk.push((stkNode){1, a[1]});
dfs(1);
ll ans = 0;
for(int i = 1; i <= n; i++)
ans ^= (g[i]*i);
// cout << g[i] << ' ';
cout << ans;
return 0;
} P5304 [GXOI/GZOI2019] 旅行者
好题
先直接说做法:
建立超极源汇点 , 枚举 个二进制位, 对于某一个关键点, 如果它这一位是 就和 连边, 否则与 连边,再跑 到 的单源最短路. 因为两个不同的点二进制位至少有一位不同, 因此这样 枚举/构造 后得到的答案一定能覆盖到答案最小的点对. 复杂度 .
实在是太巧妙了.
二进制分组其实体现了分组处理和二进制思想在算法设计中的强大威力.
分组处理: 其实可以对关键点进行 次随机分组, 在一定数值下可以跑出答案. 这样理解, 的话, 就会有 的下界是 .
也可以进行多进制分组, 这样组数少但单次处理更加复杂.
这东西我还在研究, 没找到更好的例题…
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
inline ll read(){
ll x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
ll n, m, k, dis[MAXN];
bool vis[MAXN];
struct ENode{
ll v, w;
};
vector <ENode> g[MAXN];
vector <int> ps;
struct PNode{
ll cur, dis;
bool operator < (const PNode &x) const {return this->dis < x.dis;}
bool operator > (const PNode &x) const {return this->dis > x.dis;}
};
priority_queue <PNode, vector<PNode>, greater<PNode> > pq;
void init(){
for(int i = 0; i <= n+1; i++)
vis[i] = false, dis[i] = 0x3f3f3f3f3f3f3f3f;
while(!pq.empty())
pq.pop();
}
void Dijkstra(){
init();
pq.push((PNode){0, 0});
dis[0] = 0;
while(!pq.empty()){
int u = pq.top().cur;
pq.pop();
if(vis[u]) continue;
// cout << "debug : " << u << endl;
vis[u] = true;
int len = g[u].size();
for(int i = 0; i < len; i++){
int v = g[u][i].v, w = g[u][i].w;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
pq.push((PNode) {v, dis[v]});
}
}
}
}
int main() {
// freopen("az.out", "w", stdout);
int T = read();
while(T--){
n = read(), m = read(), k = read();
for(int i = 1; i <= m; i++){
int u = read(), v = read(), w = read();
g[u].push_back((ENode) {v, w});
}
for(int i = 1; i <= k; i++)
ps.push_back(read());
ll ans = LONG_LONG_MAX;
for(int i = 0; i <= 19; i++){
g[0].clear();
g[n+1].clear();
for(int j = 0; j < k; j++){
int p = ps[j];
if(((p)&(1<<i))){
// cout << "1 : " << p << endl;
g[0].push_back((ENode){p, 0});
}
else {
// cout << "2 : " << p << endl;
g[p].push_back((ENode){n+1, 0});
}
}
// cout << 111111;
Dijkstra();
// for(int i = 0; i <= n+1; i++)
// cout << dis[i] << ' ';
// cout << endl;
// cout << dis[n+1] << endl;
ans = min(ans, dis[n+1]);
for(int j = 0; j < k; j++){
int p = ps[j];
// cout << "debug : " << ((p)&(1<<i)) << endl;
if(((p)&(1<<i)) == 0){
// cout << "Hhhhhhhhhyyyyyyyyyyymmmmmmmmmm : " << g[p].size() << endl;
g[p].pop_back();
}
}
}
for(int i = 0; i <= 19; i++){
g[0].clear();
g[n+1].clear();
for(int j = 0; j < k; j++){
int p = ps[j];
if(!((p)&(1<<i))){
// cout << "1 : " << p << endl;
g[0].push_back((ENode){p, 0});
}
else {
// cout << "2 : " << p << endl;
g[p].push_back((ENode){n+1, 0});
}
}
// cout << 111111;
Dijkstra();
// for(int i = 0; i <= n+1; i++)
// cout << dis[i] << ' ';
// cout << endl;
// cout << dis[n+1] << endl;
ans = min(ans, dis[n+1]);
for(int j = 0; j < k; j++){
int p = ps[j];
// cout << "debug : " << ((p)&(1<<i)) << endl;
if(((p)&(1<<i))){
// cout << "Hhhhhhhhhyyyyyyyyyyymmmmmmmmmm : " << g[p].size() << endl;
g[p].pop_back();
}
}
}
cout << ans << endl;
for(int i = 0; i <= n+1; i++)
g[i].clear();
ps.clear();
}
return 0;
}P3076 [USACO13FEB] Taxi G
P10977 Cut the Sequence
红温的一道题
首先 的转移式是好列的, 设 表示前 个的最小和, 则
其中 表示满足 最小的 .
因为 , 所以 是单调不减的, 因此可以用 前缀和+单调队列 进行预处理出来
注意到固定一个 , 是单调不减的, 是单调不增的, 因此我们要处理的 和 是两个单调性相反的函数
思考到这里我就不会了, 开始想能不能套数据结构 (例如线段树), 最后思考无果
solution: 我们取 变化时第一个变的点, 就例如 是 4 4 4 3 3 2, 我们就取第一个位置、第四个位置和第六个位置, 因为其他位置 和这些位置相同, 而 一定不优, 因此对于 的最优转移点肯定是这些位置.这个过程我们可以使用单调队列来维护.
口胡很容易, 实现很细节
对于现在单调队列中存在的点, , 我们拥有的可贡献的数值有 等等, 把他们放到 multiset 中就可
这样可能会漏掉 的情况, 记得加上.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
inline ll read(){
ll x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
ll n, K, a[MAXN], pre[MAXN], p[MAXN], dq[MAXN], l, r, f[MAXN];
multiset <ll> s;
bool check(){
ll mx = 0;
for(int i = 1; i <= n; i++)
mx = max(mx, a[i]);
return mx <= K;
}
int main(){
n = read(), K = read();
for(int i = 1; i <= n; i++){
a[i] = read();
pre[i] = pre[i-1] + a[i];
}
if(!check()){
puts("-1");
return 0;
}
dq[l=r=1] = 1;
p[1] = 1;
for(int i = 2; i <= n; i++){
while(l <= r && pre[i] - pre[dq[l]-1] > K)
l++;
dq[++r] = i;
p[i] = dq[l];
}
for(int i = 1; i <= n; i++) dq[i] = 0;
l = 1, r = 0;
for(int i = 1; i <= n; i++){
f[i] = f[i-1] + a[i];
while(l <= r && dq[l] < p[i]){
s.erase(f[dq[l]] + a[dq[l+1]]);
l++;
}
while(l <= r && a[dq[r]] < a[i]){
if(l < r)
s.erase(f[dq[r-1]] + a[dq[r]]);
r--;
}
dq[++r] = i;
if(l < r) s.insert(f[dq[r-1]] + a[dq[r]]);
f[i] = min(f[i], f[p[i]-1] + a[dq[l]]);
if(!s.empty())
f[i] = min(f[i], *s.begin());
}
cout << f[n];
return 0;
}P3509 [POI 2010] ZAB-Frog
意识到跳跃过程中有环状结构,考虑 倍增
记 表示点 往后跳 步所到达的位置, 显然有
问题难点在于如何求 . 可以将问题理解为定长为 的滑动窗口, 表示与 距离前 大的位置所在的区间, 容易得到 和 分别单调不减的性质, 因此容易使用单调队列维护.
需要注意的是, 数组如果开满会 MLE, 所以使用类似于快速幂的处理方式, 当然也可以理解为 滚动数组.
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
typedef long long ll;
inline ll read(){
ll x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
void write(ll x){
if(0 <= x && x <= 9) {putchar('0'+x); return ;}
write(x / 10);
putchar('0' + x%10);
}
ll n, k, m, a[MAXN];
int p[MAXN], ans[MAXN], tp[MAXN];
int main(){
n = read(), k = read(), m = read();
for(int i = 1; i <= n; i++)
a[i] = read();
int l = 1, r = k+1;
p[1] = k+1;
for(int i = 2; i <= n; i++){
while(r+1 <= n && a[r+1]-a[i] < a[i]-a[l]) l++, r++;
if(a[r]-a[i] > a[i]-a[l]){
p[i] = r;
}else {
p[i] = l;
}
}
for(int i = 1; i <= n; i++)
ans[i] = i;
for(ll j = 0; j <= 60; j++){
if((1ll<<j) & m){
for(int i = 1; i <= n; i++){
ans[i] = p[ans[i]];
}
}
for(int i = 1; i <= n; i++){
tp[i] = p[i];
}
for(int i = 1; i <= n; i++)
p[i] = tp[p[i]];
}
for(int i = 1; i <= n; i++){
write(ans[i]);
putchar(' ');
}
return 0;
}P3522 [POI 2011] TEM-Temperature
考虑一个 暴力, 一个区间 合法, 当且仅当对于任意 , 有
设对于一个 , 最小的合法的 为 , 则易得 是单调不减的
因此可以使用 双指针, 用 st表 再维护一个区间 RMQ
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
inline int read(){
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int n, a[MAXN], b[MAXN];
int st[MAXN][23];
int query(int l, int r){
int lg = log2(r-l+1);
return max(st[l][lg], st[r-(1<<lg)+1][lg]);
}
int main(){
n = read();
for(int i = 1; i <= n; i++)
a[i] = read(), b[i] = read(), st[i][0] = a[i];
for(int j = 1; j <= 20; j++){
for(int l = 1, r = (1<<j); r <= n; l++, r++)
st[l][j] = max(st[l][j-1], st[r-(1<<(j-1))+1][j-1]);
}
int ans = 0;
int l = 1, r = 1;
while(r < n){
r++;
while(l < r && query(l, r-1) > b[r]) l++;
ans = max(ans, r-l+1);
}
cout << ans;
return 0;
}注意到我们询问的区间左右端点分别单调, 因此也可以使用 单调队列 来维护 RMQ 于是得到线性复杂度做法
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
inline int read(){
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int n, a[MAXN], b[MAXN];
int dq[MAXN];
int main(){
n = read();
for(int i = 1; i <= n; i++)
a[i] = read(), b[i] = read();
int l, r;
dq[l=r=1] = 1;
int ans = 1;
for(int i = 1; i <= n; i++){
while(l <= r && b[i] < a[dq[l]]) l++;
if(l <= r) ans = max(ans, i-dq[l-1]);
else ans = max(ans, 1);
while(l <= r && a[dq[r]] <= a[i]) r--;
dq[++r] = i;
// cout << i << ' ' << dq[l] << sendl;
}
cout << ans;
return 0;
}P1772 [ZJOI2006] 物流运输
很显然的分组 DP
设 表示前 天所需要的最小代价, 则有
其中 表示第 天到第 天的有效边组成的图的最短路.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read(){
ll x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
ll n, m, K, ec, h[110][110], f[110];
ll dis[110];
bool vis[110];
struct ENode{
ll u, v, w;
}es[210];
struct EdgeNode{
ll v, w;
};
vector <EdgeNode> g[110];
bool ok[110][110];
void Dijkstra(){
for(int i = 1; i <= m; i++)
dis[i] = 0x3f3f3f3f, vis[i] = false;
dis[1] = 0;
for(int i = 1; i <= m; i++){
int u = -1, minn = 0x3f3f3f3f;
for(int j = 1; j <= m; j++)
if(minn > dis[j] && !vis[j])
u = j, minn = dis[j];
if(u == -1) break;
vis[u] = true;
int len = g[u].size();
for(int j = 0; j < len; j++){
int v = g[u][j].v, w = g[u][j].w;
dis[v] = min(dis[v], dis[u] + w);
}
}
}
int main(){
n = read(), m = read(), K = read(), ec = read();
for(int i = 1; i <= ec; i++){
es[i].u = read(), es[i].v = read(), es[i].w = read();
}
int _ = read();
while(_--){
int p = read();
int ap = read(), bp = read();
for(int i = ap; i <= bp; i++)
ok[p][i] = true;
}
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
for(int k = 1; k <= m; k++)
g[k].clear();
for(int k = 1; k <= ec; k++){
int u = es[k].u, v = es[k].v, w = es[k].w;
bool flag = false;
for(int p = i; p <= j; p++)
if(ok[u][p] || ok[v][p]){
flag = true;
break;
}
if(flag) continue;
g[u].push_back((EdgeNode){v, w});
g[v].push_back((EdgeNode){u, w});
}
Dijkstra();
h[i][j] = dis[m];
// cout << "debug : " << i << ' ' << j << ' ' << h[i][j] << endl;
}
}
memset(f, 0x3f, sizeof(f));
f[0] = 0;
f[1] = h[1][1];
for(int i = 2; i <= n; i++){
f[i] = i*h[1][i];
for(int j = 2; j < i; j++){
f[i] = min(f[i], f[j] + K + (i-j)*h[j+1][i]);
// cout << i << ' ' << j << ' ' << f[j] + K + (i-j)*h[j+1][i] << ' ' << endl;
}
// cout << "FFFFFF : " << i << ' ' << f[i] << endl;
}
cout << f[n];
return 0;
}P2365 任务安排
同样是一道分组 DP, 但是不能直接做.
不好的思路: 直接设 表示前 天的最小代价, 转移时, 发现前面分割了几次对当前会有影响, 因此得升维.
观察到每个点造成的代价为 , 其中 表示前 天分割的次数.
那么我们在位置 $s\times \sum_{j=i+1}^n t_j$, 把这个在每次分割时计算, 就可以避免对后面造成的影响.
启发: 当 DP 的状态会影响到后面时, 可以尝试在当前就计算完对后面的代价.
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5010;
typedef long long ll;
inline int read(){
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
ll n, s, t[MAXN], c[MAXN], st[MAXN], sc[MAXN], f[MAXN];
int main(){
n = read();
s = read();
for(int i = 1; i <= n; i++){
t[i] = read(), c[i] = read();
st[i] = st[i-1] + t[i];
sc[i] = sc[i-1] + c[i];
}
memset(f, 0x3f, sizeof(f));
f[0] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < i; j++){
f[i] = min(f[i], f[j] + s*(sc[n]-sc[j]) + (st[i])*(sc[i]-sc[j]));
}
}
cout << f[n];
return 0;
}P5017 [NOIP 2018 普及组] 摆渡车
以前打过, 用的斜率优化.
今天拿过来给学弟练拼包, 重新审视了一下有不同发现.
我的第一反应是这样的:
前把所有人按照等待时间排序, 设 表示前 个人的最小等待时间, 然而发现对于 来说, 不知道发车时间无法转移, 因此状态中需要带一个时间.
因此重新设计状态, 设 表示前 个时间的最小等待时间, 现在 时刻发车, 则有
其中 时来到时间在 的人
对于每个 , 西格玛中的 是常数, 因此把 提出来, 则有
时间复杂度
这是一个典型的分组 DP, 而与上述题目不同的是, 这道题每次分组没有任何代价, 因此转移点 一定在 以前是没有意义的, 因为我可以在此以前再跑一遍.
所以有效转移点 , 时间复杂度 , 足以通过此题.
需要注意的是, 因为 , 因此 是有意义的, 这一点极其重要.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 510, MAXT = 4e6 + 5;
ll n, m, t[MAXN], cnt[MAXT], sum[MAXT], lst = -1, f[MAXT + 100], q[MAXT];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> t[i],
cnt[t[i]]++, sum[t[i]] += t[i], lst = max(lst, t[i]);
for(int i = 1; i <= lst+m; i++)
cnt[i] += cnt[i-1], sum[i] += sum[i-1];
memset(f, 0x3f, sizeof(f));
f[0] = 0;
for(int i = 1; i <= lst+m; i++){
for(int j = max(i-2*m, 0ll); j <= max(i-m+1, 0ll); j++){
// cout << i << ' ' << j+1 << ' ' << (cnt[i]-cnt[j])*i - (sum[i]-sum[j]) << endl;
if(j-1 >= 0)
f[i] = min(f[i], f[j-1] + (cnt[i]-cnt[j-1])*i - (sum[i]-sum[j-1]));
else f[i] = min(f[i], (cnt[i])*i - (sum[i]));
}
// cout << f[i] << endl;
}
ll ans = 0x3f3f3f3f;
for(int i = 0; i < m; i++)
ans = min(ans, f[lst + i]);
cout << ans;
return 0;
} P11362 [NOIP2024] 遗失的赋值
去年击杀我.
今天我重生.
考虑 O(n) 做法, 一个点只有两个种类, 一个是在块上, 另一个是在块间.