这其实是一道贪心的题目,然而官方数据很水导致n^2也能卡过
题目廊桥分配
答案要的是分配出的停放飞机的最大值,但是我们并不知道哪一种方法最优,所以我们可以把国内机场和国际机场分配x个廊桥时的最大停放飞机求出来,然后从分配机场数0到n枚举一遍求出最大值。
至于求解廊桥里的飞机停放数目,则可以建一个优先队列和一个廊桥数组来求解。
而优化代码则是建立两个优先队列,一个是为了存放能进入的廊桥的标号,一个是为了存放最后离开的时间,与一直到达时间作为比较。
点击查看代码
| #include<cstdio> |
| #include<iostream> |
| #include<cstring> |
| #include<algorithm> |
| #include<cstdlib> |
| #include<queue> |
| using namespace std; |
| |
| #define MA 100005 |
| |
| int n,m1,m2; |
| int pm1[MA]={0},tt1=0; |
| int pm2[MA]={0},tt2=0; |
| int en1[MA]={0},en2[MA]={0}; |
| int as1[MA]={0},as2[MA]={0}; |
| struct node1 { |
| int fr,to; |
| bool operator < (const node1 x)const { |
| return fr>x.fr; |
| } |
| }; |
| struct node2 { |
| int to,num; |
| bool operator < (const node2 x)const { |
| return to>x.to; |
| } |
| }; |
| priority_queue<node1>s1; |
| priority_queue<node2>s2; |
| bool cmp(int x,int y) { |
| return x>y; |
| } |
| void rd1(int m,int &tot) { |
| int i; |
| int fr,to; |
| |
| while(!s1.empty()) |
| s1.pop(); |
| |
| |
| |
| for(i=1;i<=m;++i) { |
| scanf("%d%d",&fr,&to); |
| s1.push((node1){fr,to}); |
| } |
| node1 x; |
| |
| |
| bool flag; |
| while(!s1.empty()) { |
| x=s1.top(); |
| s1.pop(); |
| |
| |
| for(i=1;i<=tot;++i) |
| if(x.fr>=en1[i]) { |
| en1[i]=x.to; |
| ++pm1[i]; |
| |
| break; |
| } |
| if(i>tot) { |
| ++tot; |
| ++pm1[tot]; |
| en1[tot]=x.to; |
| } |
| |
| } |
| |
| for(i=1;i<=tt1;++i) { |
| as1[i]=as1[i-1]+pm1[i]; |
| } |
| return; |
| } |
| void rd2(int m,int &tot) { |
| int i; |
| int fr,to; |
| |
| while(!s1.empty()) |
| s1.pop(); |
| |
| |
| |
| for(i=1;i<=m;++i) { |
| scanf("%d%d",&fr,&to); |
| s1.push((node1){fr,to}); |
| } |
| node1 x; |
| |
| |
| bool flag; |
| while(!s1.empty()) { |
| x=s1.top(); |
| s1.pop(); |
| |
| |
| for(i=1;i<=tot;++i) |
| if(x.fr>=en2[i]) { |
| en2[i]=x.to; |
| ++pm2[i]; |
| |
| break; |
| } |
| if(i>tot) { |
| ++tot; |
| ++pm2[tot]; |
| en2[tot]=x.to; |
| } |
| } |
| |
| for(i=1;i<=tt2;++i) { |
| as2[i]=as2[i-1]+pm2[i]; |
| } |
| return; |
| } |
| void print() { |
| int i; |
| puts(""); |
| for(i=1;i<=tt1;++i) { |
| printf("%d ",pm1[i]); |
| } |
| puts(""); |
| puts(""); |
| } |
| int main() { |
| |
| |
| |
| scanf("%d%d%d",&n,&m1,&m2); |
| |
| rd1(m1,tt1); |
| rd2(m2,tt2); |
| |
| int ans=0,i; |
| for(i=tt1+1;i<=n;++i) { |
| as1[i]=as1[tt1]; |
| } |
| for(i=tt2+1;i<=n;++i) { |
| as2[i]=as2[tt2]; |
| } |
| |
| |
| |
| |
| |
| for(i=0;i<=n;++i) { |
| ans=max(as1[i]+as2[n-i],ans); |
| |
| |
| } |
| printf("%d\n",ans); |
| return 0; |
| } |
优化代码
点击查看代码
| #include<cstdio> |
| #include<iostream> |
| #include<cstring> |
| #include<algorithm> |
| #include<cstdlib> |
| #include<queue> |
| using namespace std; |
| |
| #define MA 100005 |
| |
| int n,m1,m2; |
| int pm1[MA]={0},tt1=0; |
| int pm2[MA]={0},tt2=0; |
| int en1[MA]={0},en2[MA]={0}; |
| int as1[MA]={0},as2[MA]={0}; |
| struct node1 { |
| int to,fr; |
| }; |
| struct node2 { |
| int to,num; |
| bool operator < (const node2 x)const { |
| return to>x.to; |
| } |
| }; |
| priority_queue<node2>s1; |
| priority_queue< int,vector<int>,greater<int> >s2; |
| bool cmp(int x,int y) { |
| return x>y; |
| } |
| bool cmp2 (node1 x,node1 y) { |
| return x.fr<y.fr; |
| } |
| node1 y[MA]; |
| void rd(int m,int &tot,int *as) { |
| int i; |
| int fr,to; |
| |
| while(!s1.empty()) |
| s1.pop(); |
| while(!s2.empty()) |
| s2.pop(); |
| |
| for(i=1;i<=m;++i) { |
| scanf("%d%d",&y[i].fr,&y[i].to); |
| s2.push(i); |
| } |
| sort(y+1,y+1+m,cmp2); |
| int sz=0; |
| for(i=1;i<=m;++i) { |
| if(sz) { |
| while(sz && y[i].fr>s1.top().to) { |
| --sz; |
| s2.push(s1.top().num); |
| s1.pop(); |
| } |
| } |
| if(!s2.empty()) { |
| tot=max(tot,s2.top()); |
| ++as[s2.top()]; |
| s1.push((node2){y[i].to,s2.top()}); |
| s2.pop(); |
| ++sz; |
| } |
| } |
| for(i=1;i<=tot;++i) { |
| as[i]=as[i-1]+as[i]; |
| } |
| return; |
| } |
| void print() { |
| int i; |
| puts(""); |
| for(i=1;i<=tt1;++i) { |
| printf("%d ",pm1[i]); |
| } |
| puts(""); |
| puts(""); |
| } |
| int main() { |
| |
| |
| scanf("%d%d%d",&n,&m1,&m2); |
| |
| rd(m1,tt1,as1); |
| rd(m2,tt2,as2); |
| |
| int ans=0,i; |
| for(i=tt1+1;i<=n;++i) { |
| as1[i]=as1[tt1]; |
| } |
| for(i=tt2+1;i<=n;++i) { |
| as2[i]=as2[tt2]; |
| } |
| |
| |
| |
| for(i=0;i<=n;++i) { |
| ans=max(as1[i]+as2[n-i],ans); |
| } |
| printf("%d\n",ans); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~