HDU 5360 Hiking (segment tree)

Source: Internet
Author: User

Test instructions

n the condition that an individual accepts an invitation is the number of people who have accepted the invitation in L[i], R[i]
Asking how to set the order of invitations can make the number of people accepting invitations the most

Analytical:

First, the interval is sorted from small to large on the right edge, if the right boundary is the same, then the left border is sorted from small to large. Because the lower the right border, the higher the priority, the left edge is the same.
If the priority queue is used, it is calculated that choosing which interval is optimal for the current person.
Because I do not have two conditions priority queue, so can only change a way to write.
So I think of the line tree, reverse thinking, using line segment tree to maintain this interval to choose which person is the best.
After selecting this person, place the person in infinity and maintain the line tree.
and record the current person, which range is selected.

my code

#include <cstdio>#include <cstring>#include <algorithm>#define LS o<<1#define RS O<<1|1#define Lson ls, L, M#define Rson RS, m+1, Rusing namespace STD;Const intINF =0x3f3f3f3f;Const intN =1e5+Ten;intNstructNode {intL, R, id;} Node[n];intminv[n<<2];inline voidPushup (intO) {Minv[o] = min (Minv[ls], minv[rs]);}voidBuildintOintLintR) {if(L = = R) {Minv[o] = L;return; }intM = (L + R)/2;    Build (Lson);    Build (Rson); Pushup (o);}intQueryintOintLintRintQlintQR) {if(QL <= L && R <= qr)returnMinv[o];intM = (L + R)/2, ret = INF;if(QL <= M) ret = min (ret, query (Lson, QL, QR));if(QR > M) ret = min (ret, query (Rson, QL, QR));returnRET;}voidModifyintOintLintRintPOS) {if(L = = R) {Minv[o] = INF;return; }intM = (L + R)/2;if(POS <= M) Modify (Lson, POS);ElseModify (Rson, POS); Pushup (o);}intAns[n], res[n];BOOLVis[n];BOOLCMP (Node A, Node B) {if(A.R! = B.R)returnA.R < B.R;returnA.l < B.L;}intMain () {intTscanf("%d", &t); while(t--) {scanf("%d", &n); for(inti =1; I <= N; i++) {scanf("%d", &AMP;NODE[I].L);        Node[i].id = i; } for(inti =1; I <= N; i++)scanf("%d", &AMP;NODE[I].R); Sort (node+1, node+n+1, CMP);memset(ANS,-1,sizeof(ans));memset(Vis,false,sizeof(VIS)); Build1,0, n); for(inti =1; I <= N; i++) {intret = Query (1,0, N, NODE[I].L, NODE[I].R);if(ret! = INF)                {Ans[ret] = node[i].id; Modify1,0, N, ret); }        }inttot =0; for(inti =0; I < n; i++) {if(Ans[i]! =-1) {res[tot++] = ans[i]; Vis[ans[i]] =true; }Else  Break; }printf("%d\n", tot); for(inti =1; I <= N; i++) {if(!vis[i]) res[tot++] = i; } for(inti =0; i < tot; i++) {printf("%d", Res[i]);if(i = = tot-1)puts("");Else Putchar("'); }    }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5360 Hiking (segment tree)

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.