【长期不定期更新】杂题做题记
junhaowang · · 算法·理论
众所周知,迄今为止我一共有三个储存代码的仓库(都在我的博客也就是文章区里面):
- 常见算法代码库(已经更新了很多内容了)
- 动态规划做题记(更新可能比较慢但是充满脑汁!)
- 还有这个杂题记(才刚发布)
一些其他的题目可以去上面三个看看哦!
在这个题单里,所谓的“杂题”要满足以下几个条件:
- 并没有什么明显的算法(否则就放到算法代码库里去了)
- 需要自己想专门的思路,但是可能又想不到思路
- 题目的类型很杂,策略很杂
像这种题,要想应付也不容易,于是一些代码和详解(史上最详细)就放在这里了。
2024.07.09 PSN102359
由于 PSNOJ 的特性,这道题并没有链接,但是可以提供该题目的简化题面。
现有
//PSN102359牛牛吃草,用二维数组存储数据并适当地预处理
//啥算法都不是,一道想法题,但是用到了一些策略(所以我也放在这里了)
//我来阐述一下简化的题意:有n个序列,对于在1~m之间的任意整数t,
//求其中任意序列a(长度为k)的第(t mod k)个元素的最大值和序列编号
//(重要备注:如果余数是0则替换成k,下同)
//补充说明:如果有多个序列编号,输出更小的那个
//预处理是解题的关键:对于k确定以及(t mod k)也就是序列中的位置确定,则最大值确定
//就是这个思路有点难想到……想到了之后相对好理解(这里简化的题目描述有点抽象了,没办法)
//思路理解了问题就不是很大了,解析非常详细!!!史上最详细
#include <bits/stdc++.h>
#pragma GCC optimize (2)
using namespace std;;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;;
int
n,
m,
ans, //每个t的临时最大元素
pos, //每个t的临时序列编号
curpos, //存储(t mod k)即序列中的位置
len [100005], //表示k
tmp, //直接开数组存序列不可行(太大了),这里也不需要(我们只关心最大值)
//因此可以临时输入临时处理最大值数组
mx [1005][1005], //存最大的序列元素用于判断最大(开大点准没错)
mxnum [1005][1005] //存第几个序列(序列的编号)
//第一个下标表示k,第二个下标表示(t mod k)即序列中的位置,求最大值
;;
//理解变量名就好
signed main (void)
{
ios::sync_with_stdio (false);;
cin.tie (0), cout.tie (0);;
cin >> n >> m;;
//预处理
for (rint i = 1; i <= n; i++)
{
cin >> len [i];;
for (rint j = 1; j <= len [i]; j++)
{
cin >> tmp;;
if (tmp > mx [len [i]][j]) //更新最大值和编号
{
mx [len [i]][j] = tmp;;
mxnum [len [i]][j] = i;;
}
}
}
for (rint i = 1; i <= m; i++) //遍历每个t
{
ans = 0;; //找最大就初始化成0,这里没有负数
//重点来了:下面遍历的是所有的k
for (rint j = 2; j <= 10; j++) //题目提及k<=10
{
curpos = (i - 1) % j + 1;; //计算(t mod k),注意对余数0的处理
//这样加减之后就不会出现余数是0的情况,计算出最终值
if (mx [j][curpos] > ans) //更新最大值和最大的序列编号
{
ans = mx [j][curpos];;
pos = mxnum [j][curpos];;
}
else if (mx [j][curpos] == ans) //更新最小编号
{
pos = min (pos, mxnum [j][curpos]);;
}
}
cout << pos << ' ';;
}
return 0;;
}
2024.07.15 P1981
//P1981求只含加、乘的表达式的值,考察栈的应用
//策略:第一个数字单独拎出来,然后后面的看做一符一数组合
//加号直接入栈,乘号取栈顶相乘然后放回去,最后栈内累加
//特殊的方法:直接cin读入,读数字的时候碰到符号就不读的
#include <bits/stdc++.h>
//#pragma GCC optimize (2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int num, tp, ans;
char tmp;
stack <int> st;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> num; //头上的数字
st.push(num);
while (cin >> tmp >> num) //一个符号一个数字
{
switch (tmp)
{
case '+': //加号直接入栈等待后续处理
st.push(num % 10000);
break;
case '*': //乘号就处理栈顶元素
tp = st.top() % 10000;
st.pop();
st.push(tp * num % 10000);
}
}
while (!st.empty())
{
ans += st.top() % 10000;
ans %= 10000;
st.pop();
}
return cout << ans % 10000, 0;
}
2024.07.16 PSN102401
有两种做法,第二种做法更好理解,还标注了调了很久的大 bug!都贴上来了。
//PSN102401,也就是弱化版的P3115,纯模拟但容易绕晕
//这个是第一种做法,第二种做法在后面
//这一道题只允许转乘1次(也可以不转),就是单向的乘车问题
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int a, b, n, w[505], route[505][505], ans = 0x3f3f3f3f3f3f3f3f;
void find(int cur /*第一趟列车*/, int start /*中转站*/, int cost /*第一趟列车(原车)的费用*/);
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> a >> b >> n;
for (rint i = 1; i <= n; i++)
{
cin >> w[i] >> route[i][0]; //route[i][0]表示当前车的站数
//w[i]表示当前车的票价
for (rint j = 1; j <= route[i][0]; j++)
{
cin >> route[i][j]; //输入站
}
}
for (rint i = 1; i <= n; i++) //首先是直达的情况
{
bool flag = false;
for (rint j = 1; j <= route[i][0]; j++) //枚举每个站(注意车是单向的)
{
if (route[i][j] == a) //如果到起点就true
{
flag = true;
}
else if (flag && route[i][j] == b) //否则如果到终点就更新答案
{
ans = min(ans, w[i]);
}
}
}
//接下来是转车的情况(很复杂)
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= route[i][0]; j++)
{
if (route[i][j] == a) //到起点了
{
for (rint k = j + 1; k <= route[i][0]; k++) //枚举之后的点作为中转站
{
find(i, route[i][k], w[i]); //传参计算,参数含义在前面解释了
}
}
}
}
return cout << (ans == 0x3f3f3f3f3f3f3f3f ? -1 : ans), 0; //如果没更新就是-1
}
void find(int cur, int start, int cost) //参数含义在前面解释过
{
for (rint k = 1; k <= n; k++) //枚举第二趟列车
{
if (k == cur) //如果正好是第一趟列车,就不用枚举了
{
continue;
}
for (rint l = 1; l <= route[k][0]; l++) //枚举车站
{
if (route[k][l] == start) //如果到中转站
{
for (rint m = l + 1; m <= route[k][0]; m++) //枚举是否能到达终点
{
if (route[k][m] == b) //如果能到达终点
{
ans = min(ans, cost + w[k]); //更新答案
}
}
}
}
}
return ;
}
//PSN102401的第二种做法(这是我一开始想到的做法,好理解一点)
//前面都是一样的,后面实现上略有区别
//这个逻辑很简单:对于每个车,都进行遍历,先判断直达
//然后再把每个点作为中转站继续写两重循环,原理一样
//本质区别不大,但是这个程序逻辑更清晰一点!
//注:有两个地方之前错了,调了很久,下面会注明“警告”二字
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int a, b, n, w[505], route[505][505], ans = 0x3f3f3f3f3f3f3f3f;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> a >> b >> n;
for (rint i = 1; i <= n; i++)
{
cin >> w[i] >> route[i][0]; //route[i][0]存储站数,w[i]票价
for (rint j = 1; j <= route[i][0]; j++)
{
cin >> route[i][j]; //输入车站
}
}
//小花絮:另外一种做法里直达的代码是从这种做法的代码复制过去的,懒得写
for (rint i = 1; i <= n; i++)
{
bool flag = false; //flag记录的是前面是否到达过起点,注意车子不能逆行
for (rint j = 1; j <= route[i][0]; j++) //遍历每一站
{
//下面一个if-else if是直达的情况,再下面if(flag)是转车
if (route[i][j] == a) //到了就记录一下
{
flag = true;
}
else if (flag && route[i][j] == b) //如果到终点了,而且前面到过起点,就更新
{
ans = min(ans, w[i]);
}
if (flag) //前面到过起点,还可以再选一辆车进行中转
//【警告】这个if(flag)一开始没加,这是错误之一。
{
for (rint k = 1; k <= n; k++) //遍历第二辆车
{
if (k == i) //不能同一辆车
{
continue;
}
bool flag2 = false; //同理,记录是否达到过中转点
for (rint l = 1; l <= route[k][0]; l++) //遍历车站
{
if (route[k][l] == route[i][j]) //看看是否到达中转点
//【超级警告】这个是主要的错误,不能直接比较下标l和j,要比较站点
//比较列车用下标,但是比较车站不能用下标,不要混淆
{
flag2 = true;
}
else if (flag2 && route[k][l] == b) //经过中转点到了终点
{
ans = min(ans, w[i] + w[k]); //更新两趟车的总票价
}
}
}
}
}
}
return cout << (ans < 0x3f3f3f3f3f3f3f3f ? ans : -1), 0; //没更新就是-1
}
2024.07.17 PSN102402
找规律题,代码含金量不高,主要在思路,此处就写写大致思路。
题目提到数轴上把最大或最小的点移到另外两个元素的中间,求最多和最少几次能使三个点两两相邻。
- 最小情况: 最多
2 次一定能归位,每一次移动使它们尽可能相邻即可。若有距离为2 则1 次,已经满足要求则0 次。 - 最大情况: 先花1次让两个点相邻,然后“滚动”,保持它们距离始终为1直到滚到第三个点。滚动距离(两点间距离)最大则次数最大,设位置之差为
x ,次数:1+(x-1-1)=x-1 。其中1 是让两点相邻,x-1 是两个位置之间的数目,再-1 是因为让两点相邻占掉一个位置
手模一下即可。
2027.07.17 P1965
这道题也就不贴代码了,讲讲思路。
有一个长度为
我们在小学五年级学过余数的若干性质:
- 可加(减)性:
(a\pm b)\bmod p=(a\bmod p\pm b\bmod p)\bmod p - 可乘性:
ab\bmod p=(a\bmod p)(b\bmod p)\bmod p - 可乘方性:
a^b\bmod p=(a\bmod p)^b\bmod p - 慎除
那么我么又可以想方设法地展开:
虽然现在非常恶心,但是已经不至于这么大了。
什么?你觉得
2024.07.17 PSN102323
//PSN102323,有若干种物品,每种物品有一个重量区间和价值
//每个物品有无数个,求有多少种不同的价值使得质量和为w
//这是一道DFS题目,但是需要正确地处理这些数据(这就是这道题的精华!)
//就一个字:勇(老子很富,全都给我搜!)
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
struct coin //定义结构体保持应有的参数
{
int value, weight;
}
coins[505];
int w, n, tmp1 /*价值*/, tmp2 /*最小重量*/,
tmp3 /*最大重量*/, tmp /*coin顶端下标*/, ans;
bool vis[505]; //题目说计面值而不是情况数,因此开个vis统计
void dfs(int step /*即将考虑的硬币(这是模板)*/, int sumWeight, int sumValue)
//这里后面两个参数是用来累加的(这也是模板)
{
if (step == tmp + 1) //已经超出去了
{
return; //不同于其他题目,这道题直接退出即可。为啥?看下面。
}
if (sumWeight == w) //满足题目要求
{
//既然满足了,那就更新一波vis并更新答案
ans += !vis[sumValue]; //人话:如果vis[sumValue]是false就+1
vis[sumValue] = true;
return ;
}
if (sumWeight > w) //如果超了直接剪掉
{
return ;
}
//题目说硬币顶多50个,那就枚举一波呗
for (rint i = 0; i <= 50; i++)
{
dfs(step + 1, sumWeight + i * coins[step].weight,
sumValue + i * coins[step].value);
}
return ;
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> w >> n;
for (rint i = 1; i <= n; i++)
{
cin >> tmp1 >> tmp2 >> tmp3;
for (rint j = tmp2; j <= tmp3; j++) //这里我们可以把区间内所有重量加入
{
coins[++tmp].weight = j;
coins[tmp].value = tmp1;
}
}
dfs(1, 0, 0);
return cout << ans, 0;
}
2024.08.07 WLH2813
//WLH2813是用指针的一道题,统计不具有重复字符的最长子串长度
//不是很难,但是要想到用指针记录可能的起始点
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x < < endl;
string s;
int ans, lt; //lt表示当前子串的起始位置
unordered_map <char, int> lst; //记录字符最后出现的位置
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> s;
for (rint i = 0; i < s.size(); i++)
{
if (lst.find(s[i]) == lst.end() || lst[s[i]] < lt)
//没有出现过这个字符或者出现在lt的左侧不影响,不用动,可以更新
{
ans = max(ans, i - lt + 1);
}
else
{
//否则就要把lt搬过来使他不再碰到这个字符了(不然就重复了)
lt = lst[s[i]] + 1;
}
lst[s[i]] = i; //更新位置
}
return cout << ans, 0;
}
2024.08.07 WLH2818
//WLH2818也是指针!求序列中长度不超过m的最大子串和
//这题比上一题复杂哦
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int n, m, a[300005], prefix[300005], pos, ans; //prefix是前缀和
//其实在这里也不止是单纯的前缀和了,表示了这一个点为终点的最大子串和
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (rint i = 1; i <= n; i++)
{
cin >> a[i];
prefix[i] = prefix[i - 1] + a[i];
if (i - pos + 1 > m) //区间长度已经超了
{
prefix[i] -= a[pos]; //只能直接把那个给去掉咯
pos++; //往后挪一个
}
//从下面开始,要开始删掉前面<0的部分了(越删越大嘛)
pos--;
while (a[++pos] <= 0 && pos <= i)
{
prefix[i] -= a[pos];
}
if (prefix[i] < 0) //小于0还不如不选呢,再删
{
prefix[i] = 0;
pos = i + 1;
}
ans = max(ans, prefix[i]);
}
return cout << ans, 0;
}
2024.08.07 ZMOI111
两种剪枝方法!
//ZMOI111,每个物品在编号为1~n的项目中发挥不同的价值
//每个项目选择一个物品使得价值之和最大
//这道题用DFS+剪枝可以得到97pts,正解好像要用状压?
//剪枝两种思路,一种:直接用一个数减去价值,改为求最小
//从而大于当前最小值剪枝
//也可以在假设之后的都用最优的策略之后仍然达不到当前最大值的时候剪枝
//反正都没法儿AC(bushi)
//此处演示方法一
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
const int k = 1005; //用这个k减去每个值
int n, value[1005][1005], a[1005], ans = numeric_limits <int> ::max();
bool vis[1005];
inline void dfs(int step, int tot)
{
if (step == n + 1)
{
return void(ans = min(ans, tot));
}
if (tot > ans)
{
return void();
}
for (rint i = 1; i <= n; i++)
{
if (!vis[i])
{
vis[i] = true;
a[step] = i;
dfs(step + 1, tot + value[step][i]);
vis[i] = false;
}
}
return void();
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= n; j++)
{
cin >> value[i][j];
value[i][j] = k - value[i][j];
}
}
dfs(1, 0);
//输出的时候别忘了转回来啊!
//S=(k-a1)+(k-a2)+...+(k-an)
// =nk-(a1+a2+...+an)
//因此a1+a2+...+an=nk-S
return cout << n * k - ans, 0;
}
//ZMOI111,每个物品在编号为1~n的项目中发挥不同的价值
//每个项目选择一个物品使得价值之和最大
//这道题用DFS+剪枝可以得到97pts,正解好像要用状压?
//剪枝两种思路,一种:直接用一个数减去价值,改为求最小
//从而大于当前最小值剪枝
//也可以在假设之后的都用最优的策略之后仍然达不到当前最大值的时候剪枝
//反正都没法儿AC(bushi)
//此处演示方法二
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int n, value[1005][1005], maxv[1005], prefix[1005], ans, a[1005];
bool vis[1005];
//maxv表示每个物品的最大价值,用于计算最优方法
//prefix是最优情况下的前缀和数组
//实际往往不能让每个物品都发挥最大价值(可能重复)
//但是这样也可以剪枝(不然连剪枝都要额外的复杂度……)
//毕竟这个结果比计划要大,如果连这个都达不到当前值就更不行了
inline void dfs(int step, int tot)
{
if (step == n + 1)
{
return void(ans = max(ans, tot));
}
//下面剪枝:如果后面都最优还达不到当前最大值,剪枝
if (tot + prefix[n] - prefix[step - 1] <= ans)
{
return void();
}
for (rint i = 1; i <= n; i++)
{
if (!vis[i])
{
a[step] = i;
vis[i] = true;
dfs(step + 1, tot + value[step][i]);
vis[i] = false;
}
}
return void();
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= n; j++)
{
cin >> value[i][j];
maxv[i] = max(maxv[i], value[i][j]);
}
prefix[i] = prefix[i - 1] + maxv[i]; //前缀和
}
dfs(1, 0);
return cout << ans, 0;
}
2024.08.08 WLH5374
//WLH5374是一道数学题!有点双指针的思想在
//两个人在每分钟可以分别发出时间间隔相同的攻击a和b次打怪兽
//单人打造成1个伤害,同时攻击称为爆击造成4个伤害
//求怪兽打死之前最后一击是谁给的?
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int n, a, b;
struct fraction
{
int numerator, denominator;
fraction(int a, int b) : numerator(a), denominator(b) {}
};
//先重载一下分数的运算
bool operator > (struct fraction a, struct fraction b)
{
return a.numerator * b.denominator > b.numerator * a.denominator;
}
bool operator == (struct fraction a, struct fraction b)
{
return a.numerator * b.denominator == b.numerator * a.denominator;
}
bool operator < (struct fraction a, struct fraction b)
{
return a.numerator * b.denominator < b.numerator * a.denominator;
}
int i = 1, j = 1; //i和j表示当前攻击第几次,用于计算攻击时刻
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> a >> b;
while (n > 0) //怪兽还活着
{
//定义攻击时间
struct fraction atime = fraction(i, a), btime = fraction(j, b);
if (atime == btime) //攻击时间相等
{
n -= 4; //爆击
i++, j++; //进入到下一次攻击
if (n <= 0) //怪兽打趴了(注意爆击有可能打到负数,不能!n)
{
return cout << 'C', 0; //是一起打的
}
}
else if (atime < btime) //a早攻击
{
n--; //攻击
i++; //a早攻击之后指针往后挪
if (!n) //怪兽打趴了(攻击一点一点打,不会打到负的)
{
return cout << 'A', 0;
}
}
else //一个套路
{
n--, j++;
if (!n)
{
return cout << 'B', 0;
}
}
}
return 0;
}
2024.08.12 ZMOI1069
这是比赛题,不能直接进去。
//ZMOI1069说的是有t次操作,操作可以是:
//在序列开头增加、 输出开头并删除、把序列复制一份放在末尾
//这道题的插入操作本身可以用STL实现,但是有一点优化:抓住数据组数!
//有些数其实永远不会被查询到!
//我们发现最后一个操作非常的慢,因此如果当前长度大于等于剩余查询,
//就可以不用复制,因为即使后面全部都是-1也不会超过,复制了也没用
//具体实现的时候细节很多!
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int t, op;
deque <int> que; //双端队列
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) //这样子t的值就是剩下的数据组数
{
cin >> op; //操作
switch (op > 0 ? 1 : op)
//题目说正数都表示第一个操作,输入要插入的值
//那就变成1方便switch(如果不要switch那就随便)
{
case 1:
que.push_front(op);
break;
case -1:
//注意如果为空不执行操作
if (!que.empty())
{
cout << que.front() << endl;
que.pop_front();
}
break;
case 0:
//先优化,当前长度小于t必须复制以应对全是-1的情况
//要全复制吗? 也不用,复制到t就好,后面的都不可能访问
if (que.size() < t)
{
//注:deque支持直接访问元素
int tmp = que.size(); //但是长度会改变,存一个比较好
//下面注意啦注意啦!!!下标从0开始的
for (rint i = 0; i < min(tmp, t - tmp); i++)
{
que.push_back(que[i]);
}
}
}
}
return 0;
}
2024.08.12 WLH3701
//WLH3701是一道优先队列的应用
//是汽车加油问题,加k单位的油可以走k米,有一个初始油量
//求的是最小的加油次数
//策略是:如果当前油还是有的,就暂时不加这个加油站
//其实以后如果用完了还是可以再给它加上的,每经过一个加油站就加入优先队列
//然后尝试到下一个加油站,如果油不够了,那就在优先队列里取最大值加上
//遍历所有的加油站,按照这种思路走一遍就可以
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
struct station
{
int pos /*距离*/, capacity /*油量*/;
station (int pos, int capacity) : pos(pos), capacity(capacity) {}
//下面重载运算符用于排序(也可以用cmp)
bool operator < (const struct station &other) const
{
return pos < other.pos;
}
//然后是等于符号
bool operator == (const struct station &other) const
{
return pos == other.pos && capacity == other.capacity;
}
};
vector <struct station> stations; //加油站数组(脑子一热用了个vector呵呵)
priority_queue <int> que; //优先队列
int totdis /*行驶总里程*/, initial /*初始油量*/, ans /*答案计数器*/,
num /*加油站数量*/, cur /*行驶过程中当前的油量*/, tmp1, tmp2;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> totdis >> initial >> num;
cur = initial;
for (rint i = 0; i < num; i++)
{
cin >> tmp1 >> tmp2;
stations.push_back(station(tmp1, tmp2));
}
sort(stations.begin(), stations.end());
for (rint i = 0; i < num; i++) //遍历加油站
{
//入队
que.push(stations[i].capacity);
//减走过的距离,如果是第一个加油站就是这个位置,否则减一减
cur -= !i ? stations[i].pos : stations[i].pos - stations[i - 1].pos;
while (!que.empty() && cur <= 0) //如果不够就加油
{
cur += que.top(); //加最多的油
que.pop();
ans++;
}
//全加完了还是不够,那就没办法啦
if (cur <= 0)
{
return cout << -1, 0;
}
}
//别忘了最后还要走到终点!一个套路
cur -= totdis - stations[num - 1].pos;
while (!que.empty() && cur < 0) //值得注意的是:终点的时候0是合法的!
{
cur += que.top();
que.pop();
ans++;
}
return cout << (cur < 0 ? -1 : ans), 0;
}
2024.08.12 P1141
//P1141是一道搜索的题目,地图中只可以从1和0之间互跳
//必须要注意到这题的最核心思想:多组数据之间,块重复的点可以传递
//也就是说,在同一连通块中的所有点都应该存储同一个数据!
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int n, m, mp[1005][1005], f[1005][1005], x, y,
ans[100005], go[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
char tmp;
inline bool judge(int x, int y, int val)
{
return x >= 1 && x <= n && y >= 1 && y <= n
&& !~f[x][y] && mp[x][y] != val;
//注意如果之前已经访问过了,就不用再访问了
}
//val为当前位置的值,cur表示当前询问的序号
inline void dfs(int x, int y, int val, int cur)
{
for (rint i = 0; i < 4; i++)
{
int xx = x + go[i][0], yy = y + go[i][1];
if (judge(xx, yy, val))
{
f[xx][yy] = cur; //标记当前位置属于哪个询问,以后可以查阅
ans[cur]++; //计数
dfs(xx, yy, mp[xx][yy], cur);
}
}
return void();
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(f, -1, sizeof(f)); //别忘了初始化
cin >> n >> m;
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= n; j++)
{
cin >> tmp;
mp[i][j] = tmp - 48;
}
}
for (rint i = 1; i <= m; i++)
{
cin >> x >> y;
if (~f[x][y])
{
ans[i] = ans[f[x][y]]; //如果已经有答案了,那么就可以直接获取
}
else
{
//否则dfs更新一下答案
//千万,千万,不要忘了起点的初始化!之前vis也是这样的
f[x][y] = i;
ans[i] = 1;
dfs(x, y, mp[x][y], i);
}
}
for (rint i = 1; i <= m; i++)
{
cout << ans[i] << endl; //输出答案
}
return 0;
}
2024.08.14 WLH5651
两种方法:递推方法和排列组合方法。
//WLH5651,一共有n个苹果和梨头(原题用了“牡牛”和“牝牛”,但是字太生僻……)
//其中每两个苹果之间至少有k个梨头
//求总的方案数。本来我用插空法+插板法排列组合做的,
//但是这里也有一种递推的做法,就也讲一讲
//题目要求取模,没事多模模,对身体好(bushi)
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int mod = 5000011;
int n, k, m[100005], p[100005];
//m[i]表示第i个位置放苹果的方案数,p是梨头
//(但是数组名还是沿用了原题的“牡牛”和“牝牛”……)
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> k;
m[1] = p[1] = 1; //放一个都是可行的
for (rint i = 2; i <= n; i++) //下面进行递推
{
//如果放梨头,那么没有限制,前面随便放啥
p[i] = (p[i - 1] % mod + m[i - 1] % mod) % mod;
//下面有一个问题:前面放不放得下一个苹果?这对后面的策略有影响
if (i - k >= 2) //可以自己推一推,这样隔了k个,前面一个放得下
{
m[i] = p[i - k] % mod; //也就是在这个位置放梨头的方案数
}
else //否则放不下,那就是沿用下来
{
m[i] = m[i - 1] % mod;
}
}
//最后是苹果梨头加起来
return cout << (m[n] % mod + p[n] % mod) % mod, 0;
}
2024.08.14 WLH5653
//WLH5653,一共有n个盘子,每个盘子顶多放一个苹果,
//但是不能连续放m个苹果,这样会爆炸(???),求方案数
//这也是一道递推的题目!
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
int n, m, dp[55];
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
dp[0] = 1, dp[1] = 2; //边界条件
for (rint i = 2; i <= n; i++)
{
if (i < m) //这样子肯定是不会炸的!随便放
{
dp[i] = dp[i - 1] * 2;
}
else if (i == m) //这样子只要前面没有放满都可以放
{
dp[i] = dp[i - 1] * 2 - 1;
}
else //第三个就难咯
{
dp[i] = dp[i - 1] * 2 - dp[i - m - 1];
//考虑第i个坑能不能放苹果
//不考虑新的爆炸,总的方案数还是dp[i-1]*2
//怎么样会炸呢?这个盘子前面(m-1)个盘子全部放着苹果
//那么它前面第m个盘子肯定是没有放的,不然连着m个被前面抵消
//所以说它前面第(m+1)个盘子,也就是第1个到第(i-m-1)个的盘子,
//这些盘子无论怎么放都不会影响,也就是这个位置的方案数
}
}
return cout << dp[n], 0;
}
2024.10.18 ZMOI1415(题目不对外开放)
由于题目是内部题,我阐述一下题面:在我的世界中有一个
//ZMOI1415,也是多源BFS和广搜路径长度相关题目
//多源BFS说白了就是先把一堆起点一起放进去一起搜索
//由于广搜的广度优先特性,广搜可以自动统计距离(根据搜索到的顺序)
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 3e3 + 5, go[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n, m, k, q, tmp1, tmp2, ans, lastcountry[maxn][maxn] /*上次访问过的国家*/, dis[maxn][maxn];
bool notliveable[maxn][maxn], vis[maxn][maxn], isboundary[maxn][maxn] /*是否国境线*/;
pair <int, int> capital[maxn];
queue <pair <int, int> > que;
inline bool judge(int xx, int yy);
inline void bfs(void);
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(dis, 0x3f, sizeof dis);
cin >> n >> m >> k;
for (rint i = 1; i <= k; i++)
{
cin >> capital[i].first >> capital[i].second;
}
cin >> q;
for (rint i = 1; i <= q; i++)
{
cin >> tmp1 >> tmp2;
notliveable[tmp1][tmp2] = true;
}
bfs();
return cout << ans, 0;
}
inline bool judge(int xx, int yy)
{
return xx >= 1 && xx <= n && yy >= 1 && yy <= n;
}
inline void bfs(void)
{
for (rint i = 1; i <= k; i++)
{
que.push(capital[i]);
//除了push和vis,别忘了初始化lastcountry啊!
lastcountry[capital[i].first][capital[i].second] = i;
}
while (!que.empty())
{
auto p1 = que.front();
int x = p1.first, y = p1.second;
que.pop();
for (rint i = 0; i < 4; i++)
{
int xx = p1.first + go[i][0], yy = p1.second + go[i][1];
if (!vis[xx][yy] && !notliveable[xx][yy] && judge(xx, yy))
{
if (!lastcountry[xx][yy] || dis[xx][yy] > dis[x][y] + 1)
//如果还没有被国家达到过或者距离更短就更新
{
dis[xx][yy] = dis[x][y] + 1;
//下面更新国家,问题是这个isboundary怎么更新呢?这是点睛之笔!
lastcountry[xx][yy] = lastcountry[x][y];
//下面我们来解释一下,假设A和B首都都相同距离到了点(x,y)也就是isboundary[x][y]==true
//那么我们再走一步(xx,yy)的时候,A和B距离(xx,yy)的距离都增加1,当然也是国境线
//同理原来不是国境线那么现在也不是了
isboundary[xx][yy] = isboundary[x][y];
que.push(make_pair(xx, yy));
}
else if(lastcountry[xx][yy] != lastcountry[x][y] && dis[xx][yy] == dis[x][y] + 1)
//这个国家已经被访问过,而且这次访问居然距离一样,就是国境线了
{
isboundary[xx][yy] = true;
lastcountry[xx][yy] = lastcountry[x][y];
}
}
}
}
for (rint i = 1; i <= n; i++) //国境线统计
{
for (rint j = 1; j <= m; j++)
{
if (isboundary[i][j])
{
ans++;
}
}
}
return void();
}
2024.10.27 WLH3579
//WLH3579,也是单调栈的比较复杂的应用
//题目大意是这样的:
//奶牛的声音能被距离他左边、右边最近的比他高的奶牛听到
//求听到的声音最大的奶牛
//核心思想:这是个单调递增栈,左边比右边大
//现在来了一头很大的奶牛,把栈顶弹掉了,栈顶能听到什么呢?
//一个是把栈顶弹掉的那头牛,还有就是这头牛本来的左边也比它大!
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 5e4 + 5, inf = 0x3f3f3f3f3f3f3f3f;
int n, ans, volume[maxn] /*奶牛听到的声音*/;
struct cow
{
int height, volume /*奶牛发出的声音*/;
}
cows[maxn];
inline void getans(void);
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (rint i = 1; i <= n; i++)
{
cin >> cows[i].height >> cows[i].volume;
}
//单调栈老套路,塞一个大的值让所有东西都出去
cows[++n].height = inf;
getans();
for (rint i = 1; i < n; i++)
{
ans = max(ans, volume[i]);
}
return cout << ans, 0;
}
inline void getans(void)
{
//维护一个单调递增栈
stack <int> sta;
for (rint i = 1; i <= n; i++)
{
//如果空或者当前更加小,就进去
if (sta.empty() || !sta.empty()
&& cows[sta.top()].height > cows[i].height)
{
sta.push(i);
continue;
}
//否则就开始弹出
while (!sta.empty() && cows[sta.top()].height < cows[i].height)
{
int tmp;
//注意!注意!下面要做一个特殊的处理:把相同高度的奶牛合并!
//为什么?因为它们互相听不见,其他牛如果能听见也一起听见它们!
while (!sta.empty())
{
tmp = sta.top();
sta.pop();
if (!sta.empty() && cows[sta.top()].height == cows[tmp].height)
{
cows[sta.top()].volume += cows[tmp].volume;
}
else
{
break;
}
}
//那么现在tmp指向的就是刚刚被弹出的元素下标了
//当然我们现在考虑它左边的牛比它高听到它的声音
if (!sta.empty() && cows[sta.top()].height > cows[tmp].height)
{
volume[sta.top()] += cows[tmp].volume;
}
volume[i] += cows[tmp].volume; //此外
}
sta.push(i); //最后进去
}
return void();
}
2024.10.27 WLH3029
这题解析不是很详细,毕竟单调队列可以看看我的博客里算法代码库嘛。
题目大意:序列里选择一个长度在
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 1e5 + 5, inf = 0x3f3f3f3f3f3f3f3f;
int prefix[maxn], n, p, q, tmp, ans = -inf;
deque <int> deq;
inline void getans(void);
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
//思路:前缀和,区间和prefix[r]-prefix[l-1],减少prefix[l-1]
cin >> n >> p >> q;
for (rint i = 1; i <= n; i++)
{
cin >> tmp;
prefix[i] = prefix[i - 1] + tmp;
}
getans();
return cout << ans, 0;
}
inline void getans(void)
{
for (rint i = p; i <= n; i++) //起码是p
{
//去尾
while (!deq.empty() && prefix[deq.back()] >= prefix[i - p])
{
deq.pop_back();
}
deq.push_back(i - p);
//删头
while (!deq.empty() && deq.front() < i - q)
{
deq.pop_front();
}
ans = max(ans, prefix[i] - prefix[deq.front()]);
}
return void();
}
2024.12.21 P6111
//P6111是一道并查集的问题,可以看成并查集求连通块,(n-1)条边
//只不过在一个询问里这道题边权>=k才算是连通
//这道题是k越大越优先处理,因为这样要求严格
//最后k渐渐变小,要求降低,增加边即可!
//如何快速找出>=k的边?降序排列嘛!
//这个并查集当然还要带上一个大小的属性,这是带权并查集!
//复习一下,权是要在合并的时候进行运算的!
//我博客里《算法代码库》应该有带权并查集的!
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 5e3 + 5;
int n, q, k, v, pos /*从前往后挪,寻找<k的第一条边,k更小的时候往后挪*/ = 1,
fa[maxn], ans[maxn] /*查询的答案*/, sze[maxn] /*根对应的连通块大小*/;
struct edge
{
int u, v, w; //方便根据k一条条加边
}
edges[maxn];
struct query
{
int k, v, id; //排序后通过编号恢复顺序
}
queries[maxn];
//查询按k从大到小排序
inline bool cmpquery(struct query a, struct query b)
{
return a.k > b.k;
}
//边按权值从大到小排序
inline bool cmpedge(struct edge a, struct edge b)
{
return a.w > b.w;
}
inline int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
//合并的时候更新sze
inline void merge(int x, int y)
{
//注意合并之后根就变掉了,提前保存起来!
int xx = find(x), yy = find(y);
fa[xx] = yy;
//别搞反了,y才是最终老大
sze[yy] += sze[xx];
return void();
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> q;
for (rint i = 1; i < n; i++)
{
cin >> edges[i].u >> edges[i].v >> edges[i].w;
}
sort(edges + 1, edges + 1 + n, cmpedge);
for (rint i = 1; i <= q; i++)
{
cin >> queries[i].k >> queries[i].v;
queries[i].id = i;
}
sort(queries + 1, queries + 1 + q, cmpquery);
//并查集必须初始化!
for (rint i = 1; i <= n; i++)
{
fa[i] = i;
//还要初始化sze哟
sze[i] = 1;
}
for (rint i = 1; i <= q; i++)
{
--pos;
//不要忘了:pos永远是要判断越界的!
while (++pos <= n - 1 /*注意边数(n-1)条*/
&& edges[pos].w >= queries[i].k)
{
//边是满足要求了,那就纳入进去,合并!
merge(edges[pos].u, edges[pos].v);
}
//注意!这里sze访问的应该是根!!!
ans[queries[i].id] = sze[find(queries[i].v)];
}
for (rint i = 1; i <= q; i++)
{
cout << ans[i] - 1 << endl; //注意注意!减去起点!!!
}
return 0;
}
2024.12.21 P3416
//P3416,平面直角坐标系,已知每个点能往外走的范围半径energy
//特殊地,信号是可以中转的,
//每头奶牛可以传递信号给若干奶牛,求这个值的最大值
//这道题是求多源,所以用什么?Floyd呀!
//事实上,可以存储一个点是否能到达其他点(不用管权值),跑Floyd
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 2e2 + 5;
int n, tmp, ans;
bool able[maxn][maxn]; //能否走通
struct node
{
int x, y, energy;
}
cow[maxn];
inline int calcdis(struct node a, struct node b) //老套路,比较平方不会有误差
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (rint i = 1; i <= n; i++)
{
cin >> cow[i].x >> cow[i].y >> cow[i].energy;
}
//遍历能否走到
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= n; j++)
{
able[i][j] = calcdis(cow[i], cow[j]) <= cow[i].energy * cow[i].energy;
}
}
//跑一遍Floyd算法寻找中转点
for (rint k = 1; k <= n; k++)
{
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= n; j++)
{
able[i][j] = able[i][j] || able[i][k] && able[k][j];
}
}
}
//统计
for (rint i = 1; i <= n; i++)
{
tmp = 0;
for (rint j = 1; j <= n; j++)
{
tmp += able[i][j];
}
ans = max(ans, tmp);
}
return cout << ans, 0;
}
2024.12.21 P3611
//P3611一道二分的题目,但是排序的时候加了一个冒泡优化
//只要看这个优化的地方,剩下的了解即可
//和加水问题差不多,选择一个时间最短的队伍排进去
//已知总时间求水龙头最小数量
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 1e4 + 5;
int n, tot, t[maxn], l = 1, r, mid, ans, tmp[maxn];
inline int calc(int k) //水龙头数目
{
for (rint i = 1; i <= k; i++) //先拷贝进去前k个人
{
tmp[i] = t[i];
}
sort(tmp + 1, tmp + 1 + k);
for (rint i = k + 1; i <= n; i++)
{
if (tmp[k] > tot) //不能超时间,超时间随便返回一个比tot大的
//只要让while条件变成true就可以,啥数都行
{
return tot + 1;
}
tmp[1] += t[i]; //到最短的队伍排进去
//sort会TLE,使用冒泡思想,因为变更的只有tmp[1],所以把它往后扔
//只要一轮就可以了
for (rint j = 2; j <= k; j++)
{
if (tmp[j] >= tmp[j - 1]) //这样说明已经冒泡到位了
{
break;
}
swap(tmp[j], tmp[j - 1]);
}
}
return tmp[k];
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> tot;
r = n;
for (rint i = 1; i <= n; i++)
{
cin >> t[i];
}
while (l <= r)
{
mid = l + r >> 1;
if (calc(mid) > tot)
{
l = mid + 1;
}
else
{
ans = mid;
r = mid - 1;
}
}
return cout << ans, 0;
}
2025.03.15 P1631
//P1631,有长度相等(为n)的单调不降序列,
//两个序列各挑一个元素得到和(n^2个)
//求这里面最小的n个和
//这个程序的整体思想就是:
//通过大根堆把最大的元素暴露出来,
//这样子每次替换掉最大的元素越来越小。
//把和a[i]+b[j]放入优先队列,
//没有n个就先放进去挤满n个再说!
//如果已经n个就和队首比较:
//如果比队首更小就弹掉队首再加进去(更小的替换)
//否则就结束枚举,进入a[i+1]+a[j]
//这个是为什么呢?现在就已经更大了,
//后面的肯定比队首更大,更更新不了,干脆这一行不看了
//看懂这个下面的程序就不难!
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 1e5 + 5;
int n, a[maxn], b[maxn];
vector <int> vec;
priority_queue <int> que;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (rint i = 1; i <= n; i++)
{
cin >> a[i];
}
for (rint i = 1; i <= n; i++)
{
cin >> b[i];
}
for (rint i = 1; i <= n; i++)
{
for (rint j = 1; j <= n; j++)
{
int tmp = a[i] + b[j];
if (que.size() < n)
{
que.push(tmp);
}
else
{
if (tmp < que.top())
{
que.pop(), que.push(tmp);
}
else
{
break;
}
}
}
}
for (rint i = 1; i <= n; i++)
{
vec.push_back(que.top()), que.pop();
}
for (rint i = vec.size() - 1; i >= 0; i--)
{
cout << vec[i] << ' ';
}
return 0;
}
2025.03.15 P1801
//P1801需要用优先队列来实现哦!
//是说有加入元素、输出第++i小的元素的功能
//首先我们假设放入一个元素都会重新排序
//那么我们找到的i就是一个“切割点“
//关键是优先队列只能输出头部的元素啊!怎么办呢?
//所以说我们不妨直接切成两个数组,切出两个头
//也就是说左边是一个大根堆,右边是一个小根堆
//那么两个“头”的状况就是这个样子的:
//xxxxxOoxxxxx
//++i的时候一个头扔出来,另一个队列把头接住
//这样子第i小的元素就放在切出来的“头”上面了!
#include <bits/stdc++.h>
//#pragma GCC optimize (2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 2e5 + 5;
int m, n, a[maxn], cnt, lst /*从哪个下标开始继续处理*/ = 1, cur /*当前切割点*/;
priority_queue <int> que1;
priority_queue <int, vector <int>, greater <int> > que2;
unordered_map <int, int> mp;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> m >> n;
for (rint i = 1; i <= m; i++)
{
cin >> a[i];
}
for (rint i = 1; i <= n; i++)
{
cin >> cur;
for (rint j = lst; j <= cur; j++)
{
que1.push(a[j]); //第一个队列吃掉一个元素
if (que1.size() == i + 1) //已经满足到
{
que2.push(que1.top()), que1.pop();
//第一个队列丢出一个头被第二个队列吃掉,移动切割点
}
}
lst = cur + 1; //接下来要放的元素下标
cout << que1.top() << endl;
if (!que2.empty())
{
que1.push(que2.top()), que2.pop();
}
}
return 0;
}
2025.03.15 P1168
//P1168求数列任意前奇数项的中位数
//这时候我们不妨采用动态的策略,持续更新
//每次都排序是不可取的,先排个序观察,一看就知道:
//我们又可以使用刚才的两个优先队列头对头的方式了!
//切出一个头来,左边的小于中位数,右边的大于中位数
//左边大根堆,右边小根堆,可以随时访问到切割点的数值
//需要注意的是这里是奇数项,所以不妨把中位数拿出来
//这样子只要两个优先队列长度相等,就是中位数找到了
//像这样子不断改变切割点,就动态更新中位数了
//也就是:xxxxxxO.oxxxxxx(.是中位数,Oo是两个头)
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define int long long
#define rint register int
#define endl '\n'
#define debug(x) cerr << #x << '=' << x << endl;
constexpr int maxn = 1e5 + 5;
int n, a[maxn], mid;
priority_queue <int> que1;
priority_queue <int, vector <int>, greater <int> > que2;
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> a[1];
mid = a[1]; //一开始只有一个元素,初始化好
cout << mid << endl;
for (rint i = 2; i <= n; i++)
{
cin >> a[i];
//分队啦:大的踢到右边,小的踢到左边
if (a[i] > mid)
{
que2.push(a[i]);
}
else
{
que1.push(a[i]);
}
if (i & 1) //奇数才要查询
{
while (que1.size() != que2.size()) //未平衡
{
//下面就是看怎么平衡:左边大就往右边挪,反之亦同
//看看这个:xxxxxxO.oxxxxxx
//也就是说队列的头以及中位数,三个都是需要移动的!
if (que1.size() > que2.size())
{
que2.push(mid), mid = que1.top(), que1.pop();
}
else
{
que1.push(mid), mid = que2.top(), que2.pop();
}
}
//平衡就输出吧
cout << mid << endl;
}
}
return 0;
}