题意:
定义一个航班的起飞序号为该航班在起飞序列中的位置,即是第几个起飞的航班。 起飞序列还存在两类限制条件: 第一类(最晚起飞时间限制):编号为 i的航班起飞序号不得超过 ki; 第二类(相对起飞顺序限制):存在一些相对起飞顺序限制(a, b),表示 航班 a的起飞时间必须早于航班 b,即航班 a的起飞序号必须小于航班 b 的起飞序号。 小X 思考的第一个问题是,若给定以上两类限制条件,是否可以计算出一个 可行的起飞序列。第二个问题则是,在考虑两类限制条件的情况下,如何求出每 个航班在所有可行的起飞序列中的最小起飞序号。
题解:
题意是保证有解的。
另一道差不多的题是2535。
容易想到二分+check
考虑怎么判断一个k序列是否有解。
将第二个限制建成反图,拓扑排序,每次将k值最大的点填在起飞序列最后,如果有不合法的,那么就挂了。
显然这样是最优的。
但是会超时。
观察下,可以发现不用二分,只要当前处理的点尽量后出队就可以了。
code:
#include<utility>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
#define pa pair<int,int>
struct edge{
int y,next;
}a[10010];int len=0,last[2010];
int n,m,K[2010],d[2010],rd[2010];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*f;
}
void ins(int x,int y)
{
a[++len].y=y;
a[len].next=last[x];last[x]=len;
}
void check(int X)
{
int now=n;
priority_queue<pa> q;
for(int i=1;i<=n;i++) rd[i]=d[i];
for(int i=1;i<=n;i++)
if(rd[i]==0) q.push(make_pair(K[i],i));
while(!q.empty())
{
pa T=q.top();q.pop();
int x=T.second;
if(x==X&&!q.empty())
{
pa t=q.top();
if(t.first>=now) q.pop(),q.push(T),T=t;
}
if(T.second==X) {printf("%d ",now);return;}
now--;
x=T.second;
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;rd[y]--;
if(rd[y]==0) q.push(make_pair(K[y],y));
}
}
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++) K[i]=read();
for(int i=1;i<=m;i++)
{
int x,y;scanf("%d %d",&x,&y);
ins(y,x);d[x]++;
}
for(int i=1;i<=n;i++) check(i);
}