Distributing Parts CodeForces - 496E

You are an assistant director in a new musical play. The play consists of n musical parts, each part must be performed by exactly one actor. After the casting the director chose m actors who can take part in the play. Your task is to assign the parts to actors. However, there are several limitations.

First, each actor has a certain voice range and there are some parts that he cannot sing. Formally, there are two integers for each actor, ci and di (ci ≤ di) — the pitch of the lowest and the highest note that the actor can sing. There also are two integers for each part — aj and bj (aj ≤ bj) — the pitch of the lowest and the highest notes that are present in the part. The i-th actor can perform the j-th part if and only if ci ≤ aj ≤ bj ≤ di, i.e. each note of the part is in the actor's voice range.

According to the contract, the i-th actor can perform at most ki parts. Besides, you are allowed not to give any part to some actors (then they take part in crowd scenes).

The rehearsal starts in two hours and you need to do the assignment quickly!

Input

The first line contains a single integer n — the number of parts in the play (1 ≤ n ≤ 105).

Next n lines contain two space-separated integers each, aj and bj — the range of notes for the j-th part (1 ≤ aj ≤ bj ≤ 109).

The next line contains a single integer m — the number of actors (1 ≤ m ≤ 105).

Next m lines contain three space-separated integers each, ci, di and ki — the range of the i-th actor and the number of parts that he can perform (1 ≤ ci ≤ di ≤ 109, 1 ≤ ki ≤ 109).

Output

If there is an assignment that meets all the criteria aboce, print a single word "YES" (without the quotes) in the first line.

In the next line print n space-separated integers. The i-th integer should be the number of the actor who should perform the i-th part. If there are multiple correct assignments, print any of them.

If there is no correct assignment, print a single word "NO" (without the quotes).

Example
Input
3
1 3
2 4
3 5
2
1 4 2
2 5 1
Output
YES
1 1 2
Input
3
1 3
2 4
3 5
2
1 3 2
2 5 1
Output
NO

题意:这里有一些歌剧和演员,每个演员有一个声域L,R,并且这个演员只可以出演k个歌剧,每个歌剧也需要一个声域至少覆盖L,R的演员才能出演,现在要求每个歌剧都要有一个演员去演请问该怎么分配,


第一眼看上去,我擦,网络流,二分图最大匹配,一看数据范围,发现不对了.......仔细想了想,是个贪心

思路如下:首先让歌剧和演员都按照他们的音域的R来排序,然后逐个考虑演员,把所有R小于当前考虑的演员的音域都加入set里面,然后找到L最接近演员L的,并让这个演员出演这个歌剧,如果找不到,就考虑下一个演员

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
struct node{
    int l,r,pos,k;
};
node part[maxn],actor[maxn];
bool cmp(node a,node b){
    return a.r<b.r;
}
int ans[maxn];
set<pair<int,int> >s;
int main(){
    s.clear();
    memset(ans,0,sizeof(ans));
    int n,m;
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&part[i].l,&part[i].r);
        part[i].pos=i;
    }
    cin>>m;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&actor[i].l,&actor[i].r,&actor[i].k);
        actor[i].pos=i;
    }
    sort(part+1,part+1+n,cmp);
    sort(actor+1,actor+1+m,cmp);
    int i=1,j=1,ok=0;
    while(j<=m){
        if(i<=n && part[i].r<=actor[j].r){
            s.insert(make_pair(part[i].l,part[i].pos));
            i++;
        }
        else{
            set<pair<int,int> >::iterator it;
            it=s.lower_bound(make_pair(actor[j].l,0));
            if(it==s.end()){
                if(j==m && s.size()>0){//如果s不为空,并且最后一个演员没有歌剧演说明扔到s里面的某些歌剧没人演
                    ok=1;
                    break;
                }
                else{
                    j++;
                    continue;
                }
            }
            ans[it->second]=actor[j].pos;
            s.erase(it);
            actor[j].k--;
            if(actor[j].k==0)j++;//只有当这个演员不能再用的时候再考虑下一个演员
        }
    }
    for(int i=1;i<=n;i++)if(ans[i]==0)ok=1;
    if(!ok){
        cout<<"YES"<<endl;
        cout<<ans[1];
        for(int i=2;i<=n;i++)printf(" %d",ans[i]);
        cout<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值