【解题报告】Examination
Examination
本题解借鉴了这位dalao的思路
看上去这题没人交题解,那我就来一发吧(弥天大雾
题意
题目已经很简洁力
分析一下
一开始就不满足要求的话是肯定要交换的,我们用大根堆 A,B 分别存储一开始就不满足要求的
而一开始就满足要求的
考虑如何交换
对于一开始就不满足的人,其内部可以自己解决的话,一定会自己解决(贪心的想),即对于当前的
如果无法内部解决,说明我们需要原本合法的人的帮助,我们肯定是要用
AC code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<algorithm>
#define pii pair<int,int>
using namespace std;
const int maxn=3e5+5;
inline int read()
{
int w=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
w=(w<<3)+(w<<1)+(ch^48);
ch=getchar();
}
return w*f;
}
int n,x,y,ans;
priority_queue<pii,vector<pii>,less<pii> > q;
priority_queue<int,vector<int>,less<int> > A,B;
priority_queue<int,vector<int>,greater<int> > not_B;
int main()
{
n=read();
for(int i=1;i<=n;i++)
{
x=read(),y=read();
if(x<y) A.push(x),B.push(y);
else q.push(make_pair(x,y)),ans++;
}
while(!B.empty())
{
int k=B.top();B.pop();
if(!A.empty() && A.top()>=k)
{
A.pop();continue;
}
while(!q.empty() && q.top().first>=k)
{
not_B.push(q.top().second);q.pop();
}
if(not_B.empty()) cout<<-1,exit(0);
ans--,B.push(not_B.top()),not_B.pop();
}
cout<<ans;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误