社区讨论
求大佬讲解一下
P7913[CSP-S 2021] 廊桥分配参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lzp29oss
- 此快照首次捕获于
- 2024/08/11 12:24 2 年前
- 此快照最后确认于
- 2024/08/11 14:19 2 年前
贪心算法
CPP#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
struct hangban
{
int daoda;
int chufa;
bool nei;
};
/*void maopao(hangban today[],int n)
{
for(int i=0;i<n;i++)
{
bool b = false;
for(int j=0;j<n-i;j++)
{
if(today[j].daoda>today[j+1].daoda)
{
swap(today[j],today[j+1]);
b=true;
}
}
if(b==false)
{
break;
}
}
}*/
int maopao(hangban today[],int n)
{
int t=0;
for(int i=0;i<n;i++)
{
if(t<today[i].chufa)
{
t=today[i].chufa;
}
bool b = false;
for(int j=0;j<n-i;j++)
{
if(today[j].daoda>today[j+1].daoda)
{
hangban temp=today[j];
today[j]=today[j+1];
today[j+1]=temp;
b=true;
}
}
if(b==false)
{
break;
}
}
return t;
}
int todaytime(hangban today[],int l,int m1,int m2,int t)
{
int shu=0;
for(int nei=0;nei<=l;nei++)
{
int wai=l-nei;
int d=0;
int f=0;
int temp[100000]={0};
int t1=0;
int tag=0;
for(int i=0;i<t;i++)
{
if(today[d].daoda==i)
{
if(today[d].nei)
{
if(nei>0)
{
nei--;
tag++;
temp[t1]=today[d].chufa;
sort(temp,temp+t1-1);
t1++;
d++;
}
}
else
{
if(wai>0)
{
wai--;
tag++;
temp[t1]=today[d].chufa;
sort(temp,temp+t1-1);
t1++;
d++;
}
}
}
if(temp[f]==i)
{
if(today[d].nei)
{
temp[f]=-1;
nei++;
f++;
}
else
{
temp[f]=-1;
wai++;
f++;
}
}
}
if(tag>shu)
{
tag=shu;
}
}
return shu;
}
int main()
{
//freopen("airport.in","r",stdin);
//freopen("airport.out","w",stdout);
hangban today[100000];
int l=0;
int m1=0;
int m2=0;
cin>>l>>m1>>m2;
for(int i=0;i<m1;i++)
{
cin>>today[i].daoda>>today[i].chufa;
today[i].nei=true;
}
for(int i=m1;i<m1+m2;i++)
{
cin>>today[i].daoda>>today[i].chufa;
today[i].nei=false;
}
int t=maopao(today,m1+m2);
int tag=todaytime(today,l,m1,m2,t);
cout<<tag<<endl;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...