codeforces 497c//Distributing Parts// Codeforces Round #283(Div. 1)

题意:有n个区间[ai,bi],然后有n个人落在[ci,di],每个人能用ki次。问一种方式站满n个区间。

两种区间都用先x后y的升序排序。对于当前的区间[ai,bi],将ci值小于当前ai的全部放入multiset。再lower_bound查第一个>=bi的就是答案。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
using namespace std;
const double EPS=1e-8;
const int SZ=4000050,INF=0x7FFFFFFF;
typedef long long lon;
struct nd{
    int a,b,k,id;
    bool operator<(const nd&rbs)const
    {
        if(a!=rbs.a)return a<rbs.a;
        else return b<rbs.b;
    }
};

struct cmp{
    bool operator()(const nd&x,const nd&y)const
    {
        return x.b<y.b;
    }
};

int ans[SZ];

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    int n;
    cin>>n;
    vector<nd> vct;
    for(int i=0;i<n;++i)
    {
        nd tmp;
        cin>>tmp.a>>tmp.b;
        tmp.id=i;
        vct.push_back(tmp);
    }
    sort(vct.begin(),vct.end());
    int m;
    cin>>m;
    vector<nd> peo;
    for(int i=0;i<m;++i)
    {
        nd tmp;
        cin>>tmp.a>>tmp.b>>tmp.k;
        tmp.id=i+1;
        peo.push_back(tmp);
    }
    sort(peo.begin(),peo.end());
    multiset<nd,cmp> st;
    bool ok=1;
    vector<int> res;
    for(int i=0,j=0;i<vct.size();++i)
    {
        nd cur=vct[i];
        for(;j<peo.size();++j)
        {
            if(peo[j].a<=cur.a)
            {
                st.insert(peo[j]);
            }
            else break;
        }
        vector<nd> mvd;
        //cout<<"sz: "<<st.size()<<endl;
        for(;!st.empty();)
        {
            if(st.lower_bound(cur)==st.end())
            {
                ok=0;
                break;
            }
            auto it=st.lower_bound(cur);
            nd top=*it;
            //cout<<" "<<i<<" "<<top.id<<" "<<top.k<<endl;
            st.erase(it);
            --top.k;
            if(top.k>=0)
            {
                st.insert(top);
            }
            else continue;
            res.push_back(top.id);
            ans[cur.id]=top.id;
            break;
                
        }
    }//
    if(ok&&n==res.size())
    {
        cout<<"YES"<<endl;
        for(int i=0;i<res.size();++i)
        {
            if(i)cout<<" ";
            cout<<ans[i];
        }
        cout<<endl;
    }
    else cout<<"NO"<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gaudar/p/9686842.html