Educational Codeforces Round 15
目录
A - Maximum Increase
一段上升子数组最大化 ,我们对于每个 做一个双指针即可
int n, a[N];
void solve()
{
cin>>n;
int res = 0;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++)
{
int j = i;
while(j + 1 <= n && a[j + 1] > a[j])
j++;
res = max(res, j - i + 1);
i = j;
}
cout<<res<<'\n';
return;
}
B - Powers of Two
用map记录下每个数字的出现次数,对于每个数字 枚举 ,即可求出 出现的次数,注意特判 的情况
const int N = 2e5 + 10;
ll n, a[N];
map<ll, ll> mp;
void solve()
{
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
mp[a[i]]++;
}
ll res = 0;
for(int i = 1; i <= n; i++)
{
ll base = 1;
ll x = a[i];
for(int i = 0; i <= 30; i++)
{
if(i != 0) base *= 2;
ll y = base - x;
res += mp[y];
if(x == y)
res--;
}
}
res /= 2;
cout<<res<<'\n';
return;
}
C - Cellular Network
将信号站和城市排序后,二分答案,二分过程做一个双指针操作即可
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
ll n, m, a[N], b[N];
bool check(ll r)
{
int i = 1;
for(int j = 1; j <= m; j++)
while(i <= n && abs(b[j] - a[i]) <= r)
i++;
if(i > n) return true;
return false;
}
void solve()
{
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
}
for(int i = 1; i <= m; i++)
{
cin>>b[i];
}
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
ll l = 0, r = 2e9;
while(l < r)
{
ll mid = (l + r) >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
cout<<l<<'\n';
return;
}
D - Road to Post Office
已知 ,从路程上我们有 3 种走法:
- 自行车
- 自行车 ,走路
- 自行车 ,走路
我们对这三种的修车次数分类讨论即可,具体见代码
ll d, k, a, b, t;
void solve()
{
cin>>d>>k>>a>>b>>t;
ll res = d * b;
ll c = d / k + (d % k != 0) - 1;
// cout<<c<<" "<<r<<'\n';+
res = min({d * a + c * t, res});
// 情况1
if(d % k != 0) c--;
res = min({max(0ll, c) * t + (d % k) * b + (max(0ll, c) + 1) * k * a, res});
// 情况2
res = min(a * min(k, d) + (d - min(k, d)) * b, res);
// 情况3
cout<<res<<'\n';
return;
}
E. Analysis of Pathes in Functional Graph
倍增 LCA
2023杭电多校第六场Calculate和这道题很像
倍增预处理即可
// AC one more times
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int LOGN = 33;
ll n, k;
ll e[N], w[N], fa[N][LOGN + 2], f[N][LOGN + 2], g[N][LOGN + 2];
void init()
{
for(int j = 1; j <= LOGN; j++)
for(int i = 1; i <= n; i++)
{
fa[i][j] = fa[fa[i][j - 1]][j - 1];
f[i][j] = f[i][j - 1] + f[fa[i][j - 1]][j - 1];
g[i][j] = min(g[i][j - 1], g[fa[i][j - 1]][j - 1]);
//cout<<j<<" "<<fa[i][j - 1]<<" "<<f[i][j - 1]<<" "<<g[i][j - 1]<<'\n';
}
}
void query(int u)
{
ll res = 0;
ll res2 = 1e9;
//cout<<k<<'\n';
for(ll i = LOGN; i >= 0; i--)
{
//cout<<i<<" "<<((k >> i) & 1)<<'\n';
if(k & (1ll << i))
{
//cout<<i<<" "<<fa[u][i]<<" "<<" "<<f[u][i]<<" "<<g[u][i]<<" "<<res<<'\n';
res = res + f[u][i];
res2 = min(g[u][i], res2);
u = fa[u][i];
}
}
cout<<res<<" "<<res2<<"\n";
}
void solve()
{
cin>>n>>k;
for(int i = 1; i <= n; i++)
{
cin>>e[i]; e[i]++;
fa[i][0] = e[i];
}
for(int i = 1; i <= n; i++)
{
cin>>w[i];
f[i][0] = w[i];
g[i][0] = w[i];
}
init();
for(int i = 1; i <= n; i++)
{
query(i);
}
//cout<<e[1]<<" "<<w[1]<<'\n';
return;
}
signed main()
{
std::ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
int TC = 1;
//cin >> TC;
for(int tc = 1; tc <= TC; tc++)
{
//cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
本文来自博客园,作者:magicat,转载请注明原文链接:https://www.cnblogs.com/magicat/p/17672888.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· Apache Tomcat RCE漏洞复现(CVE-2025-24813)