Lazy salesgirl-line segment tree Optimization

Source: Internet
Author: User

Refer to Region. When talking about the person above, I felt cordial as soon as I saw his line segment tree, because it was the same as what I wrote. Take a closer look at his blog and find that my KMP is followed by him. Let's take a look and find that he and I are in the same direction as line tree. It's so late! This question should be carefully understood by sorting there (equivalent to discretization) and three line segment trees. Discover the magic of a line segment tree. /* Pro, wait for more than w more time and no one will come and then fall asleep ...... It is known that the price of bread sold to everyone, and the number of breads sold to the K-person (indeed sold) is 1 + (k-1) mod 3, that is, 1, 2, 3, 3 ...... In this way, the maximum price is W, and the average price of the sold bread is the highest. It is assumed that no one of them has the same time, and the seller will fall asleep only after the time is strictly over w ). SOL: the first idea: record the number of breads bought by each person, the unit price, and insert them into the line segment tree. At the same time, the time interval between two people is recorded. Enumeration time interval (from 1 to the maximum time interval). For each time interval, mark the unsatisfied persons and count the final unit price. For the unit price obtained by all enumeration, the maximum unit price of the record and the corresponding interval. Improvement: The enumeration interval does not start from 1. If the interval between a customer and the previous customer exceeds W, the customer will not buy anything, customers with a time interval less than or equal to W from the previous customer can certainly buy bread. Because the answer must be an existing time interval, You can enumerate the existing time interval. Positive Solution: Practice: I think the key is to grasp the characteristics of the subject and dig deep into it. The salesperson will fall asleep every w time, which means,

1: If the interval between a customer and the previous customer exceeds W, the customer will not be confused about shopping, customers with a time interval less than or equal to W from the previous customer can certainly buy bread.

Therefore, we only need to enumerate each time interval. The answer must be a certain time interval. Once the time interval is fixed, the number of breads that can be sold and the number of customers who can buy things will be fixed.

2: The number of breads purchased can be obtained in the formula given by the question. The number of breads sold is 1 2 3 1 2 3 .... So if someone buys a few breads, you can

I figured out that the next Person X bought a few breads. Because there are only three situations at most, I saved the results of the three cases and used three line segments, it is actually a two-dimensional array for the domain of the Line Segment tree.

Line Segment tree domain:

Sum [RT]: There are several customers in the interval that records the current node

PP [RT] [3]: record the total sales when the leftmost person buys 1 2 3 breads respectively

Then we can pass this result through the line segment tree.

Key code:

For (INT I = 0; I <3; I ++)

PP [x] [I] = PP [x <1] [I] + PP [x <1 | 1] [(sum [x <1] + I) % 3];

Knowing the number of breads bought by the first person in the left subtree and the number of people in the left subtree, We can naturally introduce the number of breads bought by the first person in the right subtree. Record all three cases, then pass it on

The final result is PP [1] [0], because the first person must have bought only one loaf of bread;

Insert the values with the same time difference, calculate the average value of the sum, and obtain the maximum value. Because W is increasing, the original sold products can always be sold out, and the IDs in the line segment tree are increasing, so you do not need to delete them. Date: 12/08/08 */
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define maxn 100010 using namespace STD; // sum [] indicates the number of persons in the Range int sum [maxn <2]; // P indicates the amount of money sold in this interval. Double P [maxn <2] [3]; struct customer {int ID, T; double price; bool operator <(const customer & CMP) const {return T <CMP. t ;}} CUSA [maxn], cusb [maxn]; Vo Id Update (INT POs, int L, int R, int RT) {// POS indicates the first few. Sum [RT] ++; If (L = r) {for (INT I = 0; I <3; I ++) P [RT] [I] = 1.0 * (I + 1) * CUSA [POS]. price; // return;} int M = (L + r)> 1; if (Pos <= m) Update (Pos, lson); else Update (Pos, rson); For (INT I = 0; I <3; I ++) P [RT] [I] = P [RT <1] [I] + P [RT <1 | 1] [(sum [RT <1] + I) % 3] ;}int t, n; int main () {scanf ("% d", & T); While (t --) {scanf ("% d ", & N); For (INT I = 1; I <= N; I ++) {scanf ("% lf", & CUSA [I]. price) ;}for (INT I = 1; I <= N; I ++) {scanf ("% d", & CUSA [I]. t);} // first, use the ID to record which customer is the first customer, use T to record the time interval between him and the previous customer. Sort (CUSA + 1, CUSA + 1 + n); CUSA [0]. T = 0; For (INT I = 1; I <= N; I ++) {cusb [I]. T = CUSA [I]. t-CUSA [I-1]. t; cusb [I]. id = I;} memset (sum, 0, sizeof (SUM); memset (p, 0, sizeof (p); sort (cusb + 1, cusb + 1 + n); double ans_aver = 0, ans_w, TMP; For (INT I = 1, j = 1; I <= N; I = J) {// enumeration time interval, which can be processed together at the same interval, because the same time interval can be used to buy bread while (cusb [I]. T = cusb [J]. T & J <= N) {Update (cusb [J]. ID, 1, n, 1); j ++;} TMP = P [1] [0]/(sum [1] * 1.0); If (ans_aver <TMP) {ans_aver = TMP; ans_w = cusb [I]. t ;}} printf ("%. 6lf %. 6lf \ n ", ans_w, ans_aver);} return 0 ;}
Related Article

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.