#310 (Div.2) D. Case of fugitive

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-Solving ideas: The problem using greedy method + priority queue solution. But the choice of greedy strategy is the key, some seemingly correct greedy strategy actually implies danger. First of all, the right greedy strategy: all the islands in order to find the first and I+1 islands between the minimum maximum length of the bridge, and according to L from small to large sort, if the same as r from small to large order. Then the bridge from small to large sort, will all the bridge scan again, enumerate the first bridge, the L value is less than equal to the current bridge interval according to (R,ID) into the priority queue, r Small in the team head, large at the end of the team, each time to see the first team R is greater than equal to Len, if satisfied, then record the answer There is no solution. Next, there are several classic error-greedy strategies.

error One : After sorting the interval according to the above rules, enumerate the intervals and select the bridge from small to large. Example: There are 2 intervals, sorted after [2,31], [5,19], the bridge from small to large after the sort is 19, 20. Following this policy will result in no solution.


Error Two : The interval according to the right endpoint from small to large sort, if R is the same, according to L from small to large, and then choose the bridge from small to large. Example: There are 2 intervals, sorted after [17,19], [2,31], the bridge from small to large after the sort is 2, 17. Following this policy will result in no solution.


In fact, the reason for the two errors is essentially the same: the priority of the interval endpoint actually does not guarantee the optimal solution of sub-problems. But why is it possible to enumerate bridges? Because the bridge is based on the length from small to large enumeration, if an interval of L to meet the short length of the bridge, then it will certainly meet the length of the larger bridge, if the team tail of the interval R value is still less than Len, then because the back of the bridge length is larger, R values less than Len, so this greedy strategy is correct.

3. Code:

#define _crt_secure_no_warnings#include<iostream> #include <algorithm> #include <cassert> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include <queue> #include <deque> #include <cstdlib> #include <cstdio> #include < cstring> #include <cmath> #include <ctime> #include <cctype> #include <functional>using namespace Std; #define ME (s) memset (s,0,sizeof (s)) #define PB push_backtypedef Long long ll;typedef unsigned int uint;typed EF unsigned long long ull;typedef pair <ll, int> p;const int N = 200000 + 10;ll x[n], y[n];ll length[n];int r[n];ll Ans[n];struct node{int id;ll l,r;bool operator< (const NODE&AMP;RHS) Const{return L < RHS. L | | (L = = rhs. L&&r < RHS. R);}} A[n];int N, M;bool cmp (int a, int b) {return length[a] < length[b];} int main () {while (~scanf ("%d%d", &n, &m)) {Me (a); Me (x); Me (y); me (length); Me (R); for (int i = 0; i<n; i++) scanf ("%i64d%i64d", &x[i], &y[i]); for (int i = 0; i < m; i++) {scanf ("%i64d", &length[i]); r[i] = i;} for (int i = 0; i<n-1; i++) {A[i] = {i, X[i + 1]-y[i], Y[i + 1]-x[i]}; Sort (A, a + n-1); Sort (R, R + M, CMP);p riority_queue<p, Vector<p>, greater<p> >q;int cnt = 0;int j = 0;for (int i = 0; I & Lt M i++) {int p = r[i];for (; j < n-1;j++) if (A[j]. L <= length[p])//Enter the queue that satisfies the left endpoint, while following the R value from small to large out of queue {Q.push (P (a[j]). R, A[j].id));} Else Break;while (!q.empty ()) {if (Q.top (). First >= length[p])//If the team's R value ≥ the current length of the bridge, record the answer, otherwise no solution (but there is no direct break here, Instead, use CNT to judge) {Ans[q.top (). second] = p; cnt++; Q.pop (); break;} Q.pop ();}} if (cnt = = n-1)//If the number of answers is not n-1, there must be no solution {puts ("Yes"); for (int i = 0; i<n-1; i++) printf ("%i64d%c", Ans[i] + 1, "\ n" [i = = N-2]);} Else puts ("No");}}

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

#310 (Div.2) D. Case of fugitive

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.