1178: [Apio2009]convention Conference Center _ Greedy

Source: Internet
Author: User
Tags time limit
1178: [Apio2009]convention Conference Center time limit:15 Sec Memory limit:162 MB
submit:824 solved:327
[Submit] [Status] [Discuss] Description

The Siruseri government has built a new conference center. Many companies are interested in renting halls in the conference center, and they want to be able to hold meetings there. For a client, he rented the synagogue only when he was able to occupy the whole hall alone during the meeting. The Sales Director at the Conference Center believes that the best strategy should be to rent the halls to as many customers as possible. It is clear that there are more than one policy that satisfies the requirements. For example, the following example. A total of 4 companies. They made a request for the rental hall and presented the dates of their need to occupy the synagogue (as shown in the table below). Start Date End Date Company 1 4 9 Company 2 9 11 Company 3 13 19 Company 4 10 17 in the above example, the hall will be rented to two companies. Leasing strategies are leased to companies 1 and 3, or 2 and 3 of companies, or 1 to companies and 4. Note that the Conference center leases up to one company a day, so companies 1 and 2 cannot rent the conference center at the same time because they coincide on the Nineth day. For the sake of fairness, the sales director decides which leasing strategy to choose: first, the strategy to be leased to the largest number of customers as a candidate, and all companies to be numbered in the order in which they make the request. For candidate policies, the number of each company in the policy is sorted in ascending order. Finally, the candidate strategy with the minimum 1 dictionary order is chosen as the final strategy. For example, the synagogue will eventually be loaned to company 1 and Company 3:3 candidate strategies are {(1,3), (2,3), (1,4)}. In the dictionary order (1,3) < (1,4) < (2,3). Your job is to help the sales director determine which companies should be loaned to the hall. Input

The first line you enter has an integer n that indicates the number of companies that issued the rental hall application. The 2nd to N+1 line has 2 integers per line. The integer in line i+1 represents the starting and ending dates for the first company to apply for loan. For each company's application, the start date is an integer that is not less than 1, and the expiration date is not greater than the 10^9 integer. n≤200000 Output

