URAL 1424. Minibus (贪心+multiset+multimap+线段树)

题意:一辆车,从1点到n点,车上只有m个位置,载一个客人p块钱。承载哪些客人可以使收入最大。最大是多少,输出这些人。

可以中贪心。按下车的先后顺序排列。尽量让客车满载。

第八组数据大概长这副摸样:

10 3 3 1
1 3
1 3
1 3

使用了各种stl啊。。。。


#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <string>

#define LL long long
#define DB double
#define SI(a) scanf("%d",&a)
#define SD(a) scanf("%lf",&a)
#define SS(a) scanf("%s",a)
#define SF scanf
#define PF printf
#define MM(a,v) memset(a,v,sizeof(a))
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define REPD(i,a,b) for(int (i)=(a);(i)>(b);(i)--)
#define N 100009
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define BUG puts("bug")
#define PP pair<int,int>
using namespace std;
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
int n,m,k,p;
multiset<PP> st;
priority_queue<int, vector<int>, greater<int> > que;
multimap<PP,int> mp;
vector<int> L;
int tree[N<<2];
int wt[N<<2];
void build()
{
    MM(tree,0);
    MM(wt,0);
}
void pushd(int rt)
{
    if(wt[rt])
    {
        wt[rt<<1] += wt[rt];
        wt[rt<<1|1] += wt[rt];
        tree[rt<<1] += wt[rt];
        tree[rt<<1|1] += wt[rt];
        wt[rt] = 0;
    }
}
void pushu(int rt)
{
    tree[rt] = max(tree[rt<<1],tree[rt<<1|1]);
}
int query(int rt,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return tree[rt];
    }
    pushd(rt);
    int m = (l+r)>>1,ans =0;
    if(L<=m) ans = max(ans,query(lson,L,R));
    if(R>m) ans = max(ans,query(rson,L,R));
    return ans;
}
void  update(int rt,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        tree[rt]++;
        wt[rt]++;
        return ;
    }
    int m = (l+r)>>1;
    if(L<=m) update(lson,L,R);
    if(m<R) update(rson,L,R);
    pushu(rt);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int a,b;
    SF("%d%d%d%d",&n,&m,&k,&p);
    REP(i,0,k)
    {
        SI(a);
        SI(b);
        st.insert(PP(b,a));
        //mp[PP(a,b)] = i+1;
        //P aa = PP(a,b);int bb=i+1;
        mp.insert(pair<PP,int>(PP(a,b),i+1));
    }
    multiset<PP>::iterator it;
    build();

    while((int)st.size()>0)
    {
        it = st.begin();
        int a = it->second,b=it->first;
        if(query(1,1,n,a,b-1)>=m)
        {
            st.erase(it);
            continue;
        }
        update(1,1,n,a,b-1);
        multimap<PP,int>::iterator mi = mp.find(PP(it->second,it->first));
        L.push_back(mi->second);
        mp.erase(mi);
        que.push(it->first);
        st.erase(it);
    }
    cout<<(int)L.size()*p<<endl;
    REP(i,0,(int)L.size())
    {
        if(i) cout<<" ";
        cout<<L[i];
    }
    cout<<endl;
    return 0;
}



你可能感兴趣的:(数据结构,STL,贪心)