10.8考试总结
program_xwl · · 个人记录
T1 [USACO21DEC] Air Cownditioning B
赛时思路(正解)
显然每个突起的部分都是突起高度次操作解决,通过差分数组数据上升为正,下降为负的特性,将所有值为正的全部加起来就是答案。
赛时感觉初始会可能下降,把第一次负数也加上了,但显然他可以通过后面的正数填补它的下降贡献。
赛时代码(30pts )
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int n,p[N],q[N],a[N],diff[N],ans = 0;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++)
{
int x;
cin >> x;
a[i] -= x;
diff[i] = a[i]-a[i-1];
}
bool flag = 1;
for(int i = 1;i <= n;i++)
{
if(diff[i] != 0 && flag)
{
ans += abs(diff[i]);
flag = 0;
continue;
}
if(diff[i] > 0) ans += diff[i];
}
cout << ans;
return 0;
}
正解代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int n,a[N],b[N],ans = 0,lt = 0;
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++) cin >> b[i];
for(int i = 1;i <= n+1;i++)
{
int cha = a[i]-b[i];
ans += max(0,cha-lt);
lt = cha;
}
cout << ans;
return 0;
}
T2 求数列所有后缀最大值的位置
赛时思路(正解)
每个元素在加入时会产生一次贡献,用堆维护左边在产生贡献的元素,如果值小于它,则消除它的贡献。
赛时代码(AC)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
#define ull unsigned long long
struct node
{
ull x;
int id;
bool operator<(const node &b)const {return x>b.x;}
};
int n,ans = 0;
priority_queue<node>pq;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)
{
ull x;
cin >> x;
while(pq.size() && pq.top().x <= x)
{
ans ^= pq.top().id;
pq.pop();
}
pq.push({x,i});
ans ^= i;
cout << ans << '\n';
}
return 0;
}
T3 [CCC 2021 S5] Math Homework
AC
T4 [GESP202503 八级] 割裂
赛时思路(正解)
删除的点要让坏点对不连通,说明它必须在坏点对的路径上。
用树上差分求出好点对路径经过每个点的次数。枚举坏点对路径上的每一个点,如果其中一个点上经过的好点对路径为
赛时代码(0pts )
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+5,M = 1e5+5;
struct dui {int u,v;} a[M];
int n,m,bu,bv,fa_t[25][N],depth[N],ans = 0,diff[N];
bool vis[N];
vector<int>tree[N];
vector<int>point;
vector<int>hang_p[N];
bool dfs(int x,int fa)
{
bool is_important = 0;
if(x == bv) is_important = 1;
fa_t[0][x] = fa;
depth[x] = depth[fa]+1;
for(int i = 1;i <= 20;i++) fa_t[i][x] = fa_t[i-1][fa_t[i-1][x]];
for(auto v:tree[x])
{
if(v == fa) continue;
is_important |= dfs(v,x);
}
if(is_important) point.push_back(x);
return is_important;
}
int lca(int x,int y)
{
if(depth[x] < depth[y]) swap(x,y);
if(depth[x] > depth[y]) for(int i = 20;i >= 0;i--) if(depth[fa_t[i][x]] >= depth[y]) x = fa_t[i][x];
if(x == y) return x;
for(int i = 20;i >= 0;i--)
{
if(fa_t[i][x] != fa_t[i][y])
{
x = fa_t[i][x];
y = fa_t[i][y];
}
}
return fa_t[0][x];
}
void mark_path(int pid,int hpid)
{
dui hang = a[hpid];
int l = -1,r = pid+1;
while(l+1 < r)
{
int mid = (l+r)/2;
if(lca(point[mid],lca(hang.u,hang.v)) == point[mid]) r = mid;
else l = mid;
}
diff[r]++;
diff[pid+1]--;
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1;i < n;i++)
{
int u,v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
for(int i = 1;i <= m;i++) cin >> a[i].u >> a[i].v;
cin >> bu >> bv;
dfs(bu,0);
for(auto v:point) vis[v] = 1;
for(int i = 1;i <= m;i++)
{
int l = lca(a[i].u,a[i].v);
if(vis[l]) hang_p[l].push_back(i);
}
for(int i = 0;i < point.size();i++)
{
if(hang_p[point[i]].size() == 0) continue;
for(auto v:hang_p[point[i]]) mark_path(i,v);
}
for(int i = 1;i <= point.size();i++)
{
diff[i] += diff[i-1];
if(diff[i] == 0) ans++;
}
cout << ans;
return 0;
}
正解代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+5,M = 1e5+5;
struct dui {int u,v;} a[M];
int n,m,bu,bv,fa_t[25][N],depth[N],ans = 0,diff[N];
bool vis[N];
vector<int>tree[N];
vector<int>point;
bool dfs(int x,int fa)
{
bool is_important = 0;
if(x == bv) is_important = 1;
fa_t[0][x] = fa;
depth[x] = depth[fa]+1;
for(int i = 1;i <= 20;i++) fa_t[i][x] = fa_t[i-1][fa_t[i-1][x]];
for(auto v:tree[x])
{
if(v == fa) continue;
is_important |= dfs(v,x);
}
if(is_important) point.push_back(x);
return is_important;
}
int lca(int x,int y)
{
if(depth[x] < depth[y]) swap(x,y);
if(depth[x] > depth[y]) for(int i = 20;i >= 0;i--) if(depth[fa_t[i][x]] >= depth[y]) x = fa_t[i][x];
if(x == y) return x;
for(int i = 20;i >= 0;i--)
{
if(fa_t[i][x] != fa_t[i][y])
{
x = fa_t[i][x];
y = fa_t[i][y];
}
}
return fa_t[0][x];
}
void dfs2(int x,int fa)
{
for(auto v:tree[x])
{
if(v == fa) continue;
dfs2(v,x);
diff[x] += diff[v];
}
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1;i < n;i++)
{
int u,v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
for(int i = 1;i <= m;i++) cin >> a[i].u >> a[i].v;
cin >> bu >> bv;
dfs(bu,0);
for(auto v:point) vis[v] = 1;
for(int i = 1;i <= m;i++)
{
int l = lca(a[i].u,a[i].v);
diff[a[i].u]++;
diff[a[i].v]++;
diff[l]--;
diff[fa_t[0][l]]--;
}
dfs2(bu,0);
for(auto v:point)
if(diff[v] == 0) ans++;
cout << ans;
return 0;
}