The first line of output should have an integer m representing the maximum number of companies that can be loaned. The second line should list the number of M, which will ultimately be loaned to the hall to which companies. Sample Input 4
6 {
9 11
13 19
2 Sample Output
1 3 HINT

Fix data bugs and add new data to a group by Nanoape 2016.5.11

Post-repair data: Judgeonline/upload/201605/dd.rar

Source

[Submit] [Status] [Discuss]




Remove the second query is classic greed,,,

Now the question is how to deal with the second one ...


Return to the first question

We remove all the larger of the range that contains the relationship, and it will not affect the result.

Because if one of the best schemes has that big interval, there must be no difference between the small selection.

So we get some neighborhoods where the left and right endpoints of these ranges are strictly incremented.

When we choose the most, 22 disjoint intervals from these intervals, that is the answer to the first question.

Greed can


Consider the second question,

For an interval, if you choose it, it has no effect on the optimal answer, which obviously is optional.

This takes into account each interval in chronological order, each check this interval will not make the answer worse, if not, we need it

How to check.

Obviously, if one of the positions is already covered in the current zone of consideration, the interval must not be selected.

If all is empty, then this interval must be included in a larger empty interval, remember this empty interval is [i,j], remember the current interval [l,r]

So if we know the best answer to [i,j], [i,l] and (R,j], we can judge

and have already dealt with some 22 intervals that do not contain each other.

Sort them by the left endpoint in ascending order

Right[i] is the first interval to the right, the one that does not intersect with it

For [L,r], we find all the intervals contained in [L,r], and go along the right array to find the answer.

This method can be optimized by multiplying algorithm O (logn).


#include <iostream> #include <cstdio> #include <queue> #include <vector> #include <bitset > #include <algorithm> #include <cstring> #include <map> #include <stack> #include <set
 
> #include <cmath> #include <ext/pb_ds/priority_queue.hpp> using namespace std;
const int MAXN = 4E5 + 10;
const int INF = ~0u>>1;
 
const int T = 16;
int n,cnt,top,tot,l[maxn],r[maxn],l2[maxn],r2[maxn],c[maxn*t], cur = 1,ans,right[maxn][20],s[maxn],num[maxn*2];
 
BOOL Mark[maxn*t];
    void Read () {cin >> n;
        for (int i = 1; I <= n; i++) {scanf ("%d%d", &l[i],&r[i));
        Num[++tot] = L[i];
    Num[++tot] = R[i];
    Sort (Num + 1,num + tot + 1);
    for (int i = 2; I <= tot i++) if (Num[i]!= num[i-1]) num[++cur] = Num[i];
    Num[++cur] = INF;
    L[0] = r[0] = cur;
        for (int i = 1; I <= n; i++) {L[i] = lower_bound (num + 1,num + cur + 1,l[i))-Num; R[i] = Lower_bound (num + 1,num + cur + 1,r[i])-num;
    if (R[r2[l[i]]] > R[i]) r2[l[i] = i; } void Work () {for (int i = 1; I <= cur; i++) {if (!
        R2[i]) continue;
        while (top && R[s[top]] >= r[r2[i])--top;
    S[++top] = R2[i];
    for (int i = 1; I <= top; i++) l2[i] = l[s[i]],r2[i] = r[s[i]];
    int now = top + 1;
    L2[now] = R2[now] = cur;
        for (int i = top; i; i--) {while (l2[now-1] > R2[i])--now;
        Right[i][0] = now;
    if (Right[i][0] > top) right[i][0] = 0; for (int j = 1; J < + j) for (int i = 1; I <= top; i++) right[i][j] = Right[right[i][j
-1]][J-1];
    int Query (int l,int r) {if (L > R) return 0;
    int pl = Lower_bound (L2 + 1,l2 + top + 1,l)-L2;
    int PR = lower_bound (R2 + 1,r2 + top + 1,r)-R2;
    if (R2[PR] > R | | | PR > top)--PR;
    if (pl > PR) return 0;
    int pos = Pl,ret = 1; For (inT j = 19; J >= 0; j--) {if (!
        RIGHT[POS][J]) continue;
            if (Right[pos][j] <= pr) {ret = (1&LT;&LT;J);
        pos = Right[pos][j];
} return ret;
        } void pushdown (int o,int l,int R) {if (Mark[o]) {mark[o<<1] = mark[o<<1|1] = 1;
        int mid = (L + r) >> 1;
        C[O&LT;&LT;1] = mid-l + 1;
    C[O&LT;&LT;1|1] = R-mid;
        } void Modify (int o,int l,int r,int ml,int MR) {if (ml <= l && R <= Mr) {Mark[o] = 1;
        C[o] = r-l + 1;
    Return
    } pushdown (O,l,r);
    int mid = (L + r) >> 1;
    if (ml <= mid) Modify (O&LT;&LT;1,L,MID,ML,MR);
    if (Mr > mid) Modify (O&LT;&LT;1|1,MID+1,R,ML,MR);
    if (mark[o<<1] && mark[o<<1|1]) mark[o] = 1;
C[o] = c[o<<1] + c[o<<1|1];
    int Lower (int o,int l,int r,int ql,int qr) {int mid = (L + r) >> 1;
   if (QL <= l && r <= qr) {     if (!c[o]) return cur;
        if (Mark[o]) return l;
        Pushdown (O,L,R);
        if (c[o<<1]) return Lower (O&LT;&LT;1,L,MID,QL,QR);
    else return Lower (O<<1|1,mid + 1,R,QL,QR);
    } pushdown (O,l,r);
    int ret = cur;
    if (QL <= mid) ret = min (ret,lower (O&LT;&LT;1,L,MID,QL,QR));
    if (Ret < cur) return ret;
    if (QR > mid) ret = min (ret,lower (O&LT;&LT;1|1,MID+1,R,QL,QR));
return ret;
    int Upper (int o,int l,int r,int ql,int qr) {int mid = (L + r) >> 1;
        if (QL <= l && r <= qr) {if (!c[o)) return 0;
        if (Mark[o]) return R;
        Pushdown (O,L,R);
        if (c[o<<1|1]) return Upper (O&LT;&LT;1|1,MID+1,R,QL,QR);
    else return Upper (O&LT;&LT;1,L,MID,QL,QR);
    } pushdown (O,l,r);
    int ret = 0;
    if (QR > mid) ret = max (Ret,upper (O&LT;&LT;1|1,MID+1,R,QL,QR));
    if (ret) return ret;
    if (QL <= mid) ret = max (Ret,upper (O&LT;&LT;1,L,MID,QL,QR));
return ret; }
 
int Sum (int o,int l,int r,int ql,int qr) {if (QL <= l && r <= qr) return c[o];
    Pushdown (O,L,R);
    int mid = (L + r) >> 1,ret = 0;
    if (QL <= mid) ret = Sum (O&LT;&LT;1,L,MID,QL,QR);
    if (QR > mid) ret = Sum (O&LT;&LT;1|1,MID+1,R,QL,QR);
return ret;
    int main () {//freopen ("1178.in", "R", stdin);
     
    Freopen ("1178.out", "w", stdout);
    Read ();
    Work ();
    Modify (1,0,cur,0,0);
    Modify (1,0,cur,cur,cur);
    Ans = Query (1,cur);
    printf ("%d\n", ans);
    int K = 0;
        for (int i = 1; I <= n; i++) {if (Sum (1,0,cur,l[i],r[i)) > 0) continue;
        int L = Upper (1,0,cur,0,l[i]-1);
        int r = Lower (1,0,cur,r[i] + 1,cur); ++l;
        --r;
        int Need = Query (l,r);
        int suml = Query (L,l[i]-1);
        int SUMR = Query (R[i] + 1,r);
            if (suml + sumr + 1 = Need) {++k;
            if (K < ans) printf ("%d", I);
            else printf ("%d", I); Modify (1,0,CUR,l[i],r[i]);
} return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